Woocommerce: how to use wp_ajax_woocommerce_update_order_review hook in order review page

How i can use Ajax in checkout page in woocommerce. I am trying to add custom pricing just before Payment in Order Review Page.

I have a check box when user will select the checkbox and pricing should be add in order.

Read More

Here is my code

function woocommerce_update_order_review() {
global $woocommerce;
        $prc = strip_tags($woocommerce->cart->get_cart_total());
        $priceValue = str_replace(",","",str_replace("#36;","",substr($prc,1)));
        if($priceValue<=300 && $priceValue>200){
                    $rate_cost = 2.70;
        }
        else if($priceValue>300){
                $extraDvalue = 2.70;
                $extraVal = ceil(round(($priceValue-300),0)/100)*.90;
                $newPr = $extraDvalue+$extraVal;
                $rate_cost = $newPr;    
        }
            if($priceValue>100){
                $woocommerce->cart->add_fee( 'Declared value', $rate_cost, false,  '' );
            }



    exit;
}



add_action('wp_ajax_woocommerce_update_order_review','woocommerce_update_order_review');
add_action('wp_ajax_nopriv_woocommerce_update_order_review','woocommerce_update_order_review');

JS CODE

var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
    // This does the ajax request
       jQuery.ajax({
    type: "POST",
    url: '<?php echo admin_url('admin-ajax.php'); ?>',
    data: {
            action: 'woocommerce_update_order_review', check: 'check'
        }
}).done(function(value) {
  alert(value);
});

Please suggest is this possible or not ?

Thanks

Related posts

Leave a Reply