【问题标题】:Add fee based on specific payment methods in WooCommerce根据 WooCommerce 中的特定付款方式添加费用
【发布时间】:2016-11-17 20:20:26
【问题描述】:

在 WooCommerce 中,我需要为特定支付网关应用自定义手续费。我从这里有这段代码:How to Add Handling Fee to WooCommerce Checkout

这是我的代码:

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

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

        $fee = 5.00;
    $woocommerce->cart->add_fee( 'Handling', $fee, true, 'standard' );
}

此功能为所有交易添加费用。

是否可以调整此功能并使其仅适用于特定的付款方式?

另一个问题是我希望将此费用应用于购物车。有可能吗?

我也欢迎任何替代方法。我知道类似的“基于支付网关的费用”woo 插件,但我买不起。

【问题讨论】:

    标签: php wordpress woocommerce checkout payment-method


    【解决方案1】:

    2021 年更新

    注意:所有付款方式仅在结帐页面上可用。

    以下代码将根据选择的付款方式有条件地添加特定费用:

    // Add a custom fee (fixed or based cart subtotal percentage) by payment
    add_action( 'woocommerce_cart_calculate_fees', 'custom_handling_fee' );
    function custom_handling_fee ( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        $chosen_payment_id = WC()->session->get('chosen_payment_method');
    
        if ( empty( $chosen_payment_id ) )
            return;
    
        $subtotal = $cart->subtotal;
    
        // SETTINGS: Here set in the array the (payment Id) / (fee cost) pairs
        $targeted_payment_ids = array(
            'cod' => 8, // Fixed fee
            'paypal' => 5 * $subtotal / 100, // Percentage fee
        );
    
        // Loop through defined payment Ids array
        foreach ( $targeted_payment_ids as $payment_id => $fee_cost ) {
            if ( $chosen_payment_id === $payment_id ) {
                $cart->add_fee( __('Handling fee', 'woocommerce'), $fee_cost, true );
            }
        }
    }
    

    您将需要以下内容来刷新付款方式更改的结帐,以使其正常工作:

    // jQuery - Update checkout on payment method change
    add_action( 'woocommerce_checkout_init', 'payment_methods_refresh_checkout' );
    function payment_methods_refresh_checkout() {
        wc_enqueue_js( "jQuery( function($){
            $('form.checkout').on('change', 'input[name=payment_method]', function(){
                $(document.body).trigger('update_checkout');
            });
        });");
    }
    

    代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且有效。

    如何在 WooCommerce Checkout 页面中找到特定的付款方式 ID?

    以下将在结帐付款方式上显示仅供管理员使用的付款 ID:

    add_filter( 'woocommerce_gateway_title', 'display_payment_method_id_for_admins_on_checkout', 100, 2 );
    function display_payment_method_id_for_admins_on_checkout( $title, $payment_id ){
      if( is_checkout() && ( current_user_can( 'administrator') || current_user_can( 'shop_manager') ) ) {
          $title .= ' <code style="border:solid 1px #ccc;padding:2px 5px;color:red;">' . $payment_id . '</code>';
      }
      return $title;
    }
    

    代码在您的活动子主题(或活动主题)的functions.php 文件中。使用后,将其移除。


    类似的答案:

    【讨论】:

    • 已使用相关链接编辑答案。它现在指向一个解决方案,赞成
    【解决方案2】:

    首先我们需要了解一些关键的事情:

    1. 我们将使用唯一的过滤器钩子woocommerce_cart_calculate_fees
    2. 为了获得用户选择的付款方式,我们必须使用此方法从用户会话中检索它WC()-&gt;session-&gt;get( 'chosen_payment_method' )
    3. calculate_fees()calculate_totals() 方法不是必需的。
    4. 我们将使用购物车方法 WC()-&gt;cart-&gt;add_fee() 添加费用,该方法接受两个参数 - 第一个是费用说明,第二个是费用金额。
    5. 还有一件事——我们需要一个支付方式slug,这是本教程中描述的最简单的方法https://rudrastyh.com/woocommerce/add-id-column-to-payment-methods-table.html

    我们现在走吧:

    add_action( 'woocommerce_cart_calculate_fees', 'rudr_paypal_fee', 25 );
    function rudr_paypal_fee() {
    
        if( 'paypal' == WC()->session->get( 'chosen_payment_method' ) ) {
            WC()->cart->add_fee( 'PayPal fee', 2 ); // let's add a fee for paypal
        }
    
    }
    

    每次更改支付网关时刷新结帐也很棒,使用此代码很容易做到:

    jQuery( function( $ ) {
        $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
            $( 'body' ).trigger( 'update_checkout' );
        });
    });
    

    就是这样。此处也详细描述了所有内容:https://rudrastyh.com/woocommerce/charge-additional-fees-based-on-payment-gateway.html

    【讨论】:

      【解决方案3】:

      对于其他希望这样做的人,我想为银行转账 (BACS) 添加费用,这是我使用的方法:

      //Hook the order creation since it is called during the checkout process:
      add_filter('woocommerce_create_order', 'my_handle_bacs', 10, 2);
      
      function my_handle_bacs($order_id, $checkout){
      //Get the payment method from the $_POST
          $payment_method = isset( $_POST['payment_method'] ) ? wc_clean( $_POST['payment_method'] ) : '';
      
      //Make sure it's the right payment method
          if($payment_method == "bacs"){
      
              //Use the cart API to add recalculate fees and totals, and hook the action to add our fee
              add_action('woocommerce_cart_calculate_fees', 'my_add_bacs_fee');
              WC()->cart->calculate_fees();
              WC()->cart->calculate_totals();
          }
      
          //This filter is for creating your own orders, we don't want to do that so return the $order_id untouched
          return $order_id;
      }
      function my_add_bacs_fee($cart){
          //Add the appropriate fee to the cart
          $cart->add_fee("Bank Transfer Fee", 40);
      }
      

      【讨论】:

        【解决方案4】:
        add_action( 'woocommerce_cart_calculate_fees','cod_fee' );
        function cod_fee() {
            global $woocommerce;
        
            if ( is_admin() && ! defined( 'DOING_AJAX' ) )
                return;
                // get your payment method
                $chosen_gateway = WC()->session->chosen_payment_method;
                //echo $chosen_gateway;
                $fee = 5;
                if ( $chosen_gateway == 'cod' ) { //test with cash on delivery method
                WC()->cart->add_fee( 'delivery fee', $fee, false, '' );
            }
        
        
        }
        

        【讨论】:

          猜你喜欢
          • 2019-02-07
          • 1970-01-01
          • 2011-08-02
          • 2019-07-26
          • 2019-01-16
          • 2021-05-22
          • 2018-06-05
          • 2014-03-16
          • 2014-08-06
          相关资源
          最近更新 更多