我正在尝试创建一个函数,该函数从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 );
答案 0 :(得分:2)
您说要创建一个从WooCommerce中的产品中检索库存数量的函数,所以为什么不直接使用其中之一:
WC_Product
方法get_stock_quantity()
,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 );