久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Woocommerce 中根據運輸方式和付款方式添加費用

Add a fee based on shipping method and payment method in Woocommerce(在 Woocommerce 中根據運輸方式和付款方式添加費用)
本文介紹了在 Woocommerce 中根據運輸方式和付款方式添加費用的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

當客戶可以下訂單免運費,但想選擇貨到付款時,我需要申請額外費用.所以,免費送貨 + COD 付款 => 費用.

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

推薦答案

您的代碼中有錯誤,需要一些額外的代碼.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":

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;
}

代碼位于活動子主題(或活動主題)的 functions.php 文件中.經測試有效.

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

這篇關于在 Woocommerce 中根據運輸方式和付款方式添加費用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作為類名,因為它是保留的 Cake 2.2.x)
Session is lost after an OAuth redirect(OAuth 重定向后會話丟失)
Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分頁排序)
CakePHP Shared core for multiple apps(CakePHP 多個應用程序的共享核心)
Login [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登錄 [ Auth-identify() ] 始終為 false)
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命錯誤:允許的內存大小為 134217728 字節已用盡(嘗試分配 87 字節))
主站蜘蛛池模板: 久久精品小短片 | 午夜资源 | 国产精品久久久久一区二区三区 | 日韩久草| 国产精品一区二区电影 | 在线观看免费福利 | 午夜三级视频 | 91爱爱·com| 日韩在线免费视频 | 夜夜艹 | 偷拍亚洲色图 | 亚洲精品18 | 黑人粗黑大躁护士 | 欧美精品一区三区 | 亚洲精品在线国产 | 国产欧美日韩久久久 | 91欧美精品成人综合在线观看 | 97国产精品视频 | 欧美综合久久久 | 久久乐国产精品 | 在线免费国产视频 | 成人免费网站www网站高清 | 亚洲iv一区二区三区 | 亚洲一本 | 美女在线一区二区 | 成人精品国产一区二区4080 | 日韩和的一区二区 | 国产视频久久 | 欧美久久视频 | 成人午夜精品 | 亚洲精品国产综合区久久久久久久 | 亚洲一区视频在线播放 | 免费日韩av网站 | 国产精品综合视频 | 欧美激情精品久久久久 | 欧美精品中文字幕久久二区 | 91久久精品国产91久久性色tv | 久久久一区二区三区四区 | 国产成人精品久久久 | 亚洲毛片在线 | 亚洲欧美日韩在线 |