简体   繁体   中英

Function that retrieves the stock quantity from a product in WooCommerce

I'm trying to create a function that retrieves the stock quantity from a product in WooCommerce. If I comment out the following line the code runs but if I leave it in I get a white screen: $product = is_a( $product, 'WC_Product' ) ? $product : wc_get_product($product_id);

Any ideas what I'm doing wrong here?

function wc_get_variable_product_stock_quantity( $output = 'raw', $product_id = 0 ){
    global $wpdb, $product;

    // Get the product ID (can be defined)
    $product_id = $product_id > 0 ? $product_id : get_the_id();

    // Check and get the instance of the WC_Product Object
    $product = is_a( $product, 'WC_Product' ) ? $product : wc_get_product($product_id);
    $stock_quantity = $product->get_stock_quantity();

    return $stock_quantity;

}

EDIT (UPDATE):

My end goal is to sum the stock quantities for all variations after order status changes to complete and then write the total to the parent product's stock quantity so the total stock quantity is always up to date.

My over simplified example above should actually look like the below code mostly based on " Get the total stock of all variations from a variable product In Woocommerce " answer code, with light changes (see the additional SQL statement) :

    function wc_get_variable_product_stock_quantity( $output = 'raw', $product_id = 0 ){
        global $wpdb, $product;

        // Get the product ID (can be defined)
        $product_id = $product_id > 0 ? $product_id : get_the_id();

        // Check and get the instance of the WC_Product Object
        $product = is_a( $product, 'WC_Product' ) ? $product : wc_get_product($product_id);
        // Get the stock quantity sum of all product variations (children)
        $stock_quantity = $wpdb->get_var("
            SELECT SUM(pm.meta_value)
            FROM {$wpdb->prefix}posts as p
            JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
            WHERE p.post_type = 'product_variation'
            AND p.post_status = 'publish'
            AND p.post_parent = '$product_id'
            AND pm.meta_key = '_stock'
            AND pm.meta_value IS NOT NULL
        ");   
        return $stock_quantity;
    }


// Update stock totals for parent product when order status changes to completed...
function mysite_woocommerce_order_status_completed( $order_id ) {

    // For each item in the order, calculate the total stock quantity for all variations and write the total to the parent products's stock quantity
    $order = wc_get_order( $order_id );
    $order_id = $order->get_id();

    $log_txt = '';
    $items = $order->get_items();
    foreach($items as $k=>$val){     
        $product_id = $val[‘product_id’];
        $total_stock_quantity = wc_get_variable_product_stock_quantity( 'raw', $product_id );
        $log_txt .= 'Product ID:'.$val['product_id'].', Name: '.$val['name'].', Total Stock: '.$total_stock_quantity.'\n<br>';
        // Next: add code to write the stock total to the parent product
    }
}

add_action( 'woocommerce_order_status_completed', 'mysite_woocommerce_order_status_completed', 10, 1 );

You say that you want to create a function that retrieves the stock quantity from a product in WooCommerce, so why you dont use directly one of those :

  • With the product object instance use the WC_Product method get_stock_quantity() ,
  • or With the product ID use get_post_meta( $product_id, '_stock', true ) .

Now, based on " Get the total stock of all variations from a variable product In Woocommerce " answer, your code has some unneeded things and can be made differently in a much more light, compact and working way:

function wc_get_variable_product_stock_quantity( $product_id = 0 ){
    // Get the product ID (can be defined)
    $product_id = $product_id > 0 ? $product_id : get_the_ID();

    return get_post_meta( $product_id, '_stock', true );
}

Code goes in function.php file of the active child theme (or active theme). It should better works.

Or you can just use directly:

 get_post_meta( $product_id, '_stock', true ); 

or

 get_post_meta( get_the_ID(), '_stock', true ); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM