【发布时间】:2021-07-09 08:30:01
【问题描述】:
我遇到过
Coupon with 2 different percentage discounts based on product category in Woocommerce
其中显示了如何更改设置类别的折扣百分比。我想在我的商店中实施一个夏季折扣代码,该代码可为精选产品提供 20 英镑的折扣。
优惠券代码 summer21 设置为仅对以下允许的产品 ID 应用固定产品折扣
- 2523、119800、119886 和 120253
4 个中的 3 个应适用 20 英镑的折扣
但是如果产品 ID = 119886,那么应该只打折 10 英镑
我尝试将代码修改为以下内容,但不幸的是没有达到预期的结果。我错过了什么?
add_filter( 'woocommerce_coupon_get_discount_amount', 'alter_shop_coupon_data', 20, 5 );
function alter_shop_coupon_data( $round, $discounting_amount, $cart_item, $single, $coupon ){
## ---- Your settings ---- ##
// Related coupons codes to be defined in this array (you can set many)
$coupon_codes = array('Summer21');
// Product categories for only £10 discount (IDs, Slugs or Names)
$product_category10 = array('119886'); // for £10 discount
$second_discount = 10; // £10
## ---- The code: Changing the discount to £10 for specific products ---- ##
if ( $coupon->is_type('fixed_product') && in_array( $coupon->get_code(), $coupon_codes ) ) {
if( has_term( $product_category10, 'product_cat', $cart_item['product_id'] ) ){
$discount = $discounting_amount - $second_discount;
$round = round( min( $discount, $discounting_amount ), wc_get_rounding_precision() );
}
}
return $round;
}
此代码也只会更改购物车价格,但不会在产品页面上显示正确的显示价格。
【问题讨论】:
标签: php wordpress woocommerce product coupon