I'm strugeling with this ptoblem for a while. Need to pass data from JS to PHP on wordpress site. The error:
<script>
function activatePlacesSearch(){
var input = document.getElementById('shipping_user_location');
var autocomplete = new google.maps.places.Autocomplete(input);
var place;
google.maps.event.addListener(autocomplete, 'place_changed', function(){
place = autocomplete.getPlace();
console.log("Latitude: "+place.geometry.location.lat());
console.log("Longitude: "+place.geometry.location.lng());
$.ajax({
url:"https://DOMAIN/wp-content/themes/techmarket-child/functions.php",
method:"post",
data:{place:JSON.stingify(place)},
success: function(res){
console.log(res);
alert('Ok');
},
error: function(error){
console.log(error);
}
})
});
}
</script>
Finally the code in responsible for autofill the address, and it works. But just simply can't send the data. On the other end i have the this code. But the issue probably on the JS lv:
function add_lat_lng_to_customer_meta($fields){
$place = json_decode($_POST['exampleJS']);
echo $place;die;
return $fields;
}
add_filter( 'woocommerce_customer_save_address', 'add_lat_lng_to_customer_meta' );
If any additional data is need just write me.