WooCommerce:自动应用相同的优惠券几次
问题描述:
在WooCommerce,我自动应用优惠券,但我不能找到一个办法让这种行为:WooCommerce:自动应用相同的优惠券几次
- 每次X项目在购物车=>申请适当的优惠券
例如:
- 如果笔者=> -5€上车的3本书
- 如果额外的(同样是其他人)同一作者3本书=> -5额外€
- 等(没有限制:它应该工作,如果3000名的书籍订购=> -15000€)
我使用$ WC-> cart-> add_discount($折扣),但它已返回”优惠券应用“作为第二个g一组物品在购物车中。
你知道这是可能吗?
感谢
答
而不是使用优惠券,你应该更好地使用自定义功能折让得到这个工作:
add_action('woocommerce_cart_calculate_fees', 'cart_item_discount_by3', 10, 1);
function cart_item_discount_by3($cart_object) {
if (is_admin() && ! defined('DOING_AJAX'))
return;
// initializing and set variables
$discount = 0;
$by3 = 3; // each 3 item quantity
$dicount_price_by3 = 5; // amout to discount each 3 items quantity
// Iterating through each cart item
foreach($cart_object->get_cart() as $cart_item):
// Get the item quantity
$qty = $cart_item["quantity"];
// starting when quantity is upto 3
if($qty >= $by3):
for($j = $by3, $k = 0; $j <= $qty; $j+=$by3, $k++);
$discount += $dicount_price_by3 * $k;
break;
endif;
endforeach;
// Adding the discount (a negative fee)
if ($discount > 0){
$cart_object->add_fee(__("Discount quantity", 'woocommerce'), -$discount, true);
# Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
// Displaying a custom notice (optional)
wc_clear_notices();
wc_add_notice(__("You get a quantity discount on some items"), 'notice');
}
}
代码放在您的活动子主题的function.php文件(或主题)或者在任何插件文件中。
此代码已经过测试并可正常工作。
谢谢!它的工作原理:)而且我还在研究另一种解决方案,它的详细程度并不高。 –