1 回答

TA貢獻(xiàn)2003條經(jīng)驗(yàn) 獲得超2個(gè)贊
以下僅將可變產(chǎn)品的最后一個(gè)變體“靜默”保留在購(gòu)物車中:
add_action('woocommerce_before_calculate_totals', 'keep_last_variation_from_variable_products', 10, 1 );
function keep_last_variation_from_variable_products( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$parent_ids = array();
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Only for product variations
if ( $cart_item['variation_id'] > 0 ) {
// When a variable product exist already in cart
if( isset($parent_ids[$cart_item['product_id']]) && ! empty($parent_ids[$cart_item['product_id']]) ) {
// Remove it
$cart->remove_cart_item($parent_ids[$cart_item['product_id']]);
}
// Set in the array the cart item key for variable product id key
$parent_ids[$cart_item['product_id']] = $cart_item_key;
}
}
}
代碼位于活動(dòng)子主題(或活動(dòng)主題)的functions.php 文件中。經(jīng)過測(cè)試并有效。
- 1 回答
- 0 關(guān)注
- 126 瀏覽
添加回答
舉報(bào)