【问题标题】:Add a fee based on shipping method and payment method in Woocommerce根据 Woocommerce 中的运输方式和付款方式添加费用
【发布时间】:2019-02-07 20:43:26
【问题描述】:

当客户可以下单免运费但想选择货到付款时,我需要支付额外费用。 所以,免运费 + 货到付款 => 费用。

我尝试了以下代码,但没有成功。我哪里错了?

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

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

        $chosen_gateway = WC()->session->chosen_payment_method;
        $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
        $chosen_shipping = $chosen_methods[0]; 
        $fee = 19;
        if ( $chosen_shipping == 'free_shipping' && $chosen_gateway == 'cod' ) { 
        WC()->cart->add_fee( 'Spese per pagamento alla consegna', $fee, false, '' );
    }
}

【问题讨论】:

    标签: php jquery wordpress woocommerce checkout


    【解决方案1】:

    您的代码有错误,需要一些额外的代码。 Try the following code that will add a specific fee when chosen payment method is Cash on delivery (cod) and when chosen shipping methods is "Free shipping":

    // Add a conditional fee
    add_action( 'woocommerce_cart_calculate_fees', 'add_cod_fee', 20, 1 );
    function add_cod_fee( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        ## ------ Your Settings (below) ------ ##
        $your_payment_id      = 'cod'; // The payment method
        $your_shipping_method = 'free_shipping'; // The shipping method
        $fee_amount           = 19; // The fee amount
        ## ----------------------------------- ##
    
        $chosen_payment_method_id  = WC()->session->get( 'chosen_payment_method' );
        $chosen_shipping_method_id = WC()->session->get( 'chosen_shipping_methods' )[0];
        $chosen_shipping_method    = explode( ':', $chosen_shipping_method_id )[0];
    
        if ( $chosen_shipping_method == $your_shipping_method 
        && $chosen_payment_method_id == $your_payment_id ) {
            $fee_text = __( "Spese per pagamento alla consegna", "woocommerce" );
            $cart->add_fee( $fee_text, $fee_amount, false );
        }
    }
    
    // Refresh checkout on payment method change
    add_action( 'wp_footer', 'refresh_checkout_script' );
    function refresh_checkout_script() {
        // Only on checkout page
        if( is_checkout() && ! is_wc_endpoint_url('order-received') ) :
        ?>
        <script type="text/javascript">
        jQuery(function($){
            // On payment method change
            $('form.woocommerce-checkout').on( 'change', 'input[name="payment_method"]', function(){
                // Refresh checkout
                $('body').trigger('update_checkout');
            });
        })
        </script>
        <?php
        endif;
    }
    

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

    【讨论】:

    • LoicTheAztec,因为我尝试了这段代码,它工作得很好,但是一旦订单支付失败并且用户再次尝试从订单历史记录中支付,则不会添加任何 cod 费用
    • @ParthShah 是的,这是正常的,因为此代码不处理这种情况,因为此处仅在购物车对象上的结帐页面中使用了挂钩。如果订单失败,以订单支付的购物车对象不再存在。所以在这种情况下,店长需要编辑订单并添加费用。
    • 那么我该如何添加你能帮助它..任何钩子或条件?
    • 但是最终付款发送给用户的邮件,所以我们无法从后端更改金额,没有可用的挂钩??
    • 我不知道……我的回答只是现有非工作代码的工作更改版本。所以这个答案只是修补现有代码以使其工作。你要问的是完全不同的东西,没有任何提供的代码。
    猜你喜欢
    • 2016-11-17
    • 2018-10-28
    • 2017-12-06
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    • 1970-01-01
    相关资源
    最近更新 更多