2

I am beginner in magento 2 and I am trying to create an event and observer that will update the qty of item to 2 from default which is 1 when user add the item to cart. For this I successfully created the module and registered it and event file is as below

app/code/My/Redirect/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="after_add_to_cart" instance="My\Redirect\Observer\MyObserver" />
    </event>
</config>

and observer as below
app/code/My/Redirect/Observer/MyObserver.php

<?php

namespace My\Redirect\Observer;

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

class MyObserver 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);
    }

}

here the program is changing the price to 100 in base currency and its working fine but i want to update the qty with price according to that qty.

5
  • You shouldn't need to touch the price. Just use $item->setQty(2)
    – Haim
    Commented Nov 1, 2018 at 5:18
  • 1
    and also how to get the default price of the product and is there any need to make change in the price to make price relevant to the 2 qty? Commented Nov 1, 2018 at 5:24
  • You can check list of events, rakeshjesadiya.com/list-of-all-events-in-magento-2 Commented Nov 1, 2018 at 5:25
  • Once you set the qty, the price should update on its own. Try it
    – Haim
    Commented Nov 1, 2018 at 5:25
  • 1
    Thanks It worked!! yeah it worked Now the only problem is that it didn't update the price in the mini cart. Everything else is running Awesome. Put your answer in the answer box I will mark it as Correct. Commented Nov 1, 2018 at 5:39

2 Answers 2

1

You can check list of all events in Magento 2 version from, List of Magento 2 Events.

You need to use events checkout_cart_update_items_before or checkout_cart_update_items_after to accomplish your task,

Now you need to create an events.xml file,

<?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_update_items_before">
        <observer name="after_add_to_cart" instance="My\Redirect\Observer\MyObserver" />
    </event>
</config>

Create observer and do the logic for your events.

4
  • 1
    everything else is working fine qty is also updated in my module by $item->setQty(2) but now the problem is the price. It not updating to relevant quantity Commented Nov 1, 2018 at 5:36
  • 1
    The price is updating in the cart but not in the mini cart. Commented Nov 1, 2018 at 5:43
  • @CrackIt: reload the minicart then it will work
    – B.K
    Commented Nov 1, 2018 at 6:01
  • 1
    Is there any other way without reloding just by adding jquery ajax or something else? Commented Nov 1, 2018 at 6:12
1

Just use $item->setQty(2) Special Thanks to Haim for solving this problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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