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

基于 Woocommerce 中自定義單選按鈕的動態運費

Dynamic shipping fee based on custom radio buttons in Woocommerce(基于 Woocommerce 中自定義單選按鈕的動態運費)
本文介紹了基于 Woocommerce 中自定義單選按鈕的動態運費的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在 Woocommerce 中,我在結帳頁面上添加了兩個自定義單選按鈕,并在單擊時調用了 ajax 函數來添加運費.

In Woocommerce, I have added two custom radio buttons on the checkout page and on click, I called an ajax function to add a delivery fee.

這是我的代碼:

$(document).on('change','#shipping_method_0_local_pickup5',function(e) {              
            $('.woocommerce-shipping-fields').css({
                'display': 'none'
            });

                        $("#deli").css("display","block"); 
                        var selected = $("input[type='radio'][name='post-del']:checked");
                        var selectedVal = selected.val();               
                        var pickurl= "<?php echo admin_url('admin-ajax.php');?>?action=delivery";
                        $.ajax({   
                            url: pickurl,
                            type: "POST",
                            data:{
                                input:selectedVal,                       
                            },            
                            success: function(responseText) 
                            { 
                                jQuery(".order-total .woocommerce-Price-amount").html(responseText);
                                //$(".discount_code").css("display","block"); 
                            }
                        }); 

        });

當單擊單選按鈕時,我想在我的總價中添加 2 美元.

add_action( 'wp_ajax_delivery', 'delivery' );
add_action( 'wp_ajax_nopriv_delivery', 'delivery' );

function delivery()
{      
    //My code  
    do_action( 'woocommerce_cart_calculate_fees', 'prefix_add_discount_line' ); // not working
    exit;
}

注意:這是更新代碼的鉤子

add_action( 'woocommerce_cart_calculate_fees', 'prefix_add_discount_line' );
function prefix_add_discount_line( $cart ) {

    $discount = $cart->subtotal + 2;

    $cart->add_fee( __( 'Delivery', 'yourtext-domain' ) , +$discount );

}

推薦答案

您應該在問題中提供所有必要的相關代碼.請記住,尋求調試幫助的問題(為什么此代碼不起作用?")必須包括所需的行為、特定問題或錯誤以及在問題本身中重現它所需的最短代碼".

You should give all necessary related code in your question. Remember that "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself".

因此,在下面的代碼中,您將找到一個完整的工作解決方案,帶有額外的自定義單選按鈕,它將根據所選的單選按鈕和本地取貨"運輸方式動態添加運費.

So, in the code below, you will find a complete working solution, with additional custom radio buttons, that will add dynamically a delivery fee depending on the selected radio button and for "local pickup" shipping method.

代碼(您需要在其中定義目標本地取貨"方法 ID):

// Enabling delivery options for a specific defined shipping method
function targeted_shipping_method(){
    // HERE below define the shipping method Id that enable the custom delivery options
    return 'local_pickup:5';
}

// Customizing Woocommerce checkout radio form field
add_action( 'woocommerce_form_field_radio', 'custom_form_field_radio', 20, 4 );
function custom_form_field_radio( $field, $key, $args, $value ) {
    if ( ! empty( $args['options'] ) && is_checkout() ) {
        $field = str_replace( '</label><input ', '</label><br><input ', $field );
        $field = str_replace( '<label ', '<label style="display:inline;margin-left:8px;" ', $field );
    }
    return $field;
}

// Add a custom radio fields for packaging selection
add_action( 'woocommerce_review_order_after_shipping', 'checkout_shipping_form_delivery_addition', 20 );
function checkout_shipping_form_delivery_addition(){
    $domain = 'wocommerce';

    if (  WC()->session->get( 'chosen_shipping_methods' )[0] == targeted_shipping_method() ) :

        echo '<tr class="delivery-radio"><th>' . __('Delivery options', $domain) . '</th><td>';

        $chosen = WC()->session->get('chosen_delivery');
        $chosen = empty($chosen) ? WC()->checkout->get_value('delivery') : $chosen;
        $chosen = empty($chosen) ? 'regular' : $chosen;

        // Add a custom checkbox field
        woocommerce_form_field( 'radio_delivery', array(
            'type' => 'radio',
            'class' => array( 'form-row-wide' ),
            'options' => array(
                'regular' => __('Regular', $domain),
                'premium' => __('Premium +'.wc_price(2.00), $domain),
            ),
            'default' => $chosen,
        ), $chosen );

        echo '</td></tr>';

    endif;
}

// jQuery - Ajax script
add_action( 'wp_footer', 'checkout_delivery_script' );
function checkout_delivery_script() {
    // Only checkout page
    if ( ! is_checkout() ) return;
    ?>
    <script type="text/javascript">
    jQuery( function($){
        if (typeof wc_checkout_params === 'undefined')
            return false;

        $('form.checkout').on('change', 'input[name=radio_delivery]', function(e){
            e.preventDefault();
            var d = $(this).val();
            $.ajax({
                type: 'POST',
                url: wc_checkout_params.ajax_url,
                data: {
                    'action': 'delivery',
                    'delivery': d,
                },
                success: function (result) {
                    $('body').trigger('update_checkout');
                    console.log(result); // just for testing | TO BE REMOVED
                },
                error: function(error){
                    console.log(error); // just for testing | TO BE REMOVED
                }
            });
        });
    });
    </script>
    <?php

}

// Get Ajax request and saving to WC session
add_action( 'wp_ajax_delivery', 'wc_get_delivery_ajax_data' );
add_action( 'wp_ajax_nopriv_delivery', 'wc_get_delivery_ajax_data' );
function wc_get_delivery_ajax_data() {
    if ( isset($_POST['delivery']) ){
        WC()->session->set('chosen_delivery', sanitize_key( $_POST['delivery'] ) );
        echo json_encode( $delivery ); // Return the value to jQuery
    }
    die();
}

// Add a custom dynamic delivery fee
add_action( 'woocommerce_cart_calculate_fees', 'add_packaging_fee', 20, 1 );
function add_packaging_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Only for targeted shipping method
    if (  WC()->session->get( 'chosen_shipping_methods' )[0] != targeted_shipping_method() )
        return;

    if( WC()->session->get( 'chosen_delivery' ) == 'premium' )
        $cart->add_fee( __( 'Delivery fee', 'woocommerce' ), 2.00 );
}

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

Code goes in function.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 字節))
主站蜘蛛池模板: 一区二区三区日本 | 午夜a级理论片915影院 | 日本a v在线播放 | 视频三区 | 在线视频亚洲 | 国产精品免费视频一区 | 亚洲黄色网址视频 | 欧美精品在欧美一区二区少妇 | 国产一区二区三区在线 | 亚洲激情综合 | 女生羞羞网站 | 99久久精品免费看国产免费软件 | 成人国产精品入口免费视频 | 欧美一级α片 | 欧美日韩在线观看一区 | 91精品久久久久久久久久 | 亚洲九九 | 亚洲在线免费观看 | 精品国产99 | 国产精品99999999 | 日韩中文字幕在线播放 | heyzo在线| 一二三区av | 天天澡天天狠天天天做 | 中文字幕 国产 | 久热精品免费 | 在线看无码的免费网站 | 欧亚av在线 | 国产精品国产a级 | 国产综合第一页 | 成人午夜精品一区二区三区 | 国产午夜精品视频 | 老牛嫩草一区二区三区av | 日韩欧美在线不卡 | 日韩在线h| 97色在线视频 | 国产重口老太伦 | 久久久精品一区二区三区 | 欧美爱爱视频 | 九九色综合| 成人国产精品久久 |