Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 530
Chris Avatar asked May 17 '11 15:05

Chris


People also ask

How do I get the selected shipping method in WooCommerce?

Go to: WooCommerce > Settings > Shipping > Shipping Zones. Hover over Zone Name, and the option to Edit and Delete appear. Select Edit, and a screen appears so you can change the name, regions or shipping methods.

How do I add shipping in WooCommerce?

Start by navigating to WooCommerce → Settings → Shipping in your dashboard, which contains your options for Shipping Zones. A shipping zone is a geographical area that you ship to, defined by countries, regions, states, and zip codes. Set up shipping zones to define different rates based on customers' locations.


1 Answers

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!

like image 175
shybovycha Avatar answered Oct 28 '22 23:10

shybovycha