在WooCommerce中从产品中检索库存数量的功能

问题描述 投票:1回答:1

我正在尝试创建一个从WooCommerce中的产品中检索库存数量的函数。如果我注释掉以下代码,那么代码就会运行,但是如果我把它留在里面,我会得到一个白色的屏幕:$ product = is_a($ product,'WC_Product')? $ product:wc_get_product($ product_id);

我在这里做错了什么想法?

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;

}

编辑(更新):

我的最终目标是在订单状态更改完成后将所有变更的库存数量相加,然后将总数写入父产品的库存数量,以使总库存数量始终保持最新。

我上面的简化示例实际上应该看起来像下面的代码主要基于“Get the total stock of all variations from a variable product In Woocommerce”答案代码,轻微更改(请参阅其他SQL语句):

    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 );
php wordpress woocommerce product product-quantity
1个回答
2
投票

你说你想要创建一个从WooCommerce中的产品中检索库存数量的函数,那么为什么你不直接使用其中一个:

  • 使用产品对象实例使用WC_Product方法get_stock_quantity()
  • 或使用产品ID使用get_post_meta( $product_id, '_stock', true )

现在,根据“Get the total stock of all variations from a variable product In Woocommerce”的答案,你的代码有一些不需要的东西,可以用更轻,紧凑和工作的方式制作:

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

代码位于活动子主题(或活动主题)的function.php文件中。它应该更好地工作。

或者您可以直接使用:

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

要么

get_post_meta( get_the_ID(), '_stock', true );
© www.soinside.com 2019 - 2024. All rights reserved.