【发布时间】:2020-10-28 11:46:12
【问题描述】:
我目前的折扣基本上是说
- 如果购物车中有特定类别中的 6 件产品 --- 总价折扣 10 美元
这段代码运行良好。我的问题是,这不起作用
- 如果有 7 个产品,则第 7 个产品属于不同的类别。
我的目标是无论购物车中有多少产品,只要“A 类”中有 6 种产品,我都会给予折扣。
只要“折扣类别”中有 6 个产品或有 1 个数量为 6 的产品等,以下代码就可以工作。当我添加另一个类别的产品时,它会崩溃。随意撕开它。
add_action( 'woocommerce_before_calculate_totals', 'this_item_free' );
function this_item_free() {
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$valid_product_category_id = array('soup-mix');
$has_category = false;
$count = WC()->cart->get_cart_contents_count();
foreach ( WC()->cart->get_cart() as $product ) {
$quantity = $product['quantity'];
$price = $product['data']->get_price();
}
if ( has_term( $valid_product_category_id, 'product_cat', $product['product_id'],
$product['quantity'] ) ) {
$has_category = true;
$cart_total = floatval( preg_replace( '#[^\d.]#', '', WC()->cart->get_cart_total() ) );
if($count == 6 && $has_category = true){
add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_total', 25, 1 );
function discount_based_on_total( $cart ) {
$total = $cart->cart_contents_total;
$discount = 9.95;
$cart->add_fee( __('discount', 'woocommerce'), -$discount );
wc_add_notice( apply_filters( 'discount_applied', 'You just got a free soup!') );
}
}
}
}
【问题讨论】:
标签: wordpress woocommerce categories cart