我有一个小问题,不知道如何解决自己。我想在我的 Woocommerce 商店中仅针对一种产品使用此逻辑。
我使用这样的链接来自动应用优惠券代码并添加到购物车:
https://testsite.com/checkout/?add-to-cart=Product_ID&quantity=1&coupon=Coupon_Code
当数量为 1 时,这似乎有效。但我想要在单击之前的直接链接时自动放置折扣(30%),以使其动态化,例如:
- 购物车页面增加数量至2,优惠券自动计算
2 x 30$ = 60$
折扣,
- 购买 3 个相同产品,然后计算
3 X 30$ = 90$
优惠券折扣
等等..
我搜索了一下,发现了这个usefull https://stackoverflow.com/questions/43570692/woocommerce-add-a-discount-based-on-individual-items-quantity线程,但情况与我的略有不同。
如何才能获得特定优惠券?一些建议或起点。谢谢
这可以通过以下两个步骤实现:
1) 添加独特的优惠券:
- 在常规设置 > 类型 =固定产品折扣
- 在常规设置 > 金额 =
30
- 在使用限制>产品==>设置您想要的产品(s)
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 文件中。经过测试并有效。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)