前言
由 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 实例之一并检索其 methodTitle 或 carrierTitle。
经过小小的研究,我发现了如何检索所有这些费率:
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**
就是这样,伙计们!
对于我糟糕的英语和我奇怪的思维方式,我感到非常抱歉。希望这会对您或其他人有所帮助。谢谢!