How to Get Selected Shipping Method on Checkout Page in Magento 2

checkoutmagento-2.1shipping-methods

I would like to get the selected shipping method from the checkout page it looks like this one :

enter image description here

I just want to get the selected shipping method like above image shown : Shipping Table Rates so that I can get the shipping code method.

I have some clue to get it using \Magento\Checkout\Api\Data\ShippingInformationInterfaceFactory but when I add this to my block class I got this error :

Fatal error: Uncaught TypeError: Argument 3 passed to
Fabelio\Checkout\Block\ExpectedDelivery::__construct() must be an
instance of
Magento\Checkout\Api\Data\ShippingInformationInterfaceFactory

Is this the correct way to getting the shipping method from checkout page ?

Best Answer

You can do this by calling the Magento\Quote\Api\Data\ShippingMethodInterface. Add it to your constructor and get it by using this code :

 protected $shippingMethod;

public function __construct(ShippingMethodInterface $shippingMethod){
    $this->shippingMethod = $shippingMethod;
} 

// call you code in the methos somewehre like this : 
$methodTitle = $this->shippingMethod->getMethodTitle();
Related Topic