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() 方法的税务选项
重要提示: 使用
add_fee()方法,TAX 是否有效取决于 您在 woocommerce 中的税收设置。
类 WC_Cart add_fee() 方法,向购物车添加额外费用。
add_fee(string $name, float $amount, boolean $taxable u003d false, string $tax_class u003d '')> <!-- 语言: lang-css -->
参数:
$name 费用的唯一名称。不能添加多个同名费用。
$amount 费用金额。
$taxable (default: false) 费用是否应纳税?
$tax_class (default: '') 费用的税级(如果应纳税)。空白字符串是标准税级。
参考:
-
以编程方式将免税费用添加到 WooCommerce 购物车
-
类 WC\Cart - add_fee() 方法
-
类 WC_Cart - has_discount() 方法
更多推荐
所有评论(0)