这可以通过 2 个步骤实现:
1)添加一个独特的优惠券:
- 在常规设置 > 类型 =固定产品折扣
- 在常规设置 > 金额 =
30
- 在使用限制 > 产品 ==>设置您想要的产品
2)添加此代码(您将在函数中设置优惠券代码(小写)):
add_filter( 'woocommerce_coupon_get_discount_amount', 'custom_coupon_get_discount_amount', 10, 5 );
function custom_coupon_get_discount_amount( $rounded_discount, $discounting_amount, $cart_item, $single, $coupon ){
## ---- Your settings ---- ##
// Related coupons codes to be defined in this array (you can set many)
$coupon_codes = array('30perqty');
## ------ The code ------- ##
if ( $coupon->is_type('fixed_product') && in_array( $coupon->get_code(), $coupon_codes ) && $cart_item['quantity'] > 1 ) {
if( in_array( $cart_item['product_id'], $coupon->get_product_ids() ) ){
$discount = (float) $coupon->get_amount() * (int) $cart_item['quantity'];
$round = round( min( $discount, $discounting_amount ), wc_get_rounding_precision() );
}
}
return $rounded_discount;
}
代码位于您的活动子主题(或活动主题)的 function.php 文件中。测试和工作。