Magento 2: Add Product to Cart With Custom Price

Magento 2 Add Product to Cart With Custom Price - SbDEVBlog

In real-world scenarios, very often, we need to change the price at the time of adding items to the cart. In other words, we allow the vendors to sell as a sample using a sample price by showing them the actual price in the catalog.

Magento introduced a feature called a custom price. The quote item table consists of fields custom_price and `original_custom_price`. These fields are responsible for setting up a custom price. The default values for both fields are null. If Magento reads null values, the cart item will apply standard prices, but if both fields contain values, then Magento will apply the price to the cart from them. Even if we set 0 (zero) in these fields, Magento will consider 0 prices in the cart.

We can alter these fields according to our requirements. The most common requirement is to set a custom price while adding a product to the cart.

Magento 2: Add Product to Cart With Custom Price

So, let’s add a sample price while adding the product to the cart.

We will use an event checkout_cart_product_add_after.

And we will create an observer class. Since we need a frontend, we will use it in this post-frontend area. The area can be used according to the requirements.

<?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="sbdevcustomprice" instance="SbDevBlog\Catalog\Observer\AddCustomPrice" />
    </event>
</config>

It’s time to create an observer class.

<?php
/**
 * @copyright Copyright (c) sbdevblog (http://www.sbdevblog.com)
 */

namespace SbDevBlog\Catalog\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class AddCustomPrice implements ObserverInterface
{
    private const SAMPLE_PRICE = 0;

    /**
     * ADD SAMPLE PRICE
     *
     * @param Observer $observer
     * @return void
     */
    public function execute(Observer $observer)
    {
        $item = $observer->getEvent()->getData('quote_item');
        $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
        $item->setCustomPrice(self::SAMPLE_PRICE);
        $item->setOriginalCustomPrice(self::SAMPLE_PRICE);
        $item->getProduct()->setIsSuperMode(true);
    }
}

That’s it. The above example will add 0 (zero) as a custom price. You can change this according to what you need.

Thank you for reading the post, Magento 2: Add Product to Cart with Custom Price. Please use the comment section for your feedback. Also, please share this post and our blog with your connections. Thanks again. Subscribe, and stay tuned for new posts.

Download Source Code

Click here to add multiple products to the cart programmatically.

One thought on “Magento 2: Add Product to Cart With Custom Price

Leave a Reply

Your email address will not be published. Required fields are marked *