1 回答
TA贡献2003条经验 获得超2个赞
以下仅将可变产品的最后一个变体“静默”保留在购物车中:
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;
}
}
}
代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。
- 1 回答
- 0 关注
- 92 浏览
添加回答
举报