Remove shipping cost if custom checkbox is checked in WooCommerce Checkout

后端 未结 2 1825
心在旅途
心在旅途 2020-12-10 09:21

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

Current Attempt:

function no_shipping_for_ow         


        
2条回答
  •  被撕碎了的回忆
    2020-12-10 10:00

    I have similar project about 3 years ago and has radio button and date picker that will both modify and update the shipping details/amount when changed, the radio button will modify Shipping surcharge and the date picker will have -10 discount price when Saturday and Friday is selected from the calendar.

    working example here

    I assume you had knowledge with PHP and AJax and can modify this, you only need to modify few things like the session value and calculation as well as those jquery selector and form fields selector

    function to handle ajax request, the purpose is to only assign $_SESSION value

    add_action('wp_ajax_woo_modify_charges', 'etq_calculate', 10);
    add_action('wp_ajax_nopriv_woo_modify_charges', 'etq_calculate', 10);
    function etq_calculate() {
        global $woocommerce;
        $charge = isset($_POST['billing_area']) ? $_POST['billing_area'] : 'no';
        $deldate = isset($_POST['jck_delivery_date']) ? $_POST['jck_delivery_date'] : '06/08/2015';
        $delday = strtotime(str_replace('/', '-', $deldate));
        $day = date('l', $delday);
        if ( $day === 'Saturday' || $day === 'Friday') $val = -10;
        else $val = 0;
    
        if ( $charge === 'no' ) $surchage = 0;
        else $surchage = 10; 
    
        if( !isset($_SESSION) ) session_start();
        $_SESSION['val'] = $val;
        $_SESSION['surcharge'] = $surchage;
    }
    

    Next function is to hook the modification on woocommerce_cart_calculate_fees from the value of $_SESSION.. this is where the price update happens

    add_action('woocommerce_cart_calculate_fees', 'woo_change_cart_fee');
    function woo_change_cart_fee() {
        if(!isset($_SESSION)) session_start();
        $surcharge = isset($_SESSION['surcharge']) ? $_SESSION['surcharge'] : 0;
        $discount = isset($_SESSION['val']) ? $_SESSION['val'] : 0;
    
        if ( $surcharge !== '0' ) WC()->cart->add_fee('Shipping Surchage', $surcharge, false, '');
        if ( $discount !== '0' ) WC()->cart->add_fee('Delivery Discount', $discount, false, '');
    }
    

    then for jquery script that handles ajax

    (function($){
        'use strict';
        $(document).ready( function() { 
    
            var $date_field = $('#jck_delivery_date');
    
            // update cart on delivery date changes
            $('#jck_delivery_date').bind('change', function () {
                    if ($('#billing_area_yes').is(':checked')) {
                        var surcharge = $('#billing_area_yes').val();
                        $('#billing_area_yes').prop('checked', true);
                    } else {
                        var surcharge = 'no';
                    }
    
                    var deldate = $('#jck_delivery_date').val();
                    var data = {
                        action: 'woo_modify_charges',
                        jck_delivery_date: deldate,
                        billing_area: surcharge
                    };
    
                    $.ajax({
                        type: 'POST',
                        url: wc_checkout_params.ajax_url,
                        data: data,
                        success: function (code) {
                            console.log(code);
                            //jQuery('.woocommerce-error, .woocommerce-message').remove();
    
                            if (code === '0') {
                                //$form.before(code);
                                $('body').trigger('update_checkout');
                            }
                        },
                        dataType: 'html'
                    });
    
                    return false;
            });
    
            // update cart on delivery location checkbox option
            $('#billing_area_field input').change( function () {
                if ($('#billing_area_yes').is(':checked')) {
                    var surcharge = $('#billing_area_yes').val();
                } else {
                    var surcharge = 'no';
                }
                var deldate = $('#jck_delivery_date').val();
                console.log( surcharge );
                var data = {
                    action: 'woo_modify_charges',
                    billing_area: surcharge,
                    jck_delivery_date: deldate,
                };
    
                $.ajax({
                    type: 'POST',
                    url: wc_checkout_params.ajax_url,
                    data: data,
                    success: function (code) {
                        console.log(code);
                        //jQuery('.woocommerce-error, .woocommerce-message').remove();
    
                        if (code === '0') {
                            //$form.before(code);
                            $('body').trigger('update_checkout');
                        }
                    },
                    dataType: 'html'
                });
    
                return false;
            });
        });
    })(jQuery);
    

提交回复
热议问题