2Hats . 2 minutes
January 8, 2014

Estimating Shipping Rate on Product Details Page in Magento

To display the shipping rates on the product view page in Magento 2, you can use a custom block and template. Just follow these simple steps to make it happen:

Create a Custom Module:

If you don’t have a custom module yet, it’s a great idea to create one following the best practices for module creation in Magento 2.

Create a Block Class:

In your custom module, let’s create a block class to handle the super cool logic for obtaining shipping rates!

You can change the zip code and country in the code above to match the customer’s details. So, if you want to put in their zip code and country from the session, you can do that too!

Create a Template File:

You gotta make a template file if you wanna show the shipping rates

<!– File: app/code/YourVendor/YourModule/view/frontend/templates/product/shipping_rates.phtml –>

Create a Layout XML File:

Create a layout XML file in your module to tell the system where you want the block to show up.

<!– File: app/code/YourVendor/YourModule/view/frontend/layout/catalog_product_view.xml –>

Bingo!!! You’re all done. Just run the “di:compile” and “cache:clear” commands now.

So, I tried this out in Magento 2.4, but I think it should work the same for other versions too. Just keep in mind that the dependency injections might be a bit different depending on the Magento version.

blog
Greetings! I'm Aneesh Sreedharan, CEO of 2Hats Logic Solutions. At 2Hats Logic Solutions, we are dedicated to providing technical expertise and resolving your concerns in the world of technology. Our blog page serves as a resource where we share insights and experiences, offering valuable perspectives on your queries.
Aneesh ceo
Aneesh Sreedharan
Founder & CEO, 2Hats Logic Solutions
Subscribe to our Newsletter
Aneesh ceo

    Stay In The Loop!

    Subscribe to our newsletter and learn about the latest digital trends.