4

I have write module for set custom price of product when adding to cart but my code not working.

     namespace Navin\Testcart\Observer;

        use Magento\Framework\Event\ObserverInterface;
        use Magento\Framework\App\RequestInterface;

        class CustomPrice implements ObserverInterface
        {
            public function invoke(\Magento\Framework\Event\Observer $observer) {
                //echo "test";exit();
                 $item=$observer->getEvent()->getData('quote_item');
                 $product=$observer->getEvent()->getData('product');
                $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
                // Load the custom price
                $price = $product->getPrice()+10; // 10 is custom price. It will increase in product price.
                // Set the custom price
                $item->setCustomPrice($price);
                $item->setOriginalCustomPrice($price);
                // Enable super mode on the product.
                $item->getProduct()->setIsSuperMode(true);
            }

        }
  

Help for resolve my issue.

  • Please add more details about your issue? Which event you want to catch? – Khoa TruongDinh Sep 23 '16 at 5:51
  • Did you found solution?? – Kaushal Suthar Apr 10 '17 at 6:36
3

Please use this even checkout_cart_product_add_after, and also I suggest you that

ObserverInterface has only one method which is execute, so write your code in this method.

And do not use parent Item because while you are adding to cart any item then price depend upon child item so the best way is to use directly that item object that you are getting. So best practice is to use that item itself.

And also return $this object at the end of your code inside execute method.

For eg: namespace Navin\Testcart\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');
        $product=$observer->getEvent()->getData('product');
        // here i am using item's product final price
        $price = $item->getProduct()->getFinalPrice()+10; // 10 is custom price. It will increase in product price.
        // Set the custom price
        $item->setCustomPrice($price);
        $item->setOriginalCustomPrice($price);
        // Enable super mode on the product.
        $item->getProduct()->setIsSuperMode(true);
        return $this;
    }

}

Hope this will help you. :)

  • not working i have try @Rose Hale – Navin Bhudiya Sep 23 '16 at 13:52
  • When i apply wrong coupon code product price become 0 in row total. If i apply again, price display correct. Do you know why this happen? It happen only first time. – user55548 Nov 19 '18 at 10:22
  • This is not work if when bundle product is in cart in magento-2.1 @Rose Hale. – Lovely Setia Nov 20 '18 at 10:34
  • 1
    Working.. Thanks @Rose Hale.. – Bharat Chodvadiya Apr 4 at 5:45
  • hi @Rose, same code i am using in controller, its not working for me, here is my question,magento.stackexchange.com/questions/274172/… – jafar pinjar May 13 at 15:17

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.