Menu Close
    Searching for an experienced Magento 2 Development Company ?

    Magento2 – Set custom price of Product when adding to cart

    Here we will see how to set custom price of product in magento2.
    You can change product price when adding product to cart. You can achieve this by Observer.

    learn here how to create observer in Magento2.

    Set Custom Price of Product

    First create events.xml file in folder ‘Webkul/Hello/etc/frontend’ and use event ‘checkout_cart_product_add_after’.

    <?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="customprice" instance="Webkul\Hello\Observer\CustomPrice" />
    	</event>
    </config>

    Now create CustomPrice.php file in Observer folder.

    <?php
    	/**
    	 * Webkul Hello CustomPrice Observer
    	 *
    	 * @category    Webkul
    	 * @package     Webkul_Hello
    	 * @author      Webkul Software Private Limited
    	 *
    	 */
    	namespace Webkul\Hello\Observer;
    
    	use Magento\Framework\Event\ObserverInterface;
    	use Magento\Framework\App\RequestInterface;
    
    	class CustomPrice implements ObserverInterface
    	{
    		public function execute(\Magento\Framework\Event\Observer $observer) {
    			$item = $observer->getEvent()->getData('quote_item');			
    			$item = ( $item->getParentItem() ? $item->getParentItem() : $item );
    			$price = 100; //set your price here
    			$item->setCustomPrice($price);
    			$item->setOriginalCustomPrice($price);
    			$item->getProduct()->setIsSuperMode(true);
    		}
    
    	}
    . . .
    Discuss on Helpdesk

    Leave a Comment

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


    11 comments

  • Abhishek Panchal
    Is there a way to update selected **bundle product** option price programmatically on cart page?

    by using this code I am able to get Selected Bundle Product Options but I don’t know how to update price for each option Individually?

    $options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());

    Basically, I want to update quote of single and bundle product with custom price.

  • Ashish
    Hello,

    After adding custom price to product currency is not converting on cart page. Please suggest any solution if you also faced the issue.
    Magento ver. 2.1.7

    Thanks

    • Webkul Support

      Hello Ashish,

      You need to convert amount according to currency.
      you can use this snippet to get price according to currency.
      $objectManager = MagentoFrameworkAppObjectManager::getInstance();$storeManager = $objectManager->get(‘MagentoStoreModelStoreManagerInterface’);$priceCurrency = $objectManager->get(‘MagentoFrameworkPricingPriceCurrencyInterface’);$currency = null;$price = 100;$store = $storeManager->getStore()->getStoreId();$price = $priceCurrency->convert($price, $store, $currency);

      if you have any issue or doubt let me know.

      Thnaks

      Rahul Mahto

  • nagaraju
    i am able to add to cart the product but unable to redirect and price still shows zero. could u please suggest me i have followed the above code
  • Manish Saini
    not working i have follow same method. class and function not calling.
  • tallenmusclegeeks
    not working on the first time a product is added, though works well if the same product is added again.
  • Felipe
    Thanks!!! Works fine!!
    • nagaraju
      hi @@felipemarquesweb:disqus my i know while doing this process i am getting price zero whether you have faced the same issue
      • Felipe Marques
        try disable all modules or plugins and test again … for me this works fine!
        • nagaraju
          thanks @@felipemarquesweb:disqus now i got the output tanks once again
  • Valentin Picui
    Hello, and how I can change the product name?
  • Back to Top