0

I have work on the task in which I need to handle quote item, here I have use two event

checkout_cart_add_product_complete

checkout_cart_update_item_complete

here it work fine, But when I have change Quantity directly in mini cart then my Logic was not work. For this I am find some event which work for me. If You have any solution this Pleas Provide Me

my code is

<event name="checkout_cart_add_product_complete">
    <observer name="customprice" instance="Ns\Module\Observer\CustomPrice" />
</event>
<event name="checkout_cart_update_item_complete">
    <observer name="custompriceupdate" instance="Ns\Module\Observer\CustomPrice" />
</event>
<event name="sales_quote_item_qty_set_after">
    <observer name="custompriceupdateqty" instance="Ns\Module\Observer\CustomPrice" />
</event>

and observer is

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $cart = $this->cart;
    $quote = $cart->getQuote();
    $items = $cart->getQuote()->getAllItems();
    $basetotal = 0;
    foreach ($items as $itemforprice) {
        $total = $this->processItem($itemforprice);
        $basetotal += $total;
    }

    $quote->setSubtotal($basetotal);
    $quote->setGrandTotal($basetotal);
    $quote->setBaseSubtotal($basetotal);
    $quote->setBaseGrandTotal($basetotal);
    $quote->setSubtotalWithDiscount($basetotal);
    $quote->setBaseSubtotalWithDiscount($basetotal);
    $quote->save();
}

private function processItem($itemforprice)
{
    $myprice = 100;
    $total = $myprice * $qty;

    $itemforprice->setCustomPrice($myprice);
    $itemforprice->setOriginalCustomPrice($myprice);
    $itemforprice->setBasePrice($myprice);
    $itemforprice->setPrice($myprice);

    $itemforprice->setRowTotal($total);
    $itemforprice->setBaseRowTotal($total);
    $itemforprice->setRowTotalInclTax($total);
    $itemforprice->setBaseRowTotalInclTax($total);

    $itemforprice->save();

    return $total;
}
3

Use sales_quote_item_qty_set_after which will fire when Qty is update.

 $this->_eventManager->dispatch('sales_quote_item_qty_set_after', ['item' => $this]);

I guess that you want to update cart item price.

sales_quote_item_qty_set_after event provide quote item as it parameter.So you don't need to inject cart object $this->cart here. Also, don't need to save the quote and calculate the subtotal, as Magento automatically do that during firing on this event.

<?php

namespace Ns\Module\Observer;

class CustomPriceSet  implements \Magento\Framework\Event\ObserverInterface
{

    public function execute(\Magento\Framework\Event\Observer $observer)
    {

    $item = $observer->getEvent()->getItem();
    $quote = $item->getQuote();  
        $myprice = 100;
        $item->setCustomPrice($myprice);
        $item->setOriginalCustomPrice($myprice);
        $item->getProduct()->setIsSuperMode(true);
    }

}
  • after add this event it will provide me error "We can't add this item to your shopping cart right now." – Bhavesh Prajapati Jun 24 at 9:41
  • You did the wrong.I guess that you want to set a custom price for quote item ? – Amit Bera Jun 24 at 13: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.