Magento: How to get the country identifier from the selected / entered delivery address

I am trying to hide some text / code based on shipping_method.phtml based on whether the country of the selected or entered mailing address is France or not.

The only code I found was

Mage::getSingleton('checkout/type_onepage')->getQuote()->getShippingAddress()->getCountryId() 

But all this returns the country identifier of my default address in the address book. Thus, the code works when the address is already in the address book, but not if the client decides that he wants to send it to the new address.

So, I need a way to access the selected / entered CountryID in php / javascript (must be saved somewhere in the session, because it is shown in the progress sidebar).

Please note that I am using the standard one-page check in magento CE 1.7.0.2.

+4
source share
3 answers

You can try this code $this->getQuote()->getShippingAddress()->getCountry() to get the country identifier .

+8
source

The problem is that this code runs when the page loads, and basically the delivery methods block is just hidden until you reach this step. It means that

 $this->getQuote()->getShippingAddress()->getCountry() 

l

The only thing that loads Ajax is the available delivery methods, so I solved it by connecting to

 app/design/base/default/template/checkout/onepage/shipping_method/available.phtml 

and adding the following javascript code

 <?php //Show extra shipping options only if the shipping address is outside of FRANCE ?> <script type="text/javascript"> var countryID = '<?= Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getData('country_id') ?>'; jQuery('#ownTransport')[countryID == 'FR' ? 'hide' : 'show'](); </script> 

For reference: Code in

 app/design/XXX/XXX/template/checkout/onepage/shipping_method.phtml 

was

 <form id="co-shipping-method-form" class="stack-form" action=""> <div id="checkout-shipping-method-load"> <?php echo $this->getChildHtml('available') ?> </div> <script type="text/javascript"> //<![CDATA[ var shippingMethod = new ShippingMethod('co-shipping-method-form', "<?php echo $this->getUrl('checkout/onepage/saveShippingMethod') ?>"); //]]> </script> <div id="onepage-checkout-shipping-method-additional-load"> <?php echo $this->getChildHtml('additional') ?> </div> <?php //ADD AMASTY FIELDS ?> <?php //Show extra shipping options only if the shipping address is outside of FRANCE ?> <div id="ownTransport"> <?php echo Mage::helper('amorderattr')->fields('shipping_method'); ?> </div> <div id="shipping-method-buttons-container" class="buttons-set"> <button class="button" onclick="shippingMethod.save()"><?php echo $this->__('Continue') ?></button> <span class="please-wait" id="shipping-method-please-wait" style="display:none;"><?php echo $this->__('Loading') ?></span> </div> 

Hope this helps someone!

+2
source

Use this code if you are using single page check in magento

 Mage::getSingleton('checkout/type_onepage')->getQuote()->getShippingAddress() ->getCountryId(); 

you can also use: -

 $countryId =Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getData('country_id'); 
0
source

Source: https://habr.com/ru/post/1436972/


All Articles