我已经使用ajax调用计算了两个位置之间的距离。我想在function.php中的购物车数组中添加[“ distance”] =>“ 0km”。
我试图设置自定义字段来保存距离,但是它不起作用。
function custom_func_ajax_request() {
$addressFrom = isset($_POST['origin'])?trim($_POST['origin']):"";
$addressTo = isset($_POST['dest'])?trim($_POST['dest']):"";
// Get distance in km
$distance = getDistance($addressFrom, $addressTo, "K");
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee',$distance );
$response = array();
$response['message'] = "Successfull Request";
$response['distance'] =$distance;
echo json_encode($response);
exit;
}
function woo_add_cart_fee($distance) {
global $woocommerce;
$woocommerce->cart->add_fee( __('distance', 'woocommerce'), $distance );
}
我想在计算距离后将“距离”键及其值包括在购物车数组中。这是小的输出代码段。
["e12cb8b57b281ffd5b60a606e616e424"]=>
array(12) {
["key"]=>
string(32) "e12cb8b57b281ffd5b60a606e616e424"
["product_id"]=>
int(4237)
["variation_id"]=>
int(4241)
["variation"]=>
array(2) {
["attribute_quantity"]=>
string(5) "500ml"
["attribute_package"]=>
string(9) "Glass Jar"
}
["quantity"]=>
int(1)
["distance"]=>"2.4km" //Calculated from ajaxcall
}