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

根據 Woocommerce 中選擇的付款方式禁用運輸方式

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

問題描述

如果用戶選擇貨到付款"付款,我需要禁用特定的運輸方式.問題是以下代碼僅在我重置時才有效WooCommerce 每次都會瞬變并刷新.它不適用于來回用戶選擇.

I need to disable specific shipping method if user selected payment "Cash on Delivery". The problem is that the following code works only if I reset WooCommerce transients each time and refresh. It doesn't work on user selection back and forth.

add_filter( 'woocommerce_package_rates', 'alter_shipping_methods', 100 );
function alter_shipping_methods( $rates ) {

    $chosen_gateway = WC()->session->chosen_payment_method;

    // If payment is Cash on delivery remove specific shipping 
    if($chosen_gateway == 'cod') {

        foreach ( $rates as $rate_id => $rate ) {
           if ( $rate->label === 'Hrvatska po?ta' ) {
              unset( $rates[ $rate_id ] );
            }
       }

    }

    return $rates;

}

我確實有這個應該觸發的代碼,當我點擊選項時,我會在控制臺中看到輸出.

I do have this code which should trigger and I see the output in console when I click around options.

jQuery(document.body).on('change', 'input[name="payment_method"]', function() {
    console.log('Payment method changed');
    jQuery('body').trigger('update_checkout');
});

我試過了,沒用

function action_woocommerce_checkout_update_order_review($array, $int) {
    WC()->cart->calculate_shipping();
    return;
}
add_action('woocommerce_checkout_update_order_review', 'action_woocommerce_checkout_update_order_review', 10, 2);

而且我也嘗試過調用 PHP 函數的自定義 AJAX 調用,在此過濾器中,沒有結果

And I have also tried custom AJAX call which calls a PHP function and inside this filter, no result

add_filter( 'woocommerce_package_rates', 'alter_shipping_methods', 100 );

接下來我應該嘗試什么?

What should I try next?

推薦答案

2019 年 3 月更新

對于 COD 支付網關,您只需在設置中添加統一費率"即可.您要為其啟用的運輸方式,例如:

For COD payment gateways, you can just add in its settings the "Flat rate" shipping methods that you want to enable for it, like:

對于 Cod 和其他方法或其他支付網關,這里是禁用特定支付網關的特定運輸方式的完整工作方式.

For Cod and other methods or for others payment gateways, here is the complete working way to disable a specific shipping method(s) for specific payment gateway(s).

您必須在第一個函數中設置要隱藏的運輸方式 ID.

You will have to set in the first function the shipping method Id that you wish to hide.

代碼:

add_action( 'woocommerce_package_rates','show_hide_shipping_methods', 10, 2 );
function show_hide_shipping_methods( $rates, $package ) {
    // HERE Define your targeted shipping method ID
    $payment_method        = 'cod';

    $chosen_payment_method = WC()->session->get('chosen_payment_method');

    if( $payment_method == $chosen_payment_method ){
        unset($rates['flat_rate:12']);
    }
    return $rates;
}

add_action( 'woocommerce_review_order_before_payment', 'payment_methods_trigger_update_checkout' );
function payment_methods_trigger_update_checkout(){
    // jQuery code
    ?>
    <script type="text/javascript">
        (function($){
            $( 'form.checkout' ).on( 'change blur', 'input[name^="payment_method"]', function() {
                setTimeout(function(){
                    $(document.body).trigger('update_checkout');
                }, 250 );
            });
        })(jQuery);
    </script>
    <?php
}

add_action( 'woocommerce_checkout_update_order_review', 'refresh_shipping_methods' );
function refresh_shipping_methods( $post_data ){
    // HERE Define your targeted shipping method ID
    $payment_method = 'cod';
    $bool           = true;

    if ( WC()->session->get('chosen_payment_method') === $payment_method )
        $bool = false;

    // Mandatory to make it work with shipping methods
    foreach ( WC()->cart->get_shipping_packages() as $package_key => $package ){
        WC()->session->set( 'shipping_for_package_' . $package_key, $bool );
    }
    WC()->cart->calculate_shipping();
}

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

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

為了能夠獲得正確的送貨方式 ID,您可以使用瀏覽器檢查器,如下所示:

To be able to get the correct shipping method ID you can use your browser inspector, this way:

在測試此代碼之前,您可能需要清空購物車.

You may need to empty cart before testing this code.

這篇關于根據 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 字節))
主站蜘蛛池模板: 日本免费一区二区三区四区 | 日本中文字幕在线观看 | 日韩精品极品视频在线观看免费 | 一区二区在线观看av | 一区二区三区视频 | 成人不卡| 一区二区三区在线 | 国产不卡一区 | 亚洲深夜福利 | 国产成人精品一区二区三区四区 | 青草青草久热精品视频在线观看 | 国产一二区在线 | 天天狠狠 | 成人做爰69片免费观看 | 男女国产网站 | 女同videos另类| 国产一区二区三区免费观看在线 | 亚洲成年人免费网站 | 久久久久电影 | 国产精品久久久久一区二区三区 | 男女免费网站 | 99精品欧美一区二区蜜桃免费 | 91夜色在线观看 | 国产一级在线观看 | 亚洲一区自拍 | 黄网免费 | 羞羞视频网站免费观看 | 亚洲一区视频 | 成人在线观看免费视频 | 久久久久久国产精品mv | 视频1区 | 久久国产精品精品 | 亚洲国产一区二区三区 | 亚洲精品一区二区三区 | 日韩精品一区二区三区免费视频 | 色综合色综合网色综合 | 久久精品国产一区二区电影 | 99国产精品99久久久久久粉嫩 | 色接久久 | 老熟女毛片 | 日日日干干干 |