首页 文章

在Woocommerce中添加货到付款方式(cod)的费用

提问于
浏览
4

在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插件,但我负担不起 .

3 回答

  • 1

    购物车页面无法使用,因为所有付款方式仅适用于结帐页面 .

    // Add a custom fee based o cart subtotal
    add_action( 'woocommerce_cart_calculate_fees', 'custom_handling_fee', 10, 1 );
    function custom_handling_fee ( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        if ( 'cod' === WC()->session->get('chosen_payment_method') ) {
            $fee = 5;
            $cart->add_fee( 'Handling', $fee, true );
        }
    }
    

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

    // jQuery - Update checkout on methode payment change
    add_action( 'wp_footer', 'custom_checkout_jqscript' );
    function custom_checkout_jqscript() {
        if ( is_checkout() && ! is_wc_endpoint_url() ) :
        ?>
        <script type="text/javascript">
        jQuery( function($){
            $('form.checkout').on('change', 'input[name="payment_method"]', function(){
                $(document.body).trigger('update_checkout');
            });
        });
        </script>
        <?php
        endif;
    }
    

    代码位于活动子主题(或活动主题)的function.php文件中 . 测试和工作 .

    其他付款方式: - 对于银行电汇,您将使用'bacs' - 对于支票,您将使用'支票' - 对于Paypal,您将使用'paypal' - ... / ...


    类似的答案:

  • 8
    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, '' );
        }
    
    
    }
    
  • 0

    对于其他任何想要这样做的人,我想为银行转账(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);
    }
    

相关问题