0

I am trying to set the custom price of product to cart using Magento2 but unable to do that. For that I have created a module TW - as namespace Cart- as Module name . So under TW/Cart I have created 2 folders etc and Observer. Below is the code in respective folders. Do i need to register this module by putting registration.php file under this module ? Please help me what am I doing wrong?

Code for etc/frontend/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="cart_observer_customprice" instance="TW\Cart\Observer\CustomPrice" />
    </event>
</config>

Code for Observer/CustomPrice.php

namespace TW\Cart\Observer;
 use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
use Psr\Log\LoggerInterface;

class CustomPrice implements ObserverInterface
{
    protected $logger;
    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }
    public function execute(\Magento\Framework\Event\Observer $observer) {


        $item = $observer->getEvent()->getData('quote_item');         
        $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
        $price = 120; //set your price here
        $this->logger->debug("Hello I am here", [
            'price' => $price,
        ]);
        $item->setCustomPrice($price);
        $item->setOriginalCustomPrice($price);
        $item->getProduct()->setIsSuperMode(true);
    }

}
0

First create a module using free module creator tool called silk software

Magento 2 Module Creator Tool Using Silk Software

and enable using following commands below.

  1. php bin/magento module:enable Tw_Cart
  2. php bin/magento setup:upgrade
  3. php bin/magento setup:di:compile
  4. php bin/magento setup:static-content:deploy

create events.xml in the path:- app/code/Tw/Cart/etc/frontend/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="Tw_Cart_Addtocart_Customprice" instance="TW\Cart\Observer\CustomPrice" />
    </event>
</config>

create CustomPrice.php in the path: app/code/Tw/Cart/Observer/ & keep the below code

<?php
    namespace TW\Cart\Observer;
    use Magento\Framework\Event\ObserverInterface;
    use Magento\Framework\App\RequestInterface;
    use Psr\Log\LoggerInterface;
    class CustomPrice implements ObserverInterface
    {

    public function execute(\Magento\Framework\Event\Observer $observer) 
    {
            $writer = new \Zend\Log\Writer\Stream(BP.'/var/log/stackexchange.log');
            $logger = new \Zend\Log\Logger();
            $logger->addWriter($writer);
            /* Code here */
            $quote_item = $observer->getEvent()->getQuoteItem();
            $price = 400; //set your price here
            $quote_item->setCustomPrice($price);
            $quote_item->setOriginalCustomPrice($price);
            $quote_item->getProduct()->setIsSuperMode(true);
            $logger->info("success !!!!");
    }

}
  • I tried to vote .. but it does not allow me to do so :( – ssnegi Jul 5 '17 at 9:55
  • Tried This, product is added with custom price in cart, but in summary original product price is showing... any update on this..? – Rizwan Khan Nov 17 '17 at 11:13
  • How can I add the custom price with product tier price into cart? – Chintan Kaneriya Jun 29 '19 at 4:14

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.