【问题标题】:How do I get the shipping method the user has chosen during checkout?如何获取用户在结帐时选择的送货方式?
【发布时间】:2011-08-27 08:06:21
【问题描述】:

我想获取用户在结帐时选择的送货方式的名称。有谁知道如何检索该信息?

这会在一定程度上得到它但它被缓存了:

Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingDescription();

当我在一步结账时,我返回运输选项卡并更改运输,它仍然保留旧的运输方式。我需要弄清楚如何获得当前的。

【问题讨论】:

标签: magento


【解决方案1】:

前言

由 Magento app/code/core/Mage/Checkout/Block/Onepage/Shipping/Method/Available.php 等构建:

app/design/frontend/base/default/template/checkout/onepage/shipping_method/available.phtml 使用此代码来确定选择了哪种运输方式:

$this->getAddressShippingMethod()

app/code/core/Mage/Checkout/Block/Onepage/Shipping/Method/Available.php 将该代码扩展为:

return $this->getAddress()->getShippingMethod();

让我们进行一些研究并进一步扩展:

$this->getQuote()->getShippingAddress()->getShippingMethod();

父块扩展方法getQuote()

return $this->getCheckout()->getQuote();

更深入:

public function getChechout() {
    return Mage::getSingleton('checkout/session');
}

合并所有代码给我们这个:

Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingMethod()

这为您提供了运输方式代码。给它,你可以随心所欲地操纵它。此数据存储在数据库中,因此当您更改运输方式时,代码也会更改。


越来越深!

如果您曾经创建过自己的送货方式,您就会知道,它有一个名为 collectRates() 的方法。

它填充一组 shipping/rate_result_method 模型,将其存储在 shipping/rate_result 模型的实例中并返回它(您可以使用 Mage::getModel(); ).

但是,请注意:一个可能包含多个 rate_result_method 实例,而所有这些实例的运输方式代码都是相同的!

因此,为了获取描述,您需要获取 rate_result_method 实例之一并检索其 methodTitlecarrierTitle

经过小小的研究,我发现了如何检索所有这些费率:

Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingRatesCollection()

这将为您提供所选运输方式的所有费率的集合。您可以使用 getItems() 对其进行操作并获取哈希值。或者您可以使用 getFirstItem() 并将其用作模板。

无论如何,假设您已检索到该集合的某些项目并将其存储在 $rate 变量中:

$rate->getCarrier(); // This will provide you with the carrier code
$rate->getCarrierTitle(); // This will give you the carrier title
$rate->getCode(); // This will give you **current shipping method** code
$rate->getMethod(); // This will provide you with the **shipping method** code
$rate->getMethodTitle(); // This will tell you current shipping method title
$rate->getMethodDescription(); // And this is the description of the current shipping method and **it could be NULL**

就是这样,伙计们!

对于我糟糕的英语和我奇怪的思维方式,我感到非常抱歉。希望这会对您或其他人有所帮助。谢谢!

【讨论】:

  • 很好解释。谢谢!
【解决方案2】:

以防万一您仍然需要它。您可以通过以下方式从订单中获取送货方式:

$order->getShippingMethod();

当然,如何获得 $order 取决于上下文。 您也可以通过以下方式获取描述:

$order->getShippingDescription();

【讨论】:

    【解决方案3】:
    shipping method in magento
    
    $methods = Mage::getSingleton('shipping/config')->getActiveCarriers();
    $options = array();
    foreach($methods as $_code => $_method)
    {
        if(!$_title = Mage::getStoreConfig("carriers/$_code/title"))
            $_title = $_code;
    
        $options[] = array('value' => $_code, 'label' => $_title . " ($_code)");
    }
    echo "<xmp>";
    print_r($options);
    echo "</xmp>";
    

    【讨论】:

      【解决方案4】:

      如果您希望您可以访问此信息,则需要在结帐控制器中添加额外的步骤来保存您的报价。
      我添加了一些'$quote->save();'条目以使其正常工作,但是,我不能明确地说哪个条目是进行修复的条目。我也找不到 Magento 论坛上的链接,但是,我希望我已经让您了解正在发生的事情。

      【讨论】:

        【解决方案5】:

        您可以覆盖Mage_Checkout_OnepageController 中的saveShippingMethodAction() 函数,或对其进行扩展,然后通过插入将方法保存到注册表中:

        Mage::register('blahShippingMethod', $this->getRequest()->getPost('shipping_method'));  
        

        并在需要时调用它:Mage::registry('blahShippingMethod');

        当您不再需要它时不要忘记取消设置,因为如果您在已设置的情况下尝试重置,则会遇到错误。

        Mage::unregister('blahShippingMethod');
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-27
          • 1970-01-01
          • 2020-10-08
          • 2014-10-12
          • 2021-09-17
          相关资源
          最近更新 更多