WooCommerce - 使一组优惠券添加固定费用到一个订单
问题描述:
一个客户有一个非常奇怪的请求:他们想要一组优惠券代码增加一个费用一个订单。这可能吗?WooCommerce - 使一组优惠券添加固定费用到一个订单
他们正在试图解决的问题是这样的:
目前店内提供免费送货到48名美国,我有航运类和表费率正确配置。但是,客户在每日交易网站上出售的优惠券应该取消免费送货优惠并增加固定运费。
这怎么能实现呢?
答
是的,你可以用代码段(一组优惠券)做到这一点:
function coupon_add_cart_fee($bookable_total = 0) {
$has_coupon = false;
// Set here your specials coupons slugs (one by line - last one have no coma)
$coupon_codes = array (
'your_coupon_code_slug1',
'your_coupon_code_slug2',
'your_coupon_code_slug3'
);
// Set here your fee amount or make fees calculation (see the links in reference)
$fee = 20;
if (is_admin() && ! defined('DOING_AJAX'))
return;
// checking if that "special" coupon is applied to customer cart
foreach ($coupon_codes as $coupon_code) {
if (WC()->cart->has_discount($coupon_code)) {
// If yes apply the fee to the cart
if (!$has_coupon) {
$has_coupon = true;
break;
}
}
}
if ($has_coupon) {
WC()->cart->add_fee('Fees: ', $fee, false);
}
}
add_action('woocommerce_cart_calculate_fees','coupon_add_cart_fee');
此代码测试,它的工作原理。它继承您活动的儿童主题或主题的function.php文件。
税选项与 add_fee() method
重要:税收工作的事实或
add_fee()
method不取决于第一的税金设置在woocommerce。Class WC_Cart add_fee()方法,为购物车增加了额外的费用。
add_fee(string $name, float $amount, boolean $taxable = false, string $tax_class = '' )> <!-- language: lang-css --> Parameters: $name Unique name for the fee. Multiple fees of the same name cannot be added. $amount Fee amount. $taxable (default: false) Is the fee taxable? $tax_class (default: '') The tax class for the fee if taxable. A blank string is standard tax class.
参考:
这看起来很棒,正是我所需要的,@LoicTheAztec。这可能是我缺少的一个更基本的PHP问题,但是我可以在'$ coupon_code'变量中存储一组优惠券代码,因为如果正在使用许多代码中的任何一个,则需要添加费用? – Cmagb
凭借单一优惠券价值完美测试。感谢您也帮助阵列。非常感谢您的帮助!这里肯定会学到很多东西。 – Cmagb