0

I'm using WooCommerce. The checkout form has two fields hidden (latitude and longitude). To show the shipping price, I'm using an API. I created a function of class new Address( $dato1, $dato2 ); in test.php. This needs the information from AJAX (latitude and longitude) when the customer completes this in the same page. How I can modify this to work?

function shipping($rates){
  ...
  $dato2 = $_POST['dato1lat'];
  $dato3 = $_POST['dato2long'];

  $origen = new Address(  $dato1, $dato2 );
  $destino = new Address(  -85.582, -58.585 );

  $order = new Order();
  $order->setAddresses( [$origen, $destino] );
  $orderEstimado = $api->estimateOrderPrice( $order );
  $shipping_cost = $orderEstimate['total']['amount'];
  WC()->session->set( 'shipping_city_cost', $shipping_cost );
  foreach ( WC()->cart->get_shipping_packages() as $package_key => $package ) {
    WC()->session->set( 'shipping_for_package_' . $package_key, false );
  }

  //Price
  add_filter( 'woocommerce_package_rates', 'adjust_shipping_rate', 50 );
  function adjust_shipping_rate( $rates ){
    foreach ($rates as $rate) {
      $cost = $rate->cost;
      $rate->cost = WC()->session->get( 'shipping_city_cost' );
    }
    return $rates;
}

My Javascript code:

var dato1lat = $('#billing_billing_lat').val();
var dato2long = $('#billing_billing_lon').val();

$.ajax({
  data:{ dato1lat: lat,dato2long: lng},
  url:"/wp-content/plugins/test.php",
  contentType: "application/json",
  success: function(output) {
    alert(output);
  }
});

jQuery('body').trigger('update_checkout');

Thanks!

kmoser
  • 8,780
  • 3
  • 24
  • 40
Jennifer
  • 9
  • 1
  • 1
  • 6
  • Does this answers your question: https://stackoverflow.com/questions/6685249/jquery-performing-synchronous-ajax-requests ? – Owl May 19 '20 at 22:08
  • Given that you're passing `shipping` a parameter (`$rates`), my guess is you're calling that function outside the context of your AJAX call, or in other words, that function is being used by multiple 'things', not all of them AJAX. If that is the case, you need to check within the function if your POST data exists to discern between what's an AJAX request and what is not. Your question is incredibly unclear however. – parttimeturtle May 19 '20 at 23:28
  • @parttimeturtle Exactly Sorry for my english. A part of the function is ajax, so ajax should be executed before, because NewAdress need these parameters to works and bring the correct result of price of shipping – Jennifer May 20 '20 at 01:29
  • hi @Owl i cant figure out how to apply it, reformulate my question better if it wasnt understood. Thanks! – Jennifer May 20 '20 at 01:45

1 Answers1

0

I see a wrong variable in ajax call data.

Could you try this;

Change:

data:{ dato1lat: lat,dato2long: lng},

with:

data:{dato1lat: dato1lat,dato2long: dato2long},

Because of:

 var dato1lat = $('#billing_billing_lat').val();
 var dato2long = $('#billing_billing_lon').val();

EDIT: I found another problem, change your variables in your php file;

$dato1 = $_POST['dato1lat'];
$dato2 = $_POST['dato2long'];

$origen = new Address(  $dato1, $dato2 );

In comments, you asked an ajax sensitive to change if I understood correctly. Here is codes which I use in these situation

$('.yourClasswhichInputs').on('change keyup paste', function () {
    var dato1lat = $('#billing_billing_lat').val();
    var dato2long = $('#billing_billing_lon').val();
    $.ajax({
        url: "test.php",
        type: "POST",
        data: {dato1lat:dato1lat, dato2long:dato2long},
        success: function(output){
                alert(output);
            }
    });
 });
Ilker
  • 1
  • 1
  • 2
  • thanks @liker i modified this but the result on my function is undefined yet (because the checkout form isnt complete). and if i put a condition later it dont change. any suggest? – Jennifer May 20 '20 at 02:14
  • Shouldn't it be: `data:{ lat: dato1lat, lng: dato2long }`? – kmoser May 20 '20 at 03:58
  • @liker thanks for your help. I refer to php function sensitive to action of ajax. regards! – Jennifer May 20 '20 at 23:19