结帐距离使用WooCommerce 3.3中的Google Map API进行计算

时间:2018-04-30 14:30:55

标签: php jquery ajax woocommerce google-api

我发布了a similar problem a few months back,我得到了帮助以解决问题。它在Woocommerce版本3.1.2之前工作正常。我一直在更新到最新版本的WooCommerce,希望问题能够得到解决。

我根据结帐时客户billing_area(自定义下拉列表)与卖家billing_city(在用户个人资料中设置)的值之间的差异添加动态费用。我使用的代码如下 -

这是运行jQuery脚本以使billing_area的值更改为:

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
}

这是计算费用的代码:

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);
     }
}

问题是 - 它不再起作用了。

如果我转储该值,它会返回$dist_abs的值(类似于5.4,即卖方位置和客户区域之间的距离)。但是,购物车不收费。

有时,当服务器或网站速度较慢时,我会在订单审核中看到标签(交付 - 距离费率),但一旦网页正确加载,它就会消失。

有人能告诉我为什么它不再起作用了吗?有什么我想念的吗?

1 个答案:

答案 0 :(得分:2)

我找到了使用ajax而不是cookie来使其工作的方法......所以请尝试以下方法:

// Testing ==> To be removed

代码放在活动子主题(或活动主题)的function.php文件中。经过测试和工作。

一旦开始工作,您必须使用implementation 'com.google.android.gms:play-services-maps:12.0.0'

删除所有相关行