X
    Categories: Knowledgebase

How to get selected shipping method in magento

Magento 1 :

//get shipping method
Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingMethod()
//get shipping descriptions
Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingDescription();
//get shipping data
Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getData();

Magento 2 :

You can do this by calling the MagentoCheckoutModelSession. Add it to your constructor and get it by using this code :

protected $_checkoutSession;

public function __construct(
    MagentoCheckoutModelSession $checkoutSession
){
    $this->_checkoutSession = $checkoutSession;
}

//call you code in the methos somewehre like this :
//get shipping method
$this->_checkoutSession->getQuote()->getShippingAddress()->getShippingMethod();
//get shipping descriptions
$this->_checkoutSession->getQuote()->getShippingAddress()->getShippingDescription();
//get shipping data
$this->_checkoutSession->getQuote()->getShippingAddress()->getData();

Or you can get by Object Manager Instance by using this code :

//Get Object Manager Instance
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
//Get checkout Session by Object Manager Instance
$checkoutSession = $objectManager->create('MagentoCheckoutModelSession');
//get shipping method
$checkoutSession->getQuote()->getShippingAddress()->getShippingMethod();
//get shipping descriptions
$checkoutSession->getQuote()->getShippingAddress()->getShippingDescription();
//get shipping data
$checkoutSession->getQuote()->getShippingAddress()->getData();

Thank for reading this post, Hope it helps.

Kien Wiliam: Magento Ecommerce Developer