在WooCommerce中,我试图找出如何在没有优惠券或促销代码应用于购物车时为每个订单添加“手续费”。
这是我的“费用”或“手续费”代码:
add_action( 'woocommerce_cart_calculate_fees','endo_handling_fee' ); function endo_handling_fee() { global $woocommerce; if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; $fee = 2.00; $woocommerce->cart->add_fee( 'Handling', $fee, true, 'standard' ); }有任何想法吗?
谢谢
In WooCommerce, I am trying to figured out how to add a "Handling Fee" to every order when no coupons or promo codes are applied to cart.
Here's my "Fee" or "Handling Charge" code:
add_action( 'woocommerce_cart_calculate_fees','endo_handling_fee' ); function endo_handling_fee() { global $woocommerce; if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; $fee = 2.00; $woocommerce->cart->add_fee( 'Handling', $fee, true, 'standard' ); }Any ideas?
Thanks
最满意答案
在这里,我获得了购物车应用优惠券的数量,如果没有优惠券应用于购物车,则购物车将收取费用。
这是代码:
add_action( 'woocommerce_cart_calculate_fees','conditional_handling_fee' ); function conditional_handling_fee() { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; // Get the applied coupons + the count (in cart) $applied_coupons_arr = WC()->cart->get_applied_coupons(); $applied_coupons_count = count($applied_coupons_arr); $fee = 2.00; if( 0 == $applied_coupons_count ) WC()->cart->add_fee( 'Handling - '.$applied_coupons_count, $fee, true, 'standard' ); }代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
此代码经过测试并可以使用。
参考: WC_Cart类 - get_applied_coupons()方法
Here I get the array of cart applied coupons and if there is no coupons applied to cart, then a fee is applied to cart.
Here is that code:
add_action( 'woocommerce_cart_calculate_fees','conditional_handling_fee' ); function conditional_handling_fee() { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; // Get the applied coupons + the count (in cart) $applied_coupons_arr = WC()->cart->get_applied_coupons(); $applied_coupons_count = count($applied_coupons_arr); $fee = 2.00; if( 0 == $applied_coupons_count ) WC()->cart->add_fee( 'Handling - '.$applied_coupons_count, $fee, true, 'standard' ); }Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.
Reference: WC_Cart class - get_applied_coupons() method
更多推荐
发布评论