Magento : how to change item price when adding it into the cart

Magento : how to change item price when adding it into the cart

September 13, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento : how to change item price when adding it into the cart

Programmatically Changing Product Prices Upon Cart Addition in Magento

In Magento, changing product prices programmatically when adding items to the cart can be achieved using observers and custom methods. This functionality isn’t available out-of-the-box through catalog or cart rules.

Understanding the Context

The provided StackOverflow answer demonstrates how to update an existing cart item with a new price. However, it doesn’t cover the initial addition of a product to the cart at a modified price.

For this scenario, we’ll need to create a custom observer that listens for the checkout_cart_product_add_after event. This event triggers every time a product is added to the cart after the price has been calculated and before it’s actually added.

Setting Up the Solution

  1. Create an Observer Class: Navigate to your custom module and generate an observer file, e.g., AddToCartObserver. Your directory structure should look something like this:
app/code/Vendor/Module/etc/events.xml
app/code/Vendor/Module/Observer/AddToCartObserver.php
  1. Configure the Event in events.xml: Add the following XML configuration to your module’s events.xml file:
<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_module_add_to_cart_observer" instance="Vendor\Module\Observer\AddToCartObserver" />
  </event>
</config>
  1. Implement the Observer Method: In your AddToCartObserver.php file, implement the observer method:
namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Quote\Model\Quote\Item;

class AddToCartObserver implements ObserverInterface
{
    public function execute(EventObserver $observer)
    {
        // Retrieve the quote item being added to the cart
        $item = $observer->getQuoteItem();

        // Modify the price of the product (you can retrieve it from any data source here)
        $newPrice = 19.99; // demo value

        // Set the new price for the item
        $item->setOriginalCustomPrice($newPrice);
    }
}

How It Works

In this observer, we’re intercepting the checkout_cart_product_add_after event every time a product is added to the cart. The $observer->getQuoteItem() method retrieves the Quote Item being processed.

Inside the observer method, you can modify the price of the item using any data source (a database table, an API call, or even a fixed value as shown in this example). The key line setOriginalCustomPrice($newPrice) updates the item’s price to the specified new value.

Conclusion

This approach allows for dynamic pricing adjustments when adding products to the cart without relying on catalog or cart rules. It provides greater flexibility and enables you to pull prices from external data sources if needed.

Remember, in a real-world scenario, replace the fixed $newPrice with your actual logic to retrieve the desired price. You might need to use Magento’s repository pattern to fetch product information or integrate an API call depending on where your pricing data is stored.

This solution demonstrates how Magento observers can be used to implement custom business logic at key points in the checkout process. It’s a powerful technique for extending core functionality without direct code modifications.

For more advanced examples of Magento observer usage, I recommend checking out our comprehensive guide on Magento 2 Observers for detailed explanations and best practices.

Checkout Catalog Extensions Configuration

Our network