Magento – How to get the shipping method the user has chosen during checkout

magento

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.

Best Answer

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!

Related Topic