WooCommerce: aggiornare la quantità di un prodotto in stock

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

WordPress di Gabriele Romanato

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

La soluzione è la seguente:


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 ) );
}

Commenti