1 回答

TA貢獻1887條經驗 獲得超5個贊
當一種變體缺貨時,以下操作將使所有變體缺貨(對于特定可變產品)(也適用于 WooCommerce 訂閱):
add_filter('woocommerce_available_variation', 'set_all_variations_out_of_stock', 10, 3 );
function set_all_variations_out_of_stock( $data, $product, $variation ) {
// Set the Id(s) of the related variable product(s) below in the array
if( in_array( $product->get_id(), array(738) ) ){
$out_of_stock = false; // initializing
// Loop through children variations of the parent variable product
foreach( $product->get_visible_children() as $_variation_id ) {
if( $_variation_id != $data['variation_id'] ) {
$_variation = wc_get_product($_variation_id);
if( ! $_variation->is_in_stock() ) {
$out_of_stock = true; // Flag as out of stock
break;
}
}
}
if ( $out_of_stock ) {
$data['availability_html'] = '<p class="stock out-of-stock">'. __('Out of stock', 'woocommerce') .'</p>';
$data['is_in_stock'] = false;
}
}
return $data;
}
代碼位于活動子主題(活動主題)的 function.php 文件中。經過測試并有效。
重要的提示:
可以在父變量產品上管理庫存。
對可變產品啟用庫存管理(在“庫存”選項卡上)并在那里設置庫存。
禁用此可變產品的每個變體的庫存管理。
你完成了。庫存管理現在針對可變產品進行處理。
- 1 回答
- 0 關注
- 99 瀏覽
添加回答
舉報