Home Get a dynamic calculated fee based on postcode input in Woocommerce
Reply: 0

Get a dynamic calculated fee based on postcode input in Woocommerce

user4272
1#
user4272 Published in May 23, 2018, 6:38 pm

I trying to pass a variable in woocommerce_cart_calculate_fees hook, but this code doesn't work. When I try to pass static value within a variable it works.

Here is my code:

add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees', 20, 1);
function add_custom_fees( WC_Cart $cart){

    $final_discount; // storing ajax value
    $static_value = 5; // static value

    if ($final_discount) {
       /**
        * custom discount for the cart in woocommerce
        */
        // Hook before calculate fees
        add_action('woocommerce_cart_calculate_fees', 'add_custom_fees');

        /**
         * Add custom fee if more than three article
         * @param WC_Cart $cart
         */
        function add_custom_fees(WC_Cart $cart) {
            // Calculate the amount to reduce
            global $final_discount;
            $discount = $cart->subtotal * $final_discount / 100;
            $cart->add_fee('10% discount has been added.', -$discount);
        }
    }
}

EDIT:

Now I am to pass a WC_Session value in woocommerce_cart_calculate_fees hook, the code works but session variable does not update before page refreshing.

The session variable is storing from onchange ajax value of billing_postcode field of woocommerce checkout page.

My jQuery code (Ajax):

jQuery(document).ready(function () {
    jQuery('#billing_postcode').on('change', function (event) {
        //alert( this.value );
        event.preventDefault();
        var billing_postcode = jQuery('#billing_postcode').val();

        console.log(billing_postcode);
        var data = {
            action: 'woocommerce_apply_state',
            security: wc_checkout_params.apply_state_nonce,
            billing_postcode: billing_postcode
        };

        jQuery.ajax({
            type: "POST",
            data: data,
            url: wc_checkout_params.ajax_url,
            success: function (code) {
                console.log(code);
                if (code === '0') {
                    jQuery('body').trigger('update_checkout');
                }
            },
            dataType: 'html'
        });
    });
});

My PHP code in functions.php file of my theme:

wp_enqueue_script('zip_code', get_template_directory_uri() . '/assets/js/zipcode.js', array('jquery'));
wp_localize_script('zip_code', 'wc_checkout_params', array('ajaxurl' => admin_url('admin-ajax.php')));

add_action('wp_ajax_woocommerce_apply_state', 'discount', 10);
add_action('wp_ajax_nopriv_woocommerce_apply_state', 'discount', 10);

function discount() {
    if(isset($_POST['billing_postcode'])){
        $billing_postcode = isset($_POST['billing_postcode'])?$_POST['billing_postcode']:'not yet';
        global $wpdb;
        $zipcodes = $wpdb->get_results( 
            $wpdb->prepare( "
                SELECT * FROM wp_zip_codes_value 
                WHERE zip_code_value = %d", 
                $billing_postcode
            ) 
        );

        $zip_for_discount = array();
        foreach ( $zipcodes as $zipcode ){
            $zip_for_discount = $zipcode->zip_code_id;
        }

        $find_discount = $wpdb->get_results( 
            $wpdb->prepare( "
                SELECT * FROM wp_zip_codes 
                WHERE zip_code = %d", 
                $zip_for_discount
            ) 
        );

        $final_discount = array();
        if($find_discount){
            foreach ( $find_discount as $discount ){
                $final_discount[] = $discount->discount;
            }
        }
        $final_discount[0];
        WC()->session->set( 'final_discount', $final_discount[0] );
    }
}

add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees', 20, 1);

function add_custom_fees( WC_Cart $cart){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    $percent = WC()->session->get( 'final_discount' );
    if( $percent > 0 ){
        $discount = $cart->subtotal * $percent / 100;
        $cart->add_fee( __('Zip Code Discount', 'woocommerce' ) . " ($percent%)", -$discount);
    }
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.387342 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO