0

I create 2 events in 1 observer & I tried to execute them in 1 time but it just display error. How to solve it ? Is there any solution ?

=> Event :

<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="Test\Show\Observer\CustomPrice" />
  </event>
  <event name="catalog_product_collection_load_after">
    <observer name="loadupdate" instance="Test\Show\Observer\CustomPrice" />
  </event>
</config>

=> Observer :

public function execute(\Magento\Framework\Event\Observer $observer) {
        //event for change price when product added to cart
        $item = $observer->getEvent()->getData('quote_item');
        $price = $item->getProduct()->getPrice() - 800;
        $item->setCustomPrice($price);
        $item->setOriginalCustomPrice($price);
        $item->getProduct()->setIsSuperMode(true);

        //event for change product price when product data loaded
        $collections = $observer->getEvent()->getCollection();
        foreach( $collections as $collection ) {
            $collection->setPrice(25000);
            $collection->save();
        }
    }
  • 2
    what error you are getting – magento noob Sep 8 '17 at 4:52
  • When i tried to add a product to the cart, there is a warning "We can't add this item to your shopping cart right now." – Edwin Widhiyanto Sep 8 '17 at 7:07
  • have you checked log ? Also add the magento version you are using to question – magento noob Sep 8 '17 at 8:37

Your Answer

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

Browse other questions tagged or ask your own question.