如何在支付 WooCommerce 订单后重新保存可变产品的所有变体?
How to re save all variations of a variable product when a WooCommerce order is paid?
我使用以下代码重新保存产品变体,当有已付款订单时,但没有任何反应
add_action('woocommerce_payment_complete', 'refresh_zero_stock');
function refresh_zero_stock($order_id){
$order = new WC_Order( $order_id );
foreach ($order->get_items() as $item_key => $item ){
$item_quantity = $item->get_quantity();
if($item_quantity == 0){
$product_id = $item->get_product_id();
$product_data = wc_get_product($product_id);
if ($product_data->is_type('variable')){
$handle = new WC_Product_Variable($product_id);
$variations1=$handle->get_children();
foreach ($variations1 as $value) {
$single_variation=new WC_Product_Variation($value);
$single_variation->save();
}
}
}
}
}
这个动作挂钩有什么问题?请帮忙
hook 不是问题...主要问题是 if( $item->get_quantity() == 0 ){
总是 false
,其中 $item->get_quantity()
需要替换为 产品库存数量 代替。尝试以下操作:
add_action('woocommerce_payment_complete', 'refresh_zero_stock');
function refresh_zero_stock( $order_id ){
$order = wc_get_order( $order_id );
foreach ($order->get_items() as $item ){
$product = $item->get_product(); // Get the current product (object)
// If stock quantity is 0 and if it's a product variation
if ( $product->get_stock_quantity() == 0 && $product->is_type('variation') ){
$parent_product = wc_get_product( $item->get_product_id() ); // Get parent variable product
// Loop through children Ids (variations ids) from the parent variable product
foreach ($parent_product->get_children() as $child_id ) {
$variation = wc_get_product( $child_id ); // Get the product variation from each child Id
$variation->save(); // refresh and save
}
}
}
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。它应该更好用。
由于挂钩woocommerce_payment_complete
似乎不起作用,请根据订单状态更改尝试以下操作(注意:此代码将运行每个订单仅执行一次):
add_action( 'woocommerce_order_status_changed', 'refresh_zero_stock', 10, 4 );
function refresh_zero_stock( $order_id, $from_status, $to_status, $order ){
$variations_refreshed = $order->get_meta('_variations_refreshed');
if( in_array($to_status, array('processing', 'completed') ) && ! $variations_refreshed ) {
// Loop though order items
foreach ($order->get_items() as $item ){
$product = $item->get_product(); // Get the current product (object)
// If stock quantity is 0 and if it's a product variation
if ( $product->get_stock_quantity() == 0 && $product->is_type('variation') ){
$parent_product = wc_get_product( $item->get_product_id() ); // Get parent variable product
// Loop through children Ids (variations ids) from the parent variable product
foreach ($parent_product->get_children() as $child_id ) {
$variation = wc_get_product( $child_id ); // Get the product variation from each child Id
$variation->save(); // refresh and save
}
}
}
}
// Flag the order to make this code run only once for current order
$order->update_meta_data( '_variations_refreshed', '1' );
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。它可以更好地工作。
我找到问题了。我必须添加
$variation->set_manage_stock(false);
$variation->set_stock_status('outofstock');
之前
$variation->save();
我使用以下代码重新保存产品变体,当有已付款订单时,但没有任何反应
add_action('woocommerce_payment_complete', 'refresh_zero_stock');
function refresh_zero_stock($order_id){
$order = new WC_Order( $order_id );
foreach ($order->get_items() as $item_key => $item ){
$item_quantity = $item->get_quantity();
if($item_quantity == 0){
$product_id = $item->get_product_id();
$product_data = wc_get_product($product_id);
if ($product_data->is_type('variable')){
$handle = new WC_Product_Variable($product_id);
$variations1=$handle->get_children();
foreach ($variations1 as $value) {
$single_variation=new WC_Product_Variation($value);
$single_variation->save();
}
}
}
}
}
这个动作挂钩有什么问题?请帮忙
hook 不是问题...主要问题是 if( $item->get_quantity() == 0 ){
总是 false
,其中 $item->get_quantity()
需要替换为 产品库存数量 代替。尝试以下操作:
add_action('woocommerce_payment_complete', 'refresh_zero_stock');
function refresh_zero_stock( $order_id ){
$order = wc_get_order( $order_id );
foreach ($order->get_items() as $item ){
$product = $item->get_product(); // Get the current product (object)
// If stock quantity is 0 and if it's a product variation
if ( $product->get_stock_quantity() == 0 && $product->is_type('variation') ){
$parent_product = wc_get_product( $item->get_product_id() ); // Get parent variable product
// Loop through children Ids (variations ids) from the parent variable product
foreach ($parent_product->get_children() as $child_id ) {
$variation = wc_get_product( $child_id ); // Get the product variation from each child Id
$variation->save(); // refresh and save
}
}
}
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。它应该更好用。
由于挂钩woocommerce_payment_complete
似乎不起作用,请根据订单状态更改尝试以下操作(注意:此代码将运行每个订单仅执行一次):
add_action( 'woocommerce_order_status_changed', 'refresh_zero_stock', 10, 4 );
function refresh_zero_stock( $order_id, $from_status, $to_status, $order ){
$variations_refreshed = $order->get_meta('_variations_refreshed');
if( in_array($to_status, array('processing', 'completed') ) && ! $variations_refreshed ) {
// Loop though order items
foreach ($order->get_items() as $item ){
$product = $item->get_product(); // Get the current product (object)
// If stock quantity is 0 and if it's a product variation
if ( $product->get_stock_quantity() == 0 && $product->is_type('variation') ){
$parent_product = wc_get_product( $item->get_product_id() ); // Get parent variable product
// Loop through children Ids (variations ids) from the parent variable product
foreach ($parent_product->get_children() as $child_id ) {
$variation = wc_get_product( $child_id ); // Get the product variation from each child Id
$variation->save(); // refresh and save
}
}
}
}
// Flag the order to make this code run only once for current order
$order->update_meta_data( '_variations_refreshed', '1' );
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。它可以更好地工作。
我找到问题了。我必须添加
$variation->set_manage_stock(false);
$variation->set_stock_status('outofstock');
之前
$variation->save();