繁体   English   中英

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

[英]Function that retrieves the stock quantity from a product in WooCommerce

我正在尝试创建一个函数,该函数从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;

}

编辑(更新):

我的最终目标是对订单状态更改完成后所有变体的库存量求和,然后将总数写入母产品的库存量,以便总库存量始终是最新的。

我上面的过度简化示例实际上应该看起来像以下代码,主要基于“ 从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 );

您说您想创建一个从WooCommerce中的产品中检索库存数量的函数,因此为什么不直接使用其中之一

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

现在,基于“ 从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 ); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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