How to Get Selected Shipping Rate in Magento 2 Checkout Page
Store owners implement various pricing strategies like offering discounts, coupon codes, or even have a system of store credits in order to manage refunds.
Also, sometimes, the customers are eligible for tax exemption such as VAT exemption.
In all these scenarios, the online store owner needs to calculate the exemption on the order total.
For Magento 2 store owners, if one wants to implement this exemption on subtotal and shipping rates, one needs to get selected shipping rate in Magento 2 checkout page.
The programmatic method to do is shown below using which you can get the selected shipping rate on the checkout page and use it to calculate the exemption for discounts or store credits on order total. Enhance the user experience by providing real-time cost calculation, transparency and essential shipping information by checkout summary update on selecting shipping method in Magento 2.
Method to Get Selected Shipping Rate in Magento 2 Checkout Page:
Use the below code in your block file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php namespace Vendor\Magento\Block; use Magento\Checkout\Model\Session; class ShippingRate implements ConfigProviderInterface { protected $checkoutSession; protected $quote; public function __construct( Session $checkoutSession, ) { $this->checkoutSession = $checkoutSession; $this->quote = $checkoutSession->getQuote(); } public function getShippingRate() { $shippingAmount = $this->quote->getShippingAddress()->getShippingAmount(); } } |
That’s it
If you have any doubts regarding this post, just mention them in the Comments section below.
I would be happy to help.
Feel free to share the solution with Magento Community via social media.
Thank You.
Related Articles:
- How to Create Custom Carrier Trackers in Magento 2
- How to Configure Magento 2 Custom Order Number Extension
- How to Change Shipping Price on Address Field Change in Magento 2 Custom Shipping Method
- How to Configure Checkout Totals Sort Order in Magento 2
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How to Disable Welcome Email After Creating New Customer From Admin in Magento 2
How to Make Purchase Order Number Optional in Magento 2
Next