如何获得用户在结帐时选择的送货方式? [英] How do I get the shipping method the user has chosen during checkout?

查看:59
本文介绍了如何获得用户在结帐时选择的送货方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to get the name of the shipping method the user has chosen during checkout. Does anyone know how to retrieve that info?

这在一定程度上可以解决问题,但是会被缓存:

This will get it to some extent but it is cached:

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

当我一步一步结帐时,我返回到运送"选项卡并更改运送方式,它仍然保留旧的运送方式.我需要弄清楚如何获得最新的.

When I am on the onestep checkout and I go back to the shipping tab and change the shipping, it is still holding the old shipping method. I need to figure out how to get the current one.

推荐答案

前言

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

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

app/design/frontend/base/default/template/checkout/onepage/shipping_method/available.phtml uses this code to determine which shipping method was selected:

$this->getAddressShippingMethod()

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

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

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

Let's research a bit and expand it even deeper:

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

父块扩展方法 getQuote():

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

更深:

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

合并所有代码可为我们提供此功能:

Merging all that code gives us this:

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

这为您提供了运输方法代码.鉴于此,您可以按照自己的意愿进行操作.这些数据存储在数据库中,因此,当您更改运输方式时,代码也会更改.

That gives you the shipping method code. Giving that, you could manipulate it just as you wish. This data is stored within the database, so when you change shipping method, the code changes too.

如果您曾经创建过自己的运输方法,您会知道它具有称为 collectRates()的方法.

If you've ever created your own shipping method, you'd know, that it has the method called collectRates().

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

It fills a set of shipping/rate_result_method models, stores it within the instance of shipping/rate_result model and returns it (you can get each model' instance using Mage::getModel(<model i've named>); ).

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

Yet, note: one could contain multiple rate_result_method instances, while the shipping method code is the same for all those instances!

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

Thus, in order to get the description, you need to get one of the rate_result_method instances and retrieve its methodTitle or carrierTitle.

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

After a small researching i've found how to retrieve all these rates:

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

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

This will provide you with a collection of all rates for the selected shipping method. You can operate it with getItems() and get a hash. Or you could use getFirstItem() and use it as the template.

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

Anyway, let's assume u've retrieved some item of that collection and stored it within the $rate variable:

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


就这样,伙计们!

我真的为我的英语差和心智怪异而感到抱歉.希望这对您或其他人有帮助.谢谢!


That's all, folks!

I am really sorry for my poor English and for my strange mind flow. Hope this will help you or someone else. Thanks!

这篇关于如何获得用户在结帐时选择的送货方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆