WooCommerce-制作一组优惠券,向订单添加固定费用 [英] WooCommerce - Make a set of coupons adding a fixed fee to an order
问题描述
客户的要求很奇怪:他们希望使用一组优惠券代码向订单添加费用.这可能吗?
A client has a very odd request: they would like for a set of coupon codes to add a fee to an the order. Is this possible?
他们试图解决的问题是:
The problem they are trying to solve is this:
目前,该商店可免费送货至美国48个较低的国家/地区,为此我已正确配置了运输等级和餐桌费率.但是,客户在日常交易网站上出售的优惠券应该会消除免费送货的好处,并增加固定的送货费用.
Currently the store offers free shipping to the lower 48 United States, for which I have shipping classes and table rates properly configured. However, the coupons the customer has sold on a daily deal site are supposed to remove the free shipping benefit and add a fixed shipping fee.
如何实现呢?
推荐答案
是的,您可以使用该代码段代码(针对一组优惠券)进行操作:
Yes you can do it with that snippet code (for a set of coupons):
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()方法的税收选项
IMPORTANT: The fact that TAX is working or not with add_fee()
method depends first of your tax settings in 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.
参考:
- Add tax free fees to WooCommerce cart programmatically
- Class WC_Cart - add_fee() method
- Class WC_Cart - has_discount() method
这篇关于WooCommerce-制作一组优惠券,向订单添加固定费用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!