I posted a similar problem a few months back and I have been helped out there to solve it. It was working fine until Woocommerce version 3.1.2. I have been updating to latest version of WooCommerce hoping the problem will be solved.
I am adding dynamic fees based on the difference between customer's billing_area
(customized dropdown) on checkout and the value of seller's billing_city
(set in user profile). The code I was using as below -
This is the jQuery Script that runs to get the value of billing_area as changed:
add_action( 'woocommerce_after_checkout_form', 'custom_checkout_jquery_script', 10 );
function custom_checkout_jquery_script() {
?>
<script type="text/javascript">
(function($){
$( 'form.checkout' ).on( 'change', '#billing_area', function(){
var location = $('#billing_area option:selected').val();
document.cookie="cusarea="+location;
// Browser console output (Just for testing)
function readCookie(n){ for(var r=n+"=",t=document.cookie.split(";"),e=0;e<t.length;e++){
for(var i=t[e];" "==i.charAt(0);)i=i.substring(1,i.length);
if(0==i.indexOf(r))return i.substring(r.length,i.length)}return null}
$('body').trigger('update_checkout');
console.log('Selected Area: '+location+' | Cookie: '+readCookie("cusarea"));
//$('#order_review').load(document.URL + ' #order_review');
//window.location.reload(true)
});
})(jQuery);
</script>
<?php
}
This is the code for calculating fees:
add_action( 'woocommerce_cart_calculate_fees', 'distance_shipping_fee', 30, 1 );
function distance_shipping_fee( $wc_cart ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
if( empty($_COOKIE ['cusarea']) ) return;
else $cookie = $_COOKIE ['cusarea'];
// Encoding the customer's location for Google API
$customer_area = rawurlencode( $cookie );
// Getting billing city of vendors
foreach( $wc_cart->get_cart() as $cart_item ){
$vendor_id = get_post_field( 'post_author', $cart_item['product_id'] );
$vendors[$vendor_id] = get_user_meta($vendor_id, 'billing_city', true);
}
foreach( $vendors as $vend_loc){
// Setting Google API URL ##
$gapi_key = MY_APY; // Set HERE your google api key
$shippingurl = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=$vend_loc";
$shippingurl .= "+dhaka+bangladesh&destinations=$customer_area+dhaka+bangladesh&key=$gapi_key";
// Now fetching json response from googleapis:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $shippingurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($ch), true);
// If google responds with a status of OK: Extract the distance text:
if($response['status'] == "OK")
$dist = $response['rows'][0]['elements'][0]['distance']['text'];
$dist_array[] = preg_replace("/[^0-9\.]/", '', $dist);
}
// Get the bigger distance
$dist_ance = max($dist_array);
$dist_abs = abs ($dist_ance);
if ( $dist_abs < 5) {
$wc_cart->add_fee( "Delivery - Distance Rate", 10 , true);
} else {
$wc_cart->add_fee( "Delivery - Distance Rate", 20 , true);
}
}
Problem is - It's not working anymore.
If I dump the value, it returns me the value of $dist_abs
(something like 5.4 which is the distance between seller's location and customer's area). But, no fee is added to the cart.
Sometimes, when server or site is slow, I see the label (Delivery - Distance Rate) appears in order review, but it disappears once page loads properly.
Can anyone tell me why it's not working anymore? Is there something I'm missing?