如果购物车没有优惠券,则有条件地增加购物车费(Conditionally adding a cart fee if no coupons are applied to cart)

编程入门 行业动态 更新时间:2024-10-26 15:13:58
如果购物车没有优惠券,则有条件地增加购物车费(Conditionally adding a cart fee if no coupons are applied to cart)

在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

更多推荐

本文发布于:2023-07-28 12:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1305199.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:优惠券   车费   购物车   则有   条件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!