Magento – How to get the selected shipping rate

magento2shipping

I have a a module that makes a soap call to a service to retrieve live shipping quotes, and it returns a bunch of data, an import fee being one of those things it returns.

I have created a new AbstractTotal class called ImportFee and I need a way to set the amount of this fee to be the amount of the user's selected shipping rate's import fee. I have already stored the import fee on my shipping rates in an extension_attribute, I just do not know how I am supposed to obtain the object from the AbstractTotal class.

If some code is needed, please ask which code would be specific. I do not want to spam this with a bunch of code. The concept is the same.

I have an extension_attribute on my shipping rate, and I need to set an AbstractTotal class value to that extension_attribute. Thanks!

Best Answer

Declare \Magento\Checkout\Model\Cart in construct of your class.

Try following snippet:

protected $_cart;

public function __construct (
    \Magento\Framework\App\Helper\Context $context,
    \Magento\Checkout\Model\Cart $_cart
) {
    $this->_cart = $_cart;
    parent::__construct($context);
}

Then you can do following:

$shippingAmount = $this->_cart->getQuote()->getShippingAddress()->getShippingAmount();
Related Topic