I am trying tho again update cart after update cart action(when cart qty is updated from cart page Or from miniCart) and change the qty if some cases
$mappedProductSkus= $this->_helperData->getSku();
$cartData=[];
foreach($quote->getAllVisibleItems() as $item)
{
$productSku=$item->getSku();
$productAdded = $this->_productRepository->get($productSku);
if(in_array($productSku, $mappedProductSkus ))
{
$itemQty= $item->getQty();
$allocatedQty = $this->_helperData->finalQty($productSku,$itemQty);
$itemId= $item->getId();
$cartData[$itemId]['qty'] = $allocatedQty;
if($allocatedQty<$itemQty){
$this->_messageManager->addNoticeMessage('Max Qty of '.$allocatedQty.' can be added to cart for'.$productAdded->getName());
}
}
else
{
$itemId= $item->getId();
$cartData[$itemId]['qty'] = 1;
if($item->getQty()>1){
$this->_messageManager->addNoticeMessage('Only one item can be bought at a time );
}
}
}
$cartData = $this->_cart->suggestItemsQty($cartData);
$this->_cart->updateItems($cartData)->save();
But as we call updateItems it again triggers checkout_cart_update_items_after
and the code goes in infinite loop
event.xml
<event name="checkout_cart_update_items_after">
<observer name="checkbeforeupdate" instance="Fleetguard\Inventoryrationing\Observer\Checkbeforeupdate" />
</event>