The Ultimate Cheat Sheet on Magento 2 Get Shipping Method Title From Quote

In this article, we going to see how we can fetch all the list of active shipping method carrier code, with all the details such as title, the amount from current quote id using Magento 2.

There are many time as you see, when you are working on customization part of checkout page you need to get all the active shipping methods for current quote.

So from current quote id we can get all the active shipping method.

To achieve this we can use "ShippingMethodManagementInterface" interface and the path is Magento\Quote\Api\ShippingMethodManagementInterface for getting shipping methods.

Let’s start with point step by step: Shipping method in Magento 2

With the help of below script, you can get active shipping method carrier code title.

<?php
namespace Techone\Modulename\PathtoClass;

use Exception;
use Magento\Framework\Exception\StateException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Quote\Api\ShippingMethodManagementInterface;

class ShippingCarrierCode
{
    /**
     * @var ShippingMethodManagementInterface
     */
    private $shippingMethodManagement;

    public function __construct(
        ShippingMethodManagementInterface $shippingMethodManagement
    ) {
        $this->shippingMethodManagement = $shippingMethodManagement;
    }

    /**
     * get shipping method code
     *
     * @return string[]
     * @throws LocalizedException
     */
    public function getShippingFromQuote()
    {
        $quoteId = 1;
        try {
            $getShippingMethods = $this->shippingMethodManagement->getList($quoteId);

            $methodCodes = [];
            foreach ($getShippingMethods as $method) {
                $methodCodes[] = $method->getMethodTitle() . '=>' . $method->getAmount();
            }
        } catch (StateException $stateException) {
            throw new LocalizedException(__($stateException->getMessage()));
        } catch (Exception $exception) {
            throw new LocalizedException(__($exception->getMessage()));
        }
        return $methodCodes;
}

After which,using this method $this->getShippingFromQuote() it will return current selected shipping method data (Shipping method in Magento 2).

Another important thing is that you can also fetch result for getAmount(), getCarrierTitle(), getMethodTitle() related data also using above snippets.

There is also one important note is that if your Quote is not active then it will throw error like.

Error : No such entity with CartId = 1

And suppose if you might be not selected shipping method from all the shipping list by mistake then it will throw error, Error is as follow as:

"The shipping address is missing. Set the address and try again."

Then it will return result output for Flat rate shipping method as it is selected for the current quote:

Array
(
       [0] => flatrate => 6.00
)

If your shipping methods carrier contains more than one shipping (method), then it will return all the active methods as output.

Create custom module in Magento 2

So finally we done with task of fetching all the active shipping carrier method from which you can get shipping title and amount from quote id using Magento 2.

I hope you liked my this article. If you have any queries or any question regarding this, Feel free to comment on Me.

Leave a Comment