编程语言
首页 > 编程语言> > php – 在Woocommerce中添加货到付款方式(cod)的费用

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

作者:互联网

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

解决方法:

It is not possible for cart page as all payment methods are only available on Checkout page.

// 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文件中.测试和工作.

Other payment methods:

– For Bank wire you will use 'bacs'
– For Cheque you will use 'cheque'
– For Paypal you will use 'paypal'
– … / …

类似的答案:

> Add a custom fee for a specific payment gateway in Woocommerce
> Add a fee based on shipping method and payment method in Woocommerce
> Percentage discount based on user role and payment method in Woocommerce

标签:checkout,php,wordpress,woocommerce,payment-method
来源: https://codeday.me/bug/20190724/1524793.html