WooCommerce: aggiornare la quantità presente in magazzino di un prodotto

WooCommerce: aggiornare la quantità presente in magazzino di un prodotto

In WooCommerce possiamo aggiornare la quantità presente in magazzino di un prodotto.

La soluzione è la seguente:


<?php

function my_wc_update_stock_qty( $product_id, $qty ) {
    if( !is_numeric( $qty ) || !filter_var( intval( $qty ), FILTER_VALIDATE_INT ) ) {
        return;
    }
    if( !is_numeric( $product_id ) || !filter_var( intval( $product_id ), FILTER_VALIDATE_INT ) ) {
        return;
    }
    update_post_meta( intval( $product_id ), '_stock', intval( $qty ) );
}

Torna su