我试图根据3个不同的
来更改折扣百分比例如:
问题是如何在所选产品中应用此折扣? 以及如何制作手册,我的意思是当客户在可用的colomn中输入优惠券代码时折扣工作?
到目前为止这是我的代码
add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_total',
25, 1 );
function discount_based_on_total( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$total = $cart->cart_contents_total;
// Percentage discount (10%)
if( $total >= 200000 )
$discount = $total * 0.1;
if( $total >= 350000 )
$discount = $total * 0.15;
if( $total >= 500000 )
$discount = $total * 0.20;
$cart->add_fee( __('discount', 'woocommerce'), -$discount );
}
答案 0 :(得分:0)
您还应该为前两个条件添加上限。否则,您可以在多个if条件下进入多个。
例如390K大于200K且大于350K。
add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_total',25, 1 );
function discount_based_on_total( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$total = $cart->cart_contents_total;
// Percentage discount (10%)
if( $total >= 200000 && $total < 350000 ){
$discount = $total * 0.1;
}
else if( $total >= 350000 && $total < 500000 ){
$discount = $total * 0.15;
}
else if( $total >= 500000 ){
$discount = $total * 0.20;
}
else{
$discount = 0;
}
$cart->add_fee( __('discount', 'woocommerce'), -$discount );
}