0

Can any one please suggest me how to change the product price when adding product to cart through admin.

I want to change the price of the product programmatically when adding product to cart.

1

Create events.xml in app/code/Vendor/Module/etc

<?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="sales_quote_add_item">
        <observer name="customprice" instance="Vendor\Module\Observer\CustomPrice" />
    </event>
</config>

Create CustomPrice.php in Vendor\Module\Observer

<?php

    namespace Vendor\Module\Observer;

    use Magento\Framework\Event\ObserverInterface;
    use Magento\Framework\App\RequestInterface;
    use Magento\Framework\App\State;
    class CustomPrice implements ObserverInterface
    {
    protected $_state;
    public function __construct(State $state){

        $this->_state=$state;
    }

        public function execute(\Magento\Framework\Event\Observer $observer) {
           if($this->_state->getAreaCode()=='adminhtml'){
            $item = $observer->getEvent()->getData('quote_item');         
            $price = 100; //set your price here
            $item->setCustomPrice($price);
            $item->setOriginalCustomPrice($price);
            $item->getProduct()->setIsSuperMode(true);
        }
      }

    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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