【发布时间】:2021-05-29 07:46:13
【问题描述】:
我正在尝试创建一个自动折扣,当购物车包含至少三种或更多产品时,该折扣就会生效。
如果是这样,无论客户是否登录,都应给予 10% 的折扣。
这是我试图开始工作的代码(没有成功)。
add_action( 'woocommerce_cart_calculate_fees', 'wc_discount_when_more_than_three', 10, 1 );
function wc_discount_when_more_than_three( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 10; // 10% discount when cart has a minimum of three products
$discount = 0;
// check all cart items
foreach ( $cart->get_cart() as $cart_item ) {
// when quantity is more than 3
if( $cart_item['quantity'] > 3 ) {
// give 10% discount on the subtotal
$discount = $percentage / 100;
}
}
if( $discount > 0 )
$cart->add_fee( __( '10% OFF', 'woocommerce' ) , -$discount );
}
【问题讨论】:
标签: php wordpress woocommerce cart discount