/** * {@inheritdoc} */ public function modifyData(array $data) { $fieldCode = 'quantity_and_stock_status'; $model = $this->locator->getProduct(); $modelId = $model->getId(); /** @var StockItemInterface $stockItem */ $stockItem = $this->stockRegistry->getStockItem($modelId, $model->getStore()->getWebsiteId()); $stockData = $modelId ? $this->getData($stockItem) : []; if (!empty($stockData)) { $data[$modelId][self::DATA_SOURCE_DEFAULT][self::STOCK_DATA_FIELDS] = $stockData; } if (isset($stockData['is_in_stock'])) { $data[$modelId][self::DATA_SOURCE_DEFAULT][$fieldCode]['is_in_stock'] = (int) $stockData['is_in_stock']; } if (!empty($this->stockConfiguration->getDefaultConfigValue(StockItemInterface::MIN_SALE_QTY))) { $minSaleQtyData = null; $defaultConfigValue = $this->stockConfiguration->getDefaultConfigValue(StockItemInterface::MIN_SALE_QTY); if (strpos($defaultConfigValue, 'a:') === 0) { // Set data source for dynamicRows Minimum Qty Allowed in Shopping Cart $minSaleQtyValue = unserialize($defaultConfigValue); foreach ($minSaleQtyValue as $group => $qty) { $minSaleQtyData[] = [StockItemInterface::CUSTOMER_GROUP_ID => $group, StockItemInterface::MIN_SALE_QTY => $qty]; } } else { $minSaleQtyData = $defaultConfigValue; } $path = $modelId . '/' . self::DATA_SOURCE_DEFAULT . '/stock_data/min_qty_allowed_in_shopping_cart'; $data = $this->arrayManager->set($path, $data, $minSaleQtyData); } return $data; }
/** * Retrieve stock_id by store * * @param int $websiteId The website Id * * @return int */ private function getStockId($websiteId) { if (!isset($this->stockIdByWebsite[$websiteId])) { $stockId = $this->stockRegistry->getStock($websiteId)->getStockId(); $this->stockIdByWebsite[$websiteId] = $stockId; } return $this->stockIdByWebsite[$websiteId]; }
/** * Copy product inventory data (used for product duplicate functionality) * * @param \Magento\Catalog\Model\Product $product * @param \Magento\Catalog\Model\Product $duplicate * @return void */ public function build(\Magento\Catalog\Model\Product $product, \Magento\Catalog\Model\Product $duplicate) { $stockData = ['use_config_min_qty' => 1, 'use_config_min_sale_qty' => 1, 'use_config_max_sale_qty' => 1, 'use_config_backorders' => 1, 'use_config_notify_stock_qty' => 1]; $currentStockItemDo = $this->stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId()); if ($currentStockItemDo->getItemId()) { $stockData += ['use_config_enable_qty_inc' => $currentStockItemDo->getUseConfigEnableQtyInc(), 'enable_qty_increments' => $currentStockItemDo->getEnableQtyIncrements(), 'use_config_qty_increments' => $currentStockItemDo->getUseConfigQtyIncrements(), 'qty_increments' => $currentStockItemDo->getQtyIncrements()]; } $duplicate->setStockData($stockData); }
protected function setUp() { parent::setUp(); $this->stockRegistryMock = $this->getMockBuilder(StockRegistryInterface::class)->setMethods(['getStockItem'])->getMockForAbstractClass(); $this->storeMock = $this->getMockBuilder(Store::class)->disableOriginalConstructor()->getMock(); $this->stockItemMock = $this->getMockBuilder(StockItemInterface::class)->setMethods(['getData'])->getMockForAbstractClass(); $this->stockRegistryMock->expects($this->any())->method('getStockItem')->willReturn($this->stockItemMock); $this->productMock->expects($this->any())->method('getStore')->willReturn($this->storeMock); }
protected function setUp() { $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->registryMock = $this->getMock('Magento\\Framework\\Registry', [], [], '', false); $this->stockItem = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\Data\\StockItemInterface', ['getQtyIncrements'], '', false); $this->stockItem->expects($this->any())->method('getStockItem')->willReturn(1); $this->stockRegistry = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\StockRegistryInterface', ['getStockItem'], '', false); $this->stockRegistry->expects($this->any())->method('getStockItem')->willReturn($this->stockItem); $this->block = $objectManager->getObject('Magento\\CatalogInventory\\Block\\Qtyincrements', ['registry' => $this->registryMock, 'stockRegistry' => $this->stockRegistry]); }
/** * Add stock item information to the product's extension attributes * * @param \Magento\Catalog\Model\Product $product * @return \Magento\Catalog\Model\Product */ public function afterLoad(\Magento\Catalog\Model\Product $product) { $productExtension = $product->getExtensionAttributes(); if ($productExtension === null) { $productExtension = $this->productExtensionFactory->create(); } // stockItem := \Magento\CatalogInventory\Api\Data\StockItemInterface $productExtension->setStockItem($this->stockRegistry->getStockItem($product->getId())); $product->setExtensionAttributes($productExtension); return $product; }
/** * @param array $productsData * @dataProvider generateSimpleProductsWithPartialDataDataProvider * @magentoDbIsolation enabled */ public function testGenerateSimpleProductsWithPartialData($productsData) { $this->_product->setNewVariationsAttributeSetId(4); $generatedProducts = $this->_model->generateSimpleProducts($this->_product, $productsData); $parentStockItem = $this->stockRegistry->getStockItem($this->_product->getId()); foreach ($generatedProducts as $productId) { $stockItem = $this->stockRegistry->getStockItem($productId); $this->assertEquals($parentStockItem->getManageStock(), $stockItem->getManageStock()); $this->assertEquals('1', $stockItem->getIsInStock()); } }
/** * Add stock status information to products * * @param \Magento\Catalog\Model\Resource\Collection\AbstractCollection $productCollection * @return void * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ public function addStockStatusToProducts(\Magento\Catalog\Model\Resource\Collection\AbstractCollection $productCollection) { $websiteId = $this->storeManager->getStore($productCollection->getStoreId())->getWebsiteId(); $productIds = []; foreach ($productCollection as $product) { $productId = $product->getId(); $stockStatus = $this->stockRegistry->getStockStatus($productId, $websiteId); $status = $stockStatus->getStockStatus(); $product->setIsSalable($status); } }
/** * Retrieve product qty increments * * @return float|false */ public function getProductQtyIncrements() { if ($this->_qtyIncrements === null) { $stockItem = $this->stockRegistry->getStockItem($this->getProduct()->getId(), $this->getProduct()->getStore()->getWebsiteId()); $this->_qtyIncrements = $stockItem->getQtyIncrements(); if (!$this->getProduct()->isSaleable()) { $this->_qtyIncrements = false; } } return $this->_qtyIncrements; }
/** * @param \Magento\Quote\Model\Quote $quote * @param \Magento\Catalog\Model\Product $product * @param \Magento\Framework\DataObject $config * @return \Magento\Quote\Model\Quote\Item|string */ public function init(\Magento\Quote\Model\Quote $quote, \Magento\Catalog\Model\Product $product, \Magento\Framework\DataObject $config) { $stockItem = $this->stockRegistry->getStockItem($product->getId(), $quote->getStore()->getWebsiteId()); if ($stockItem->getIsQtyDecimal()) { $product->setIsQtyDecimal(1); } else { $config->setQty((int) $config->getQty()); } $product->setCartQty($config->getQty()); $item = $quote->addProduct($product, $config, \Magento\Catalog\Model\Product\Type\AbstractType::PROCESS_MODE_FULL); return $item; }
public function testBuildWithCurrentProductStockItem() { $expectedData = ['use_config_min_qty' => 1, 'use_config_min_sale_qty' => 1, 'use_config_max_sale_qty' => 1, 'use_config_backorders' => 1, 'use_config_notify_stock_qty' => 1, 'use_config_enable_qty_inc' => 'use_config_enable_qty_inc', 'enable_qty_increments' => 'enable_qty_increments', 'use_config_qty_increments' => 'use_config_qty_increments', 'qty_increments' => 'qty_increments']; $this->stockRegistry->expects($this->once())->method('getStockItem')->will($this->returnValue($this->stockItemDoMock)); $this->stockItemDoMock->expects($this->any())->method('getItemId')->will($this->returnValue(50)); $this->stockItemDoMock->expects($this->any())->method('getUseConfigEnableQtyInc')->will($this->returnValue('use_config_enable_qty_inc')); $this->stockItemDoMock->expects($this->any())->method('getEnableQtyIncrements')->will($this->returnValue('enable_qty_increments')); $this->stockItemDoMock->expects($this->any())->method('getUseConfigQtyIncrements')->will($this->returnValue('use_config_qty_increments')); $this->stockItemDoMock->expects($this->any())->method('getQtyIncrements')->will($this->returnValue('qty_increments')); $this->duplicateMock->expects($this->once())->method('setStockData')->with($expectedData); $this->model->build($this->productMock, $this->duplicateMock); }
/** * @param \Magento\Catalog\Block\Product\View $block * @param array $validators * @return array */ public function afterGetQuantityValidators(\Magento\Catalog\Block\Product\View $block, array $validators) { $stockItem = $this->stockRegistry->getStockItem($block->getProduct()->getId(), $block->getProduct()->getStore()->getWebsiteId()); $params = []; $params['minAllowed'] = max((double) $stockItem->getQtyMinAllowed(), 1); if ($stockItem->getQtyMaxAllowed()) { $params['maxAllowed'] = $stockItem->getQtyMaxAllowed(); } if ($stockItem->getQtyIncrements() > 0) { $params['qtyIncrements'] = (double) $stockItem->getQtyIncrements(); } $validators['validate-item-quantity'] = $params; return $validators; }
/** * Run test getProductStockQty method * * @return void */ public function testGetProductStockQty() { $productId = 10; $websiteId = 99; $qty = 100.0; $productMock = $this->getMock('Magento\\Catalog\\Model\\Product', ['getId', 'getStore'], [], '', false); $storeMock = $this->getMock('Magento\\Store\\Model\\Store', ['getWebsiteId'], [], '', false); $stockItemMock = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\Data\\StockItemInterface', [], '', false, true, true, ['getQty']); $productMock->expects($this->once())->method('getId')->will($this->returnValue($productId)); $productMock->expects($this->once())->method('getStore')->will($this->returnValue($storeMock)); $storeMock->expects($this->once())->method('getWebsiteId')->will($this->returnValue($websiteId)); $this->stockRegistryMock->expects($this->once())->method('getStockItem')->with($productId, $websiteId)->will($this->returnValue($stockItemMock)); $stockItemMock->expects($this->once())->method('getQty')->will($this->returnValue($qty)); $this->assertEquals($qty, $this->_block->getProductStockQty($productMock)); }
/** * {@inheritdoc} */ public function modifyData(array $data) { $fieldCode = 'quantity_and_stock_status'; $model = $this->locator->getProduct(); $modelId = $model->getId(); $stockItem = $this->stockRegistry->getStockItem($modelId, $model->getStore()->getWebsiteId()); $stockData = $stockItem->getData(); if (!empty($stockData)) { $data[$modelId][self::DATA_SOURCE_DEFAULT][self::STOCK_DATA_FIELDS] = $stockData; } if (isset($stockData['is_in_stock'])) { $data[$modelId][self::DATA_SOURCE_DEFAULT][$fieldCode]['is_in_stock'] = (int) $stockData['is_in_stock']; } return $this->prepareStockData($data); }
/** * Init stock item * * @param \Magento\Quote\Model\Quote\Item\Option $option * @param \Magento\Quote\Model\Quote\Item $quoteItem * * @return \Magento\CatalogInventory\Model\Stock\Item * @throws \Magento\Framework\Exception\LocalizedException */ public function getStockItem(\Magento\Quote\Model\Quote\Item\Option $option, \Magento\Quote\Model\Quote\Item $quoteItem) { $stockItem = $this->stockRegistry->getStockItem($option->getProduct()->getId(), $quoteItem->getStore()->getWebsiteId()); if (!$stockItem->getItemId()) { throw new \Magento\Framework\Exception\LocalizedException(__('The stock item for Product in option is not valid.')); } /** * define that stock item is child for composite product */ $stockItem->setIsChildItem(true); /** * don't check qty increments value for option product */ $stockItem->setSuppressCheckQtyIncrements(true); return $stockItem; }
public function testSaveInventoryData() { $productId = 4; $websiteId = 5; $stockData = null; $websitesChanged = true; $statusChanged = true; $product = $this->getMock('Magento\\Catalog\\Model\\Product', ['getStockData', 'getIsChangedWebsites', 'dataHasChangedFor', 'getId', 'getStore', '__wakeup'], [], '', false); $product->expects($this->once())->method('getStockData')->will($this->returnValue($stockData)); if ($stockData === null) { $product->expects($this->any())->method('getIsChangedWebsites')->will($this->returnValue($websitesChanged)); $product->expects($this->any())->method('dataHasChangedFor')->will($this->returnValue($statusChanged)); if ($websitesChanged || $statusChanged) { $product->expects($this->once())->method('getId')->will($this->returnValue($productId)); $store = $this->getMock('Magento\\Store\\Model\\Store', ['getWebsiteId', '__wakeup'], [], '', false); $store->expects($this->once())->method('getWebsiteId')->will($this->returnValue($websiteId)); $product->expects($this->once())->method('getStore')->will($this->returnValue($store)); $this->stockIndex->expects($this->once())->method('rebuild')->will($this->returnValue(true)); } } else { $stockItem = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\Data\\StockItem', ['__wakeup'], '', false); $this->stockRegistry->expects($this->once())->method('getStockItem')->with($productId, $websiteId)->will($this->returnValue($stockItem)); } $this->event->expects($this->once())->method('getProduct')->will($this->returnValue($product)); $this->eventObserver->expects($this->atLeastOnce())->method('getEvent')->will($this->returnValue($this->event)); $this->assertEquals($this->observer, $this->observer->saveInventoryData($this->eventObserver)); }
public function testAfterGetQuantityValidators() { $result = ['validate-item-quantity' => ['minAllowed' => 2, 'maxAllowed' => 5, 'qtyIncrements' => 3]]; $validators = []; $productViewBlock = $this->getMockBuilder('Magento\\Catalog\\Block\\Product\\View')->disableOriginalConstructor()->getMock(); $productMock = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->setMethods(['_wakeup', 'getId', 'getStore'])->getMock(); $storeMock = $this->getMockBuilder('Magento\\Store\\Model\\Store')->disableOriginalConstructor()->setMethods(['getWebsiteId', '_wakeup'])->getMock(); $productViewBlock->expects($this->any())->method('getProduct')->willReturn($productMock); $productMock->expects($this->once())->method('getId')->willReturn('productId'); $productMock->expects($this->once())->method('getStore')->willReturn($storeMock); $storeMock->expects($this->once())->method('getWebsiteId')->willReturn('websiteId'); $this->stockRegistry->expects($this->once())->method('getStockItem')->with('productId', 'websiteId')->willReturn($this->stockItem); $this->stockItem->expects($this->once())->method('getQtyMinAllowed')->willReturn(2); $this->stockItem->expects($this->any())->method('getQtyMaxAllowed')->willReturn(5); $this->stockItem->expects($this->any())->method('getQtyIncrements')->willReturn(3); $this->assertEquals($result, $this->block->afterGetQuantityValidators($productViewBlock, $validators)); }
/** * Retrieve stock identifier * * @return int */ public function getStockId() { $stockId = $this->getData(static::STOCK_ID); if ($stockId === null) { $stockId = $this->stockRegistry->getStock($this->getWebsiteId())->getStockId(); } return (int) $stockId; }
/** * Check item product availability for reorder * * @param \Magento\Sales\Model\Order\Item $orderItem * @return boolean */ protected function isItemAvailableForReorder(\Magento\Sales\Model\Order\Item $orderItem) { try { $stockItem = $this->stockRegistry->getStockItem($orderItem->getProduct()->getId(), $orderItem->getStore()->getWebsiteId()); return $stockItem->getIsInStock(); } catch (\Magento\Framework\Exception\NoSuchEntityException $noEntityException) { return false; } }
public function testUpdateStockItemBySku() { $itemId = 1; $this->stockItem->expects($this->once())->method('setProductId')->willReturnSelf(); $this->stockItem->expects($this->once())->method('getData')->willReturn([]); $this->stockItem->expects($this->once())->method('addData')->willReturnSelf(); $this->stockItem->expects($this->atLeastOnce())->method('getItemId')->willReturn($itemId); $this->assertEquals($itemId, $this->stockRegistry->updateStockItemBySku($this->productSku, $this->stockItem)); }
/** * Return the stock item that needs to be updated. * If the stock item does not need to be updated, return null. * * @param \Magento\Catalog\Api\Data\ProductInterface $product * @return \Magento\CatalogInventory\Api\Data\StockItemInterface|null */ protected function getStockItemToBeUpdated($product) { // from the API, all the data we care about will exist as extension attributes of the original product $extendedAttributes = $product->getExtensionAttributes(); if ($extendedAttributes !== null) { $stockItem = $extendedAttributes->getStockItem(); if ($stockItem != null) { return $stockItem; } } // we have no new stock item information to update, however we need to ensure that the product does have some // stock item information present. On a newly created product, it will not have any stock item info. $stockItem = $this->stockRegistry->getStockItem($product->getId()); if ($stockItem->getItemId() != null) { // we already have stock item info, so we return null since nothing more needs to be updated return null; } return $stockItem; }
/** * Whether to show 'Return to stock' checkbox for item * * @param Item $item * @return bool */ public function canReturnItemToStock($item = null) { if (null !== $item) { if (!$item->hasCanReturnToStock()) { $stockItem = $this->stockRegistry->getStockItem($item->getOrderItem()->getProductId(), $item->getOrderItem()->getStore()->getWebsiteId()); $item->setCanReturnToStock($stockItem->getManageStock()); } return $item->getCanReturnToStock(); } return $this->canReturnToStock(); }
/** * @expectedException \Magento\Framework\Exception\LocalizedException * @expectedExceptionMessage The stock item for Product in option is not valid. */ public function testInitializeWithInvalidOptionQty() { $optionValue = 5; $qty = 10; $this->optionMock->expects($this->once())->method('getValue')->will($this->returnValue($optionValue)); $this->quoteItemMock->expects($this->once())->method('getQtyToAdd')->will($this->returnValue(false)); $this->productMock->expects($this->any())->method('getId')->will($this->returnValue($this->productId)); $this->optionMock->expects($this->any())->method('getProduct')->will($this->returnValue($this->productMock)); $this->stockItemMock->expects($this->once())->method('getItemId')->will($this->returnValue(false)); $this->stockRegistry->expects($this->once())->method('getStockItem')->will($this->returnValue($this->stockItemMock)); $this->validator->initialize($this->optionMock, $this->quoteItemMock, $qty); }
public function testIsSalableNoManageStock() { $option1 = $this->getRequiredOptionMock(10, 10); $option2 = $this->getRequiredOptionMock(20, 10); $stockItem = $this->getStockItem(true); $this->stockRegistry->method('getStockItem')->willReturn($stockItem); $this->stockState->expects($this->at(0))->method('getStockQty')->with(10)->willReturn(10); $this->stockState->expects($this->at(1))->method('getStockQty')->with(20)->willReturn(10); $optionCollectionMock = $this->getOptionCollectionMock([$option1, $option2]); $selectionCollectionMock = $this->getSelectionCollectionMock([$option1, $option2]); $product = new \Magento\Framework\Object(['is_salable' => true, '_cache_instance_options_collection' => $optionCollectionMock, 'status' => \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED, '_cache_instance_selections_collection10_20' => $selectionCollectionMock]); $this->assertTrue($this->model->isSalable($product)); }
/** * Prepare stock item data for save * * @param \Magento\Catalog\Model\Product $product * @return $this */ protected function saveStockItemData($product) { $stockItemData = $product->getStockData(); $stockItemData['product_id'] = $product->getId(); if (!isset($stockItemData['website_id'])) { $stockItemData['website_id'] = $this->stockConfiguration->getDefaultScopeId(); } $stockItemData['stock_id'] = $this->stockRegistry->getStock($stockItemData['website_id'])->getStockId(); foreach ($this->paramListToCheck as $dataKey => $configPath) { if (null !== $product->getData($configPath['item']) && null === $product->getData($configPath['config'])) { $stockItemData[$dataKey] = false; } } $originalQty = $product->getData('stock_data/original_inventory_qty'); if (strlen($originalQty) > 0) { $stockItemData['qty_correction'] = (isset($stockItemData['qty']) ? $stockItemData['qty'] : 0) - $originalQty; } // todo resolve issue with builder and identity field name $stockItem = $this->stockRegistry->getStockItem($stockItemData['product_id'], $stockItemData['website_id']); $stockItem->addData($stockItemData); $this->stockItemRepository->save($stockItem); return $this; }
/** * Run test getMinimalQty method * * @param int $minSale * @param int|null $result * @return void * * @dataProvider dataProviderGetMinimalQty */ public function testGetMinimalQty($minSale, $result) { $id = 10; $websiteId = 99; $productMock = $this->getMock('Magento\\Catalog\\Model\\Product', ['getId', 'getStore'], [], '', false); $storeMock = $this->getMock('Magento\\Store\\Model\\Store', ['getWebsiteId'], [], '', false); $stockItemMock = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\Data\\StockItemInterface', [], '', false, true, true, ['getMinSaleQty']); $this->stockRegistryMock->expects($this->once())->method('getStockItem')->with($id, $websiteId)->will($this->returnValue($stockItemMock)); $productMock->expects($this->once())->method('getId')->will($this->returnValue($id)); $productMock->expects($this->once())->method('getStore')->will($this->returnValue($storeMock)); $storeMock->expects($this->once())->method('getWebsiteId')->will($this->returnValue($websiteId)); $stockItemMock->expects($this->once())->method('getMinSaleQty')->will($this->returnValue($minSale)); /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $productMock */ $this->assertEquals($result, $this->block->getMinimalQty($productMock)); }
public function testExecuteThatProductIdsAreObtainedFromAttributeHelper() { $this->attributeHelper->expects($this->any())->method('getProductIds')->will($this->returnValue([5])); $this->attributeHelper->expects($this->any())->method('getSelectedStoreId')->will($this->returnValue([1])); $this->attributeHelper->expects($this->any())->method('getStoreWebsiteId')->will($this->returnValue(1)); $this->stockConfig->expects($this->any())->method('getConfigItemOptions')->will($this->returnValue([])); $this->dataObjectHelperMock->expects($this->any())->method('populateWithArray')->with($this->stockItem, $this->anything(), '\\Magento\\CatalogInventory\\Api\\Data\\StockItemInterface')->willReturnSelf(); $this->product->expects($this->any())->method('isProductsHasSku')->with([5])->will($this->returnValue(true)); $this->stockItemService->expects($this->any())->method('getStockItem')->with(5, 1)->will($this->returnValue($this->stockItem)); $this->stockIndexerProcessor->expects($this->any())->method('reindexList')->with([5]); $this->request->expects($this->any())->method('getParam')->will($this->returnValueMap([['inventory', [], [7]]])); $this->messageManager->expects($this->never())->method('addError'); $this->messageManager->expects($this->never())->method('addException'); $this->object->execute(); }
/** * Processing additional validation to check if carrier applicable. * * @param \Magento\Framework\Object $request * @return $this|bool|\Magento\Framework\Object * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function proccessAdditionalValidation(\Magento\Framework\Object $request) { //Skip by item validation if there is no items in request if (!count($this->getAllItems($request))) { return $this; } $maxAllowedWeight = (double) $this->getConfigData('max_package_weight'); $errorMsg = ''; $configErrorMsg = $this->getConfigData('specificerrmsg'); $defaultErrorMsg = __('The shipping module is not available.'); $showMethod = $this->getConfigData('showmethod'); /** @var $item \Magento\Quote\Model\Quote\Item */ foreach ($this->getAllItems($request) as $item) { $product = $item->getProduct(); if ($product && $product->getId()) { $weight = $product->getWeight(); $stockItemData = $this->stockRegistry->getStockItem($product->getId(), $item->getStore()->getWebsiteId()); $doValidation = true; if ($stockItemData->getIsQtyDecimal() && $stockItemData->getIsDecimalDivided()) { if ($stockItemData->getEnableQtyIncrements() && $stockItemData->getQtyIncrements()) { $weight = $weight * $stockItemData->getQtyIncrements(); } else { $doValidation = false; } } elseif ($stockItemData->getIsQtyDecimal() && !$stockItemData->getIsDecimalDivided()) { $weight = $weight * $item->getQty(); } if ($doValidation && $weight > $maxAllowedWeight) { $errorMsg = $configErrorMsg ? $configErrorMsg : $defaultErrorMsg; break; } } } if (!$errorMsg && !$request->getDestPostcode() && $this->isZipCodeRequired($request->getDestCountryId())) { $errorMsg = __('This shipping method is not available. Please specify the zip code.'); } if ($errorMsg && $showMethod) { $error = $this->_rateErrorFactory->create(); $error->setCarrier($this->_code); $error->setCarrierTitle($this->getConfigData('title')); $error->setErrorMessage($errorMsg); return $error; } elseif ($errorMsg) { return false; } return $this; }
/** * @param ProductInterface $product * @param StockItemInterface $stockItem * @throws LocalizedException * @return void */ private function validateStockItem(ProductInterface $product, StockItemInterface $stockItem) { $defaultScopeId = $this->stockConfiguration->getDefaultScopeId(); $defaultStockId = $this->stockRegistry->getStock($defaultScopeId)->getStockId(); $stockId = $stockItem->getStockId(); if ($stockId !== null && $stockId != $defaultStockId) { throw new LocalizedException(__('Invalid stock id: %1. Only default stock with id %2 allowed', $stockId, $defaultStockId)); } $stockItemId = $stockItem->getItemId(); if ($stockItemId !== null && (!is_numeric($stockItemId) || $stockItemId <= 0)) { throw new LocalizedException(__('Invalid stock item id: %1. Should be null or numeric value greater than 0', $stockItemId)); } $defaultStockItemId = $this->stockRegistry->getStockItem($product->getId())->getItemId(); if ($defaultStockItemId && $stockItemId !== null && $defaultStockItemId != $stockItemId) { throw new LocalizedException(__('Invalid stock item id: %1. Assigned stock item id is %2', $stockItemId, $defaultStockItemId)); } }
/** * Adds stock item qty to $items (creates new entry or increments existing one) * $items is array with following structure: * array( * $productId => array( * 'qty' => $qty, * 'item' => $stockItems|null * ) * ) * * @param QuoteItem $quoteItem * @param array &$items * @return void */ protected function _addItemToQtyArray(QuoteItem $quoteItem, &$items) { $productId = $quoteItem->getProductId(); if (!$productId) { return; } if (isset($items[$productId])) { $items[$productId] += $quoteItem->getTotalQty(); } else { $stockItem = null; if ($quoteItem->getProduct()) { /** @var Item $stockItem */ $stockItem = $this->stockRegistry->getStockItem($quoteItem->getProduct()->getId(), $quoteItem->getStore()->getWebsiteId()); } $items[$productId] = $quoteItem->getTotalQty(); } }