How To

How to Override Product Shipping Price in Magento 2

Because your customers care about shipping! It is powerful enough to make or break your business because creating the product, offering at best prices and serving great customer experience is not enough. Your way of shipping and delivering products safely to the hands of customers can be the ultimate reason for success. Online shoppers are too picky when it comes to shipping costs and delivery options that you are using. Even sometimes store owners are offering shipping at low cost to increase average order value when the customer is making a bulk purchase or fulfilling some conditions.
Recently we did the same for one of our clients and it will be helpful to other store owners too. So, here is our another blog on “How to Override Product Shipping Prices in Magento 2.

To do the same first we need to create “di.xml” at the following location.
app\code\Vendor\Extension\di.xml

<pre class="lang:default decode:true">
<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
          <preference for="Magento\OfflineShipping\Model\Carrier\Tablerate" type="Vendor\Extension\Model\Carrier\Tablerate" />
    </config>
</pre>

Now, we need to create one more file “Tablerate.php” that contains our main logic file for setting up custom shipping price.
app\code\Vendor\Extension\Model\Carrier\Tablerate.php

<pre class="lang:default decode:true">
<?php
namespace Vendor\Extension\Model\Carrier;
use Magento\Quote\Model\Quote\Address\RateRequest;
class Tablerate extends \Magento\OfflineShipping\Model\Carrier\Tablerate
{
    public function collectRates(RateRequest $request)
    {
        //Do your logic before this line and return your custom price.
        $shippingPrice = 50;
        $method->setPrice($shippingPrice);
    }
}    
</pre>

That’s it, hope this code will help you to Override Product Shipping Price in your Magento 2 Store. You can even use & customize this code according to your need for integrating stylesheet.

Also, let us know if you are facing an issue while implementing using this code, we are happy to help you.

Happy Coding!

Click to rate this post!
[Total: 11 Average: 2.7]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

6 days ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

2 weeks ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

2 weeks ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 weeks ago