I want to set custom price programmatically and placed order with that price for that product in Magento 2.i have added below code. it set the original price. and original Custom price is updated properly in quote_item table. but custom price is not updated in quote_item table. due to that custom price is not set in order item details.

$quoteItem->setCustomPrice($setPrice); $quoteItem->setOriginalCustomPrice($setPrice); $quoteItem->getProduct()->setIsSuperMode(true);$quoteItem->save();


1 Answer 1


Suggestion :
Override addtocart.phtml from
into your theme

After That you can add a hidden field in form

 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.

/** @var $block \Magento\Catalog\Block\Product\View */
<?php $_product = $block->getProduct(); ?>
<?php $buttonTitle = __('Add to Cart'); ?>
<?php if ($_product->isSaleable()) :?>
<input type="hidden" name="isCustomPrice" value="1"/> <!-- Here -->
<div class="box-tocart">
    <div class="fieldset">
        <?php if ($block->shouldRenderQuantity()) :?>
        <div class="field qty">
            <label class="label" for="qty"><span><?= $block->escapeHtml(__('Qty')) ?></span></label>
            <div class="control">
                <input type="number"
                       value="<?= $block->getProductDefaultQty() * 1 ?>"
                       title="<?= $block->escapeHtmlAttr(__('Qty')) ?>"
                       class="input-text qty"
                       data-validate="<?= $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"
        <?php endif; ?>
        <div class="actions">
            <button type="submit"
                    title="<?= $block->escapeHtmlAttr($buttonTitle) ?>"
                    class="action primary tocart"
                    id="product-addtocart-button" disabled>
                <span><?= $block->escapeHtml($buttonTitle) ?></span>
            <?= $block->getChildHtml('', true) ?>
<?php endif; ?>
<script type="text/x-magento-init">
        "#product_addtocart_form": {
            "Magento_Catalog/js/validate-product": {}

Now Create A module that will do trigger event and it will set the custom price to your product



<?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_Module" setup_version="1.0.0">

<br/ > etc/events.xml

<?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="set_custom_price_to_product" instance="Vendor\Module\Observer\CheckoutCartProductAddAfterObserver" />
    <event name="checkout_cart_product_update_after">
        <observer name="update_custom_price_to_product" instance="Vendor\Module\Observer\CheckoutCartProductAddAfterObserver" />

<br/ > Observer/CheckoutCartProductAddAfterObserver.php


namespace Vendor\Module\Observer;

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

class CheckoutCartProductAddAfterObserver implements ObserverInterface
     * @var \Magento\Framework\View\LayoutInterface
    protected $_layout;
     * @var \Magento\Store\Model\StoreManagerInterface
    protected $_storeManager;
    protected $_request;
    private $serializer;

     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Framework\View\LayoutInterface $layout
    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\View\LayoutInterface $layout,
        \Magento\Framework\App\RequestInterface $request,
        \Magento\Framework\Serialize\SerializerInterface $serializer,
    ) {
        $this->_layout = $layout;
        $this->_storeManager = $storeManager;
        $this->_request = $request;
        $this->serializer = $serializer;
     * Add order information into GA block to render on checkout success pages
     * @param EventObserver $observer
     * @return void
    public function execute(EventObserver $observer)
        $item = $observer->getEvent()->getData('quote_item');
        $item = ($item->getParentItem() ? $item->getParentItem() : $item);
        $price = 100; //set your price here

Maybe there are many ways to do but i have done it in this way. other suggestions are welcome :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.