4

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

The only code I found was

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

But all this does is return the countryID of my default address in the address book. So the code works when the address is already in the address book, but not if the customer decides he/she want to send it to a new address.

So I need a way to access the selected / entered CountryID in php/javascript (should be stored in the session somewhere, because it's shown in the progress sidebar).

Please note that I'm using standard onepage checkout in magento CE 1.7.0.2.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Kim Herman
  • 61
  • 1
  • 1
  • 3
  • What is the page you try to modify? Checkout? What step? – user487772 Sep 29 '12 at 20:35
  • in this case $this->getQuote()->getShippingAddress()->getCountry() should work. – user487772 Sep 29 '12 at 21:20
  • That gives me the same result as before. You get the countryID of the default shipping address and not of the selected address (if you have multiple saved shipping addresses) or of the entered address (if you choose to enter a new address). – Kim Herman Sep 29 '12 at 21:37
  • Strange. For me it is working. – user487772 Sep 29 '12 at 22:31
  • Try `Mage::getSingleton('checkout/session')` instead of `$this`. – user487772 Sep 29 '12 at 22:32
  • The problem is this code gets executed when the page loads and basically the shipping methods block is just hidden. The only thing that gets loaded by Ajax is the available shipping methods, so I solved it by hooking into the – Kim Herman Sep 30 '12 at 10:41

3 Answers3

8

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

MeenakshiSundaram R
  • 2,837
  • 3
  • 29
  • 41
Afsal
  • 349
  • 3
  • 10
2

The problem is this code gets executed when the page loads and basically the shipping methods block is just hidden until you get to that step. This means that

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

l

The only thing that gets loaded by Ajax is the available shipping methods, so I solved it by hooking into

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 : The 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 out !

Kim Herman
  • 61
  • 1
  • 1
  • 3
  • Just going to throw this out there... I'm not sure what you're trying to accomplish here, but it's worth noting that if you're trying to modify shipping rates or anything based on country, the folks at WebShopApps have a solid lineup of mature shipping modules than can do just about anything you need. – pspahn Oct 01 '12 at 03:15
  • thats exactly what i was searching for.. i wanted to check if the shipping country is germany or equals the billing country.. thanks a lot.. you saved me lots of time :) – alphanyx Jan 21 '13 at 15:49
0

Use this code if you are using onepage checkout in magento

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

you can also use :-

$countryId =Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getData('country_id');
Ankur
  • 2,171
  • 23
  • 29
mukund002
  • 123
  • 1
  • 2
  • 15