Get the total stock of all variations from a variable product In Woocommerce

前端 未结 2 1948
醉梦人生
醉梦人生 2020-12-04 01:10

IN woocommerce I would like to display the total stock of all variations in a variable product. The stock of each product variation is managed at the product variation level

2条回答
  •  無奈伤痛
    2020-12-04 01:27

    The following custom function will displays the sum of all product variations stock quantity for a variable product (only).

    It use a very light SQL query that make the sum of all children variations from a variable product.

    Note: The stock need to be managed at product variation level.

    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);
    
        // Only for variable product type
        if( $product->is_type('variable') ){
    
            // 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
            ");
    
            // Preparing formatted output
            if ( $stock_quantity > 0 ) {
                $html = '

    '. sprintf( __("%s in stock", "woocommerce"), $stock_quantity ).'

    '; } else { if ( is_numeric($stock_quantity) ) $html = '

    ' . __("Out of stock", "woocommerce") . '

    '; else $html = ''; } // Different output options if( $output == 'echo_html' ) echo $html; elseif( $output == 'return_html' ) return $html; else return $stock_quantity; } }

    Code goes in function.php file of your active child theme (or active theme). Tested and works.


    USAGE (3 cases):

    1) Anywhere in php code, get the stock quantity (from a dynamic $product_id):

    $stock_quantity = wc_get_variable_product_stock_quantity( 'raw', $product_id );
    

    2) Display in shop and archives pages (Under the price range example):

    add_action( 'woocommerce_after_shop_loop_item_title', 'display_variable_product_stock_quantity', 20 );
    function display_variable_product_stock_quantity(){
        wc_get_variable_product_stock_quantity( 'echo_html' );
    }
    

    Code goes in function.php file of your active child theme (or active theme).


    3) Display in single product pages (Under the price range example):

    add_action( 'woocommerce_single_product_summary', 'display_variable_product_stock_quantity', 15 );
    function display_variable_product_stock_quantity(){
        wc_get_variable_product_stock_quantity( 'echo_html' );
    }
    

    Code goes in function.php file of your active child theme (or active theme).

提交回复
热议问题