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

Gia*_*las 4 php wordpress checkout woocommerce payment-method

在WooCommerce中,我需要为特定支付网关应用自定义处理费.我从这里得到这段代码:如何向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' );
}
Run Code Online (Sandbox Code Playgroud)

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

是否可以调整此功能并使其仅适用于货到付款?

另一个问题是我希望这笔费用适用于购物车.可能吗?

我也欢迎任何替代方法.我知道类似的"支付网关费用"woo插件,但我负担不起.

Loi*_*tec 11

由于所有付款方式仅在结帐页面上提供,因此购物车页面无法使用.

// 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 );
    }
}
Run Code Online (Sandbox Code Playgroud)

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

// 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;
}
Run Code Online (Sandbox Code Playgroud)

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

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


类似的答案: