问题描述
我遇到过
具有 2 个不同百分比的优惠券基于 Woocommerce 中产品类别的折扣
其中显示了如何更改设置类别的折扣百分比.我想在我的商店中实施一个夏季折扣代码,为选择的产品提供 20 英镑的折扣.
Which shows how to alter % discounts for set categories. I want to implement a summer discount code on my store which gives £20 off to a selection of products.
优惠券代码 summer21 设置为仅对以下允许的产品 ID 应用固定产品折扣
The coupon code summer21 is set to apply a fixed product discount on only the following allowed product IDs
- 2523, 119800, 119886 &120253
对于 4 个中的 3 个,应享受 20 英镑的折扣
On 3 of the 4 a £20 discount should apply
但是如果产品 ID = 119886,那么应该只打折 10 英镑
But if the product ID = 119886, then only £10 should be discounted
我尝试将代码修改为以下内容,但不幸的是没有达到预期的结果.我错过了什么?
I've tried modifying the code to the following, but unfortunately without the desired result. What am I missing?
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;
}
此代码也仅更改购物车价格,但并未在产品页面上显示正确的显示价格.
This code also only changes the cart price, but does not show the correct display prices on the product pages.
推荐答案
你的代码有几个错误
has_term()
不适用,我们使用in_array()
代替- 不考虑每个产品的购物车商品数量.
has_term()
is not applicable, we are usingin_array()
instead- Cart item quantity per product is not taken into account.
为了使这个答案有效,我们应用了一些步骤
To make this answer work, we apply some steps
第一步,优惠券设置:
- 标题:summer21
- 一般>折扣类型:固定产品折扣
- 一般>优惠券金额:20
- 使用限制>产品:其中产品 ID:2523、119800、119886、120253
第二步,代码功能里面的设置:
Second step, settings inside the code function:
- 优惠券代码:将您的优惠券代码设置为小写
- 产品 ID,第二次折扣:119886
- 设置第二个折扣:10
所以你得到:
function filter_woocommerce_coupon_get_discount_amount( $round, $discounting_amount, $cart_item, $single, $coupon ) {
// Related coupons codes to be defined in this array
$coupon_codes = array( 'summer21' );
// Product IDs, for second discount
$product_ids = array( 119886 );
// Second discount
$second_discount = 10;
// Changing the discount for specific product Ids
if ( $coupon->is_type('fixed_product') && in_array( $coupon->get_code(), $coupon_codes ) ) {
// Search for product ID in array product IDs
if ( in_array( $cart_item['product_id'], $product_ids ) ) {
// Get cart item quantity
$cart_item_qty = is_null( $cart_item ) ? 1 : $cart_item['quantity'];
// Discount
$discount = $coupon->get_amount() - $second_discount;
$discount = $single ? $discount : $discount * $cart_item_qty;
$round = round( min( $discount, $discounting_amount ), wc_get_rounding_precision() );
}
}
return $round;
}
add_filter( 'woocommerce_coupon_get_discount_amount', 'filter_woocommerce_coupon_get_discount_amount', 10, 5 );
这篇关于基于 WooCommerce 中的产品 ID 的 2 种不同固定产品折扣的优惠券的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!