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

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

2 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

5 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

5 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

5 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

6 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago