0

Due to some custom requirement on cart page, I need country id when someone change it using jquery.

enter image description here

I have tried with below jquery script but its not working.

<script>
require(['jquery', 'jquery/ui'], function($){
  jQuery(document).ready( function() {
    $('select[name="country_id"]').on("click",function(){
        alert('hi');
        var countyId=$(this).filter(":selected").val();
        alert(countyId);
    });
      
  });
});
</script>

please share your idea on how to achieve it.

1 Answer 1

1

You can get it using quote object in JS. See code below. Might have to change a bit according to your requirements.

define([
    'jquery',
    'uiComponent',
    'Magento_Checkout/js/model/quote'
], function ($, Element, quoteData) {
    'use strict';

    return Element.extend({
getCountryId: function () {         
            if(quoteData.shippingAddress() !== null && quoteData.shippingAddress() !== undefined ){
                //do something
                //get country id with quoteData.shippingAddress().countryId
            }
 });
});

Sorry, if there are some syntax errors while copying and editing it, hope you get that. And also you can write this function in other ways, but main thing was using quote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.