如何获得用户在结账时选择的送货方式?

Chr*_*ris 15 magento

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

这将在某种程度上得到它,但它被缓存:

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

当我在onetep结账时,我回到运输选项卡并更改运输,它仍然持有旧的运输方式.我需要弄清楚如何获得当前的.

shy*_*cha 74

前言

由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()
Run Code Online (Sandbox Code Playgroud)

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

return $this->getAddress()->getShippingMethod();
Run Code Online (Sandbox Code Playgroud)

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

$this->getQuote()->getShippingAddress()->getShippingMethod();
Run Code Online (Sandbox Code Playgroud)

父块扩展方法getQuote():

return $this->getCheckout()->getQuote();
Run Code Online (Sandbox Code Playgroud)

而且更深:

public function getChechout() {
    return Mage::getSingleton('checkout/session');
}
Run Code Online (Sandbox Code Playgroud)

合并所有代码可以为我们提供:

Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingMethod()
Run Code Online (Sandbox Code Playgroud)

这将为您提供送货方法代码.给予它,你可以按照自己的意愿操纵它.此数据存储在数据库中,因此当您更改送货方式时,代码也会更改.


越来越深入!

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

它填充了一组shipping/rate_result_method模型,将其存储在shipping/rate_result模型的实例中并返回它(您可以使用Mage :: getModel(<model i've named>)获取每个模型的实例;).

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

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

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

Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingRatesCollection()
Run Code Online (Sandbox Code Playgroud)

这将为您提供所选送货方式的所有费率的集合.您可以使用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**
Run Code Online (Sandbox Code Playgroud)

总而言之,伙计们!

对于我可怜的英语和奇怪的思维流,我感到非常抱歉.希望这会帮助你或其他人.谢谢!


Tar*_*ras 6

万一你还需要它.您可以通过以下方式获得送货方式:

$order->getShippingMethod();
Run Code Online (Sandbox Code Playgroud)

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

$order->getShippingDescription();
Run Code Online (Sandbox Code Playgroud)


ʍǝɥ*_*ʇɐɯ 0

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