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

发布于 2024-11-07 18:52:45 字数 250 浏览 0 评论 0原文

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

这将在一定程度上得到它,但它会被缓存:

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

诗化ㄋ丶相逢 2024-11-14 18:52:45

前言

由 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 strong> 将该代码扩展为:

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

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

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

就是这样,伙计们!

我真的很抱歉我糟糕的英语和我奇怪的思维方式。希望这会对您或其他人有所帮助。谢谢!

Foreword

Constructed from Magento app/code/core/Mage/Checkout/Block/Onepage/Shipping/Method/Available.php and others:

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 expands that code to this:

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

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

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

Parent block expands method getQuote():

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

And deeper:

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.


Getting deeper and deeper!

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

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>); ).

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

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()

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.

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!

金兰素衣 2024-11-14 18:52:45

以防万一您仍然需要它。您可以从 order by 获取运输方式:

$order->getShippingMethod();

当然,您如何获取 $order 取决于上下文。
您还可以通过以下方式获取描述:

$order->getShippingDescription();

Just in case you need it still. You can get shipping method from order by:

$order->getShippingMethod();

Of course how you get your $order depends on context.
Also you can get description by:

$order->getShippingDescription();
望喜 2024-11-14 18:52:45
shipping method in magento

$methods = Mage::getSingleton('shipping/config')->getActiveCarriers();
$options = array();
foreach($methods as $_code => $_method)
{
    if(!$_title = Mage::getStoreConfig("carriers/$_code/title"))
        $_title = $_code;

    $options[] = array('value' => $_code, 'label' => $_title . " ($_code)");
}
echo "<xmp>";
print_r($options);
echo "</xmp>";
shipping method in magento

$methods = Mage::getSingleton('shipping/config')->getActiveCarriers();
$options = array();
foreach($methods as $_code => $_method)
{
    if(!$_title = Mage::getStoreConfig("carriers/$_code/title"))
        $_title = $_code;

    $options[] = array('value' => $_code, 'label' => $_title . " ($_code)");
}
echo "<xmp>";
print_r($options);
echo "</xmp>";
乖不如嘢 2024-11-14 18:52:45

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

In your checkout controller you need to add extra steps to save your quote if you want this information to be accessible to you.
I added a few '$quote->save();' entries to get this to work, however, I cannot definitively say which entry is the one that did the fix. I also cannot find the link on Magento forums, however, I hope I have given you a head start on what is going on.

甜是你 2024-11-14 18:52:45

您可以重写 Mage_Checkout_OnepageController 中的 saveShippingMethodAction() 函数,或对其进行扩展,然后通过插入以下内容将该方法保存到注册表中:

Mage::register('blahShippingMethod', $this->getRequest()->getPost('shipping_method'));  

并在需要时调用它: Mage::registry('blahShippingMethod');

当您不再需要它时,不要忘记取消设置它,因为如果您尝试在以下情况重置,您将遇到错误它已经被设置了。

Mage::unregister('blahShippingMethod');

You could override the saveShippingMethodAction() function in the Mage_Checkout_OnepageController, or extend upon it, and save the method into the registry by inserting:

Mage::register('blahShippingMethod', $this->getRequest()->getPost('shipping_method'));  

and call upon it as you need it: Mage::registry('blahShippingMethod');

Don't forget to unset it when you no longer need it as you will run into an error if you try to reset when it's already been set.

Mage::unregister('blahShippingMethod');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文