Magento Tutorials

How to Add Product To Cart With Custom Price in Magento 2

Hello, Magento Friends!

Magento Custom Development is a must-have for any business to fill the needs and save time and efforts to serve an excellent shopping experience to the shoppers. With the aid of smooth modifications and amalgamations in the frontend as well as backend, the Magento 2 E-commerce can provide a first-class user experience where it allows the user to set the custom price while adding a product to the cart. We are making headway to appending a short fragment of code that can outweigh all the prices of products with your intended one.

Introduction

Often, it happens that the store owner is willing to set a fixed product price even if multiple options/add-ons are selected by the customer from the front end for all or some specific products. At that time we need to manually code to override all store product prices with your desired custom price instead of manually editing all store products. Also, have a glance at the Magento 2 Custom Price Extension by MageComp which enables the admin to set a product price and the customer to add their desired product price.

In this tutorial blog, we will be demonstrating how you can Add Product to Cart with the Custom Price in Magento 2. This action triggers the already calculated price by Magento and lets you add your customized price.

Steps to Add Product To Cart With Custom Price in Magento 2

Step 1: First, create a file “events.xml” at the below-given path.

app\code\Vendor\Extension\etc\frontend\events.xml

Now add the below code

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

 <event name="checkout_cart_product_add_after">

     <observer name="customprice" instance="Vendor\Extension\Observer\Customprice" />

 </event>

</config>

Step 2: Now, we need one more file named “Customprice.php” that overrides our price. Go to the below path

app\code\Vendor\Extension\Observer\Customprice.php

And finally add the code as mentioned below,

<?php

namespace Vendor\Extension\Observer;

use Magento\Framework\Event\ObserverInterface;

use Magento\Framework\App\RequestInterface;

class Customprice implements ObserverInterface

{

 public function execute(\Magento\Framework\Event\Observer $observer) {

     $item = $observer->getEvent()->getData('quote_item');

     $item = ( $item->getParentItem() ? $item->getParentItem() : $item );

     $price = 100; //set your price here

     $item->setCustomPrice($price);

        $item->setOriginalCustomPrice($price);

        $item->getProduct()->setIsSuperMode(true);

 }

}

That’s it!

Bottom Line:

And, bingo! Hope everyone is now aware of How to Add Product To Cart With Custom Price in Magento 2. You are free to play and manipulate this according to your need for setting a custom price for one or more products by adding conditions. Also, get the Magento Custom Extension Development Service and custom develop your Magento store as per your business needs.

Let us know by commenting below if you are facing any issues during the implementation of this code.

Happy Coding!

Click to rate this post!
[Total: 21 Average: 4.8]
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.🏏

View Comments

    • You have to set the proper price according to your requirement by checking the store or website parameter into the observer and everything should work fine

  • Hi, This is a nice tutorial. If you can suggest me, suppose i have different tier price (which is different than Magento product tier price added in backend) for a specific product. Then how can I achieve the same?

Recent Posts

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…

1 day 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…

1 day ago

The ABCs of Geofencing: Definition, Features and Uses

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

2 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…

3 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.…

5 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

5 days ago