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

如果在 WooCommerce Checkout 中選中自定義復選框,則

Remove shipping cost if custom checkbox is checked in WooCommerce Checkout(如果在 WooCommerce Checkout 中選中自定義復選框,則刪除運費)
本文介紹了如果在 WooCommerce Checkout 中選中自定義復選框,則刪除運費的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

如果選中結帳字段中的復選框,我將嘗試將運費價格設置為 0.00 美元.

I am trying to set the price of shipping rates to $0.00 if a checkbox in the checkout fields is checked.

當前嘗試:

function no_shipping_for_own_ups($rates,$package) {
    foreach ($rates as $rate) {
        //Set the price
        $rate->cost = 0;
    }
    return $rates;
}

function woo_add_cart_ups_y_n_fee( $cart ){
    if ( ! $_POST || ( is_admin() && ! is_ajax() ) ) {
        return;
    }

    if ( isset( $_POST['post_data'] ) ) {
        parse_str( $_POST['post_data'], $post_data );
    } else {
        $post_data = $_POST;
    }

    if (isset($post_data['billing_ups_yn'])) {
        add_filter( 'woocommerce_package_rates','no_shipping_for_own_ups', 99, 2 );
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_ups_y_n_fee', 43, 1 );

As $post_data['billing_ups_yn'] 是我設置的復選框,用于在觸發時更新結帳.

As $post_data['billing_ups_yn'] being the checkbox that I set to update the checkout when triggered.

只需選中復選框,然后應用過濾器將運費設置為 ??0.
但是,這不起作用.

Simply if the checkbox is checked, then apply the filter to set shipping rates to 0.
This, however, isn't working.

推薦答案

通過 Ajax 在結帳頁面中的自定義 javascript 事件上更改運輸方式成本比有條件地更改費用或其他任何事情要復雜得多,如下所示.

To alter shipping methods cost via Ajax on a custom javascript event in checkout page is quiet more complicated than conditionally altering fees or anything else as you will see below.

為了存儲 Ajax 數據,我使用 WC_Sessions 并改變運輸方式成本只有在您操縱運輸會話數據

To store the Ajax data I use WC_Sessions and to alter Shipping methods costs will only work if you manipulate the shipping sessions data

完整的工作代碼:

// Add a Custom checkbox field for shipping options (just for testing)
add_action( 'woocommerce_after_checkout_billing_form', 'custom_billing_checkbox_for_testing', 10, 1 );
function custom_billing_checkbox_for_testing( $checkout ) {
    $field_id = 'billing_ups_yn';

    // Get the checked state if exist
    $billing_ups = WC()->session->get('billing_ups' );
    if(empty($billing_ups))
        $billing_ups = $checkout->get_value( $field_id );

    // Add the custom checkout field (checkbox)
    woocommerce_form_field( $field_id, array(
        'type' => 'checkbox',
        'class' => array( 'form-row-wide' ),
        'label' => __('Billing UPS'),
    ), $billing_ups );
}

// function that gets the Ajax data
add_action( 'wp_ajax_woo_get_ajax_data', 'woo_get_ajax_data' );
add_action( 'wp_ajax_nopriv_woo_get_ajax_data', 'woo_get_ajax_data' );
function woo_get_ajax_data() {
    if ( $_POST['billing_ups'] == '1' ){
        WC()->session->set('billing_ups', '1' );
    } else {
        WC()->session->set('billing_ups', '0' );
    }
    echo json_encode( WC()->session->get('billing_ups' ) );
    die(); // Alway at the end (to avoid server error 500)
}

// Conditionally changing the shipping methods costs
add_filter( 'woocommerce_package_rates','conditional_custom_shipping_cost', 90, 2 );
function conditional_custom_shipping_cost( $rates, $package ) {

    if ( WC()->session->get('billing_ups' ) == '1' ){
        foreach ( $rates as $rate_key => $rate_values ) {
            // Not for "Free Shipping method" (all others only)
            if ( 'free_shipping' !== $rate_values->method_id ) {

                // Set the rate cost
                $rates[$rate_key]->cost = 0;

                // Set taxes rate cost (if enabled)
                $taxes = array();
                foreach ($rates[$rate_key]->taxes as $key => $tax)
                    if( $rates[$rate_key]->taxes[$key] > 0 ) // set the new tax cost
                        $taxes[$key] = 0;
                $rates[$rate_key]->taxes = $taxes;
            }
        }
    }
    return $rates;
}

// Enabling, disabling and refreshing session shipping methods data
add_action( 'woocommerce_checkout_update_order_review', 'refresh_shipping_methods', 10, 1 );
function refresh_shipping_methods( $post_data ){
    $bool = true;
    if ( WC()->session->get('billing_ups' ) == '1' ) $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();
}

// The Jquery script
add_action( 'wp_footer', 'custom_checkout_script' );
function custom_checkout_script() {
    ?>
    <script type="text/javascript">
        jQuery( function($){

            // update cart on delivery location checkbox option
            $('#billing_ups_yn_field input').change( function () {
                var checked = 0;
                if ( $('#billing_ups_yn').is(':checked') )
                    checked = 1;

                $.ajax({
                    type: 'POST',
                    url: wc_checkout_params.ajax_url,
                    data: {
                        'action': 'woo_get_ajax_data',
                        'billing_ups': checked,
                    },
                    success: function (result) {
                        $('body').trigger('update_checkout');
                        console.log('response: '+result); // just for testing
                    },
                    error: function(error){
                        console.log(error); // just for testing
                    }
                });
            });
        });
    </script>
    <?php
}

代碼位于活動子主題(或活動主題)的 function.php 文件中.

經過測試并有效.

與您的評論相關的更新:

如果您希望在加載時始終默認取消選中復選框,您將用這個函數替換第一個函數:

If you wish to have always the checkbox unchecked by default at load you will replace the first function by this one:

// Add a Custom checkbox field for shipping options (just for testing)
add_action( 'woocommerce_after_checkout_billing_form', 'custom_billing_checkbox_for_testing', 10, 1 );
function custom_billing_checkbox_for_testing( $checkout ) {
    $field_id = 'billing_ups_yn';

    // Add the custom checkout field (checkbox)
    woocommerce_form_field( $field_id, array(
        'type' => 'checkbox',
        'class' => array( 'form-row-wide' ),
        'label' => __('Billing UPS'),
    ), '' );
}

這篇關于如果在 WooCommerce Checkout 中選中自定義復選框,則刪除運費的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 字節))
主站蜘蛛池模板: 国产成人精品区一区二区不卡 | 欧美综合在线观看 | 欧美性另类 | 国产精品一区二区在线播放 | 超碰免费观看 | 久久这里只有精品首页 | 91av视频| japan25hdxxxx日本 做a的各种视频 | 啪一啪在线视频 | 色偷偷888欧美精品久久久 | 国产精品视频久久久久 | 久久亚洲国产 | 国产又爽又黄的视频 | 天天看逼 | 国产精品美女久久久久aⅴ国产馆 | 一区中文字幕 | 91精品国模一区二区三区 | 亚洲视频中文字幕 | 精品久久一区 | 超碰97免费在线 | 91在线一区二区 | 国产精品免费在线 | 一级一级一级毛片 | 精品视频一区二区三区 | 欧美日韩一区二区三区在线观看 | xx视频在线| 在线免费观看色 | 久久精品欧美视频 | 国产乱码高清区二区三区在线 | 亚洲国产精品精华素 | 天堂在线免费视频 | 成人午夜影院 | 成人影院一区二区三区 | 五月网婷婷 | 国产成人精品av | 日本不卡视频在线播放 | 国产精品久久久久久久7777 | 日韩欧美在 | 97精品国产97久久久久久免费 | 精品久久久久久红码专区 | 成人在线免费观看av |