【问题标题】:Magento : How to get countryID from selected / entered shipping addressMagento:如何从选定/输入的送货地址中获取 countryID
【发布时间】:2012-09-21 06:24:21
【问题描述】:

我正在尝试隐藏一些基于 shipping_method.phtml 中的文本/代码,具体取决于所选或输入的送货地址所在的国家/地区是否为法国。

我找到的唯一代码是

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

但这所做的只是返回我在通讯录中的默认地址的 countryID。 因此,当地址已经在地址簿中时,代码有效,但如果客户决定他/她要将其发送到新地址,则代码无效。

所以我需要一种方法来访问 php/javascript 中的选择/输入的 CountryID(应该存储在会话中的某处,因为它显示在进度侧边栏中)。

请注意,我在 magento CE 1.7.0.2 中使用标准单页结帐。

【问题讨论】:

  • 您尝试修改的页面是什么?退房?哪一步?
  • 在这种情况下 $this->getQuote()->getShippingAddress()->getCountry() 应该可以工作。
  • 这给了我和以前一样的结果。您将获得默认送货地址的 countryID,而不是所选地址(如果您有多个保存的送货地址)或输入地址(如果您选择输入新地址)的 countryID。
  • 奇怪。对我来说它正在工作。
  • 试试Mage::getSingleton('checkout/session') 而不是$this

标签: magento checkout shipping street-address country


【解决方案1】:

您可以尝试使用此代码$this->getQuote()->getShippingAddress()->getCountry()获取国家/地区 ID

【讨论】:

    【解决方案2】:

    问题是在页面加载时执行此代码,并且基本上运输方法块只是隐藏,直到您到达该步骤。这意味着

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

    l

    Ajax 加载的唯一内容是可用的运输方式,所以我通过挂钩解决了它

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

    并添加以下 javascript 代码

    <?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>
    

    供参考:

    中的代码
    app/design/XXX/XXX/template/checkout/onepage/shipping_method.phtml 
    

    曾经

    <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>
    

    希望这对某人有所帮助!

    【讨论】:

    • 只是要把这个扔出去......我不确定你想在这里完成什么,但值得注意的是,如果你想修改运费或任何基于在国家/地区,WebShopApps 的人们拥有丰富的成熟运输模块阵容,几乎可以满足您的任何需求。
    • 这正是我要搜索的内容。我想检查发货国家/地区是德国还是付款国家/地区。非常感谢。您为我节省了很多时间 :)
    【解决方案3】:

    如果您在 magento 中使用单页结帐,请使用此代码

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

    你也可以使用:-

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多