public function testInitWithoutModificationAdminhtmlAreaCode() { $areaCode = \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE; $storeId = 1000000000; $requestId = 20000000; $itemId = $requestId; $this->stateMock->expects($this->any())->method('getAreaCode')->will($this->returnValue($areaCode)); $this->productMock->expects($this->never())->method('getCustomOptions'); $this->productMock->expects($this->never())->method('getStickWithinParent'); $this->productMock->expects($this->any())->method('getParentProductId')->will($this->returnValue(true)); $this->itemMock->expects($this->never())->method('setOptions'); $this->itemMock->expects($this->never())->method('setProduct'); $this->itemMock->expects($this->any())->method('getId')->will($this->returnValue($itemId)); $this->itemMock->expects($this->never())->method('setData'); $this->storeMock->expects($this->any())->method('getId')->will($this->returnValue($storeId)); $this->objectMock->expects($this->never())->method('getResetCount'); $this->objectMock->expects($this->never())->method('getId'); $this->processor->init($this->productMock, $this->objectMock); }
/** * Advanced func to add product to quote - processing mode can be specified there. * Returns error message if product type instance can't prepare product. * * @param mixed $product * @param null|float|\Magento\Framework\Object $request * @param null|string $processMode * @return \Magento\Sales\Model\Quote\Item|string * @throws \Magento\Framework\Model\Exception */ public function addProduct(\Magento\Catalog\Model\Product $product, $request = null, $processMode = \Magento\Catalog\Model\Product\Type\AbstractType::PROCESS_MODE_FULL) { if ($request === null) { $request = 1; } if (is_numeric($request)) { $request = $this->objectFactory->create(['qty' => $request]); } if (!$request instanceof \Magento\Framework\Object) { throw new \Magento\Framework\Model\Exception(__('We found an invalid request for adding product to quote.')); } $cartCandidates = $product->getTypeInstance()->prepareForCartAdvanced($request, $product, $processMode); /** * Error message */ if (is_string($cartCandidates)) { return $cartCandidates; } /** * If prepare process return one object */ if (!is_array($cartCandidates)) { $cartCandidates = array($cartCandidates); } $parentItem = null; $errors = array(); $item = null; $items = array(); foreach ($cartCandidates as $candidate) { // Child items can be sticked together only within their parent $stickWithinParent = $candidate->getParentProductId() ? $parentItem : null; $candidate->setStickWithinParent($stickWithinParent); $item = $this->getItemByProduct($candidate); if (!$item) { $item = $this->itemProcessor->init($candidate, $request); // Add only item that is not in quote already $this->addItem($item); } $items[] = $item; /** * As parent item we should always use the item of first added product */ if (!$parentItem) { $parentItem = $item; } if ($parentItem && $candidate->getParentProductId()) { $item->setParentItem($parentItem); } $this->itemProcessor->prepare($item, $request, $candidate); // collect errors instead of throwing first one if ($item->getHasError()) { $message = $item->getMessage(); if (!in_array($message, $errors)) { // filter duplicate messages $errors[] = $message; } } } if (!empty($errors)) { throw new \Magento\Framework\Model\Exception(implode("\n", $errors)); } $this->_eventManager->dispatch('sales_quote_product_add_after', ['items' => $items]); return $item; }