Sometimes the price in your catalog isn't the price a product should carry into the cart. Maybe you've negotiated a special rate for a B2B customer, you're running a promo that isn't easy to model with cart price rules, or you're building a bundle flow where one item needs to land in the cart at a different amount.
Magento 2 lets you override the price at the exact moment a product is added to the cart. In this guide, we'll do it the clean way — with a small custom module and an observer on the checkout_cart_product_add_after event. No core edits, nothing that breaks on upgrade.
One thing worth checking before you write any code: if what you're really after is a promotion — say, you want to automatically add a free product to cart when the order total crosses a threshold or a specific item is purchased — you don't need custom development for that at all. A rule-based extension handles it from the admin in minutes, and your marketing team can change the offer without calling a developer.
Still need a truly custom price? Let's build it.
Step 1: Create a Small Module
We'll call the module Vendor_CustomCart. Create app/code/Vendor/CustomCart/registration.php:
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Vendor_CustomCart',
__DIR__
);
Then declare the module in app/code/Vendor/CustomCart/etc/module.xml:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_CustomCart"/>
</config>
Step 2: Register the Observer
Create app/code/Vendor/CustomCart/etc/events.xml. Note the schema here is events.xsd, not module.xsd — a common copy-paste mistake that silently breaks the observer:
<?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="vendor_customcart_set_custom_price"
instance="Vendor\CustomCart\Observer\SetCustomPrice"/>
</event>
</config>
Step 3: Write the Observer
Create app/code/Vendor/CustomCart/Observer/SetCustomPrice.php:
<?php
namespace Vendor\CustomCart\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class SetCustomPrice implements ObserverInterface
{
public function execute(Observer $observer)
{
$item = $observer->getEvent()->getData('quote_item');
// For configurable products the price lives on the parent item
$item = $item->getParentItem() ?: $item;
// Replace this with your real pricing logic
$customPrice = 19.99;
$item->setCustomPrice($customPrice);
$item->setOriginalCustomPrice($customPrice);
$item->getProduct()->setIsSuperMode(true);
}
}
Three lines do the actual work. setCustomPrice() changes the price shown in the cart, setOriginalCustomPrice() makes it survive totals recalculation (without it, the catalog price comes back on the next cart update), and setIsSuperMode(true) tells Magento to accept the override without extra validation.
Step 4: Enable the Module and Test
php bin/magento module:enable Vendor_CustomCart
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:flush
Add a product to the cart from the storefront and you should see it priced at 19.99 regardless of its catalog price. In a real project, the hard-coded value would come from your own logic — the customer group, a request parameter, quantity tiers, or an external pricing API.
Two things to test before you ship: simple products inside configurables (that's what the getParentItem() line is for), and what happens when a guest cart merges into a customer cart after login — if the price should persist there too, apply the same logic on the merged quote items.
Custom Code or an Extension?
This observer approach is the right call when your pricing rule is fixed and technical — one condition, one price, set once and forget. But every hard-coded rule is code your team maintains through every Magento upgrade.
If the requirement is really "let the admin decide the price per product or per customer from the backend", the Magento 2 Custom Price extension does exactly that without touching code — the custom price stays hidden from other customers, and your team can adjust it anytime from the admin panel.
Conclusion
Overriding the cart price in Magento 2 takes one observer and about thirty lines of code — the key details are using the right event, remembering setOriginalCustomPrice(), and handling parent items for configurables.
Building something more involved on top of this — tiered logic, API-driven prices, B2B quoting? Our Magento extension development services can take it from prototype to production.