_eventManager->dispatch( 'checkout_cart_update_items_before', ['cart' => $this, 'info' => $infoDataObject] ); $qtyRecalculatedFlag = false; foreach ($data as $itemId => $itemInfo) { $item = $this->getQuote()->getItemById($itemId); if (!$item) { continue; } if (!empty($itemInfo['remove']) || isset($itemInfo['qty']) && $itemInfo['qty'] == '0') { $this->removeItem($itemId); continue; } $qty = isset($itemInfo['qty']) ? (double)$itemInfo['qty'] : false; if ($qty > 0) { $item->clearMessage(); $item->setHasError(false); $item->setQty($qty); if ($item->getHasError()) { throw new \Magento\Framework\Exception\LocalizedException(__($item->getMessage())); } if (isset($itemInfo['before_suggest_qty']) && $itemInfo['before_suggest_qty'] != $qty) { $qtyRecalculatedFlag = true; $this->messageManager->addNoticeMessage( __('Quantity was recalculated from %1 to %2', $itemInfo['before_suggest_qty'], $qty), 'quote_item' . $item->getId() ); } } if (isset($itemInfo['extension_attributes']) && is_array($itemInfo['extension_attributes'])) { foreach ($itemInfo['extension_attributes'] as $attribute => $val) { if ($val!='' || $val!=null) { $item->setData($attribute, $val); } } } if (!empty($itemInfo['discount_percent'])) { $item->setDiscountAmount($item->getRowTotal()*$itemInfo['discount_percent']); $item->setDiscountPercent($itemInfo['discount_percent']); } // $item->setRowTotal($item->getRowTotal()); // $item->setDiscountPercent($itemInfo['discount_percent']); } isset($data['salesperson_id']) && $this->getQuote()->setData('salesperson_id', $data['salesperson_id']); isset($data['customer_note']) && $this->getQuote()->setCustomerNote($data['customer_note']); if ($qtyRecalculatedFlag) { $this->messageManager->addNoticeMessage( __('We adjusted product quantities to fit the required increments.') ); } $this->_eventManager->dispatch( 'checkout_cart_update_items_after', ['cart' => $this, 'info' => $infoDataObject] ); return $this; } /** * Get request quantity * * @param Product $product * @param \Magento\Framework\DataObject|int|array $request * @return int|DataObject * @throws LocalizedException */ protected function getQtyRequest($product, $request = 0) { $request = $this->_getProductRequest($request); $stockItem = $this->stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId()); $minimumQty = $stockItem->getMinSaleQty(); //If product quantity is not specified in request and there is set minimal qty for it if ($minimumQty && $minimumQty > 0 && !$request->getQty() ) { $request->setQty($minimumQty); } return $request; } /** * Add product to shopping cart (quote) * * @param int|Product $productInfo * @param \Magento\Framework\DataObject|int|array $requestInfo * @return $this * @throws \Magento\Framework\Exception\LocalizedException * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function addProduct($productInfo, $requestInfo = null) { $product = $this->_getProduct($productInfo); $productId = $product->getId(); if ($productId) { $request = $this->getQtyRequest($product, $requestInfo); try { $this->_eventManager->dispatch( 'checkout_cart_product_add_before', ['info' => $requestInfo, 'product' => $product] ); $result = $this->getQuote()->addProduct($product, $request); } catch (\Magento\Framework\Exception\LocalizedException $e) { $this->_checkoutSession->setUseNotice(false); $result = $e->getMessage(); } /** * String we can get if prepare process has error */ if (is_string($result)) { if ($product->hasOptionsValidationFail()) { $redirectUrl = $product->getUrlModel()->getUrl( $product, ['_query' => ['startcustomization' => 1]] ); } else { $redirectUrl = $product->getProductUrl(); } $this->_checkoutSession->setRedirectUrl($redirectUrl); if ($this->_checkoutSession->getUseNotice() === null) { $this->_checkoutSession->setUseNotice(true); } throw new \Magento\Framework\Exception\LocalizedException(__($result)); } } else { throw new \Magento\Framework\Exception\LocalizedException(__('The product does not exist.')); } $this->_eventManager->dispatch( 'checkout_cart_product_add_after', ['quote_item' => $result, 'product' => $product] ); $this->_checkoutSession->setLastAddedProductId($productId); return $this; } public function truncate() { $this->getQuote()->removeAllItems(); $this->getQuote()->setCustomerNote(null)->setData('salesperson_id', 0); return $this; } }