开发者

How do I get the shipping method the user has chosen during checkout?

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::getSingleto开发者_如何学运维n('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.


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!


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


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>";


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.


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');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜