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

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

问题描述

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

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 实例之一并检索其methodTitlecarrierTitle.

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天全站免登陆