dpjj4763 2018-09-02 16:10
浏览 345
已采纳

根据Woocommerce中的送货方式和付款方式添加费用

I need to apply an additional fee when a customer can place an order with free shipping, but wants to select COD payment. So, Free Shipping + COD payment => fee.

I tried unsuccessfully the following piece of code. Where am I wrong?

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, '' );
    }
}
  • 写回答

1条回答 默认 最新

  • dragon8899 2018-09-02 16:53
    关注

    There is a mistake in your code and some additional code is needed. 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;
    }
    

    Code goes in functions.php file of your active child theme (or active theme). tested and works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计