在 Woocommerce 中添加费用之前检查应用的优惠券

Check applied coupons before adding a fee in Woocommerce

我正在使用一些代码在 Woocommerce 结账时添加自定义费用,但是在使用优惠券时它不会扣除费用。

有没有办法在加费前检查优惠券是否被使用过?那是任何优惠券,而不是特定的优惠券。

到目前为止的代码是

global $woocommerce;

if (empty($woocommerce->cart->applied_coupons))  {

    add_action( 'woocommerce_cart_calculate_fees','endo_handling_fee' );
    function endo_handling_fee() {
         global $woocommerce;

         if ( is_admin() && ! defined( 'DOING_AJAX' ) )
              return;

         $fee = 1.50;
         $woocommerce->cart->add_fee( 'Transaction Fee', $fee, true, 'standard' );
    }

}

所以你可以看到我正在尝试为优惠券添加一个 if 语句,但是这会显示是否有优惠券的费用。所以我被困住了,非常感谢任何帮助!

谢谢

您的代码有点过时,您需要在函数中添加 If 语句:

add_action( 'woocommerce_cart_calculate_fees','endo_handling_fee', 10, 1 );
function endo_handling_fee( $cart ) {
     if ( is_admin() && ! defined( 'DOING_AJAX' ) )
          return;

     if ( sizeof( $cart->get_applied_coupons() ) > 0 )
          return;

     $fee = 1.50;
     $cart->add_fee( __("Transaction Fee"), $fee, true );
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。

Note: Coupon discounts are applied only on cart items. They don't care about Fees.