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

标签 magento

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

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

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

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

最佳答案

前言

从 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() 的方法。

它填充了一组运费/rate_result_method 模型,将其存储在 的实例中运费/费率结果模型并返回它(您可以使用 Mage::getModel(); 获取每个模型的实例)。

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

因此,为了获得描述,您需要获得 rate_result_method 实例之一并检索其 方法标题运营商名称 .

经过一小部分研究后,我发现了如何检索所有这些费率:
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**

就是这样,伙计们!

我真的很抱歉我糟糕的英语和我奇怪的思维流。希望这会帮助你或其他人。谢谢!

关于magento - 如何获得用户在结账时选择的送货方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6032936/

相关文章:

magento - Mage::log() 函数不起作用

magento - 以编程方式切换商店magento 2

php - 第一次客户折扣 magento

mysql - 使用 MySQL Workbench 连接到 Docker MySQL

php - Magento 中的定期促销

php - 通过 Postman 使用 Magento API 通过 Oauth 1.0 授权

php - 以编程方式将 Magento 订单标记为已完成

php - Magento 中的 Paypal 状态

php - 将文件sql导入cpanel服务器上的phpmyadmin时出现超时错误

magento - 在此服务器上找不到请求的 URL (Magento)