fix stock item configuration#1353
Conversation
| // Sku is not assigned to Stock | ||
| return null; | ||
| } | ||
|
|
There was a problem hiding this comment.
looks like it would be great to throw NoSuchEntityException exception instead of removed code. It would be more reliable for a code of business logic.
But for doing this we need to introduce one more service isProductAssignedToStock::execute($sku, $stockId) to InventoryApi.
Because currently we create and return configuration with default values for a product which is actually not belonging to specified Stock
| $stockItemData = $this->getStockItemData->execute($sku, $stockId); | ||
| if (null === $stockItemData) { | ||
| // Sku is not assigned to Stock | ||
| return false; |
There was a problem hiding this comment.
good candidate for usage of isProductAssignedToStock::execute($sku, $stockId)
| } | ||
|
|
||
| // iterate over the required conditions: return error as soon as a condition fails | ||
| $stockItemData = $this->getStockItemData->execute($sku, $stockId); |
There was a problem hiding this comment.
another candidate for isProductAssignedToStock::execute($sku, $stockId) usage
| $requiredConditionsErrors = array_merge(...$requiredConditionsErrors); | ||
| if (count($requiredConditionsErrors)) { | ||
| return $this->productSalableResultFactory->create(['errors' => $requiredConditionsErrors]); | ||
| } |
There was a problem hiding this comment.
+ $requiredConditionsErrors = array_merge(...$requiredConditionsErrors);
+ if (count($requiredConditionsErrors)) {
+ return $this->productSalableResultFactory->create(['errors' => $requiredConditionsErrors]);
+ }
this looks like a code which could be extracted to separate private method
| ); | ||
|
|
||
| if ($stockItemConfiguration === null || $stockItemConfiguration->isManageStock()) { | ||
| if ($stockItemConfiguration->isManageStock()) { |
There was a problem hiding this comment.
this is not correct fix, because of condition $stockItemConfiguration === null was added to support Order processing for deleted products from Catalog.
and condition $stockItemConfiguration === null checks that product does not exists anymore
isProductAssignedToStock::execute($sku, $stockId) should be used here as well, but Exception should be caught here and Reservation should be appended anyway
|
|
||
| if ($stockItemConfiguration === null || !$stockItemConfiguration->isManageStock()) { | ||
| //Product not assigned to Given Stock or we No need to Manage Stock | ||
| if (!$stockItemConfiguration->isManageStock()) { |
6054445 to
a7b4fad
Compare
| $productId = $this->getProductIdsBySkus->execute([$sku])[$sku]; | ||
| } catch (NoSuchEntityException $skuNotFoundInCatalog) { | ||
| $stockItem = \Magento\Framework\App\ObjectManager::getInstance()->create(StockItemInterface::class); | ||
| $stockItem = ObjectManager::getInstance()->create(StockItemInterface::class); |
There was a problem hiding this comment.
Why do we use ObjectManager manager directly instead of construct dependency? Or is it a temporal solution?
| * | ||
| * @api | ||
| */ | ||
| interface IsSourceItemManagementAllowedForSkuInterface |
There was a problem hiding this comment.
I am not sure that we need to introduce this interface
| return true; | ||
| } | ||
| } | ||
| } catch (NoSuchEntityException $e) { |
There was a problem hiding this comment.
Looks like we need to move this logic in some of a condition instead of applying it under all of the conditions
| return $this->productSalableResultFactory->create(['errors' => $requiredConditionsErrors]); | ||
| } | ||
|
|
||
| $sufficientConditionsErrors = $this->processSufficientConditions($sku, $stockId, $requestedQty); |
There was a problem hiding this comment.
Not bad to describe in dockblock
What is a difference between requiredConditions and sufficientConditions
|
|
||
| if ($stockItemConfiguration === null || !$stockItemConfiguration->isManageStock()) { | ||
| //Product not assigned to Given Stock or we No need to Manage Stock | ||
| try { |
There was a problem hiding this comment.
Generally, we have a lot of boilerplate code like
try {
+ $stockItemConfig = $this->getStockItemConfiguration->execute($sku, $stockId);
+ } catch (NoSuchEntityException $e) {
+ // GetStockItemConfiguration throw NoSuchEntityException when SKU is not assigned to Stock
return false;
}
Maybe it will be better to return some $stockItemConfiguration with predefined values manage_stock=0 (like as Special Case pattern)
If some sku doesn't have configuration for some stock will be sensible use fallback on global configuration
In another words, move concept is sku assigned to stock our of get stock item configuration
And you should always wrap work this service in try...catch
| } | ||
|
|
||
| if (!$stockItemConfiguration->isManageStock()) { | ||
| //We No need to Manage Stock |
| if (null === $stockItemData) { | ||
| // Sku is not assigned to Stock | ||
| return null; | ||
| if ($this->defaultStockProvider->getId() !== $stockId |
There was a problem hiding this comment.
Why do we need $this->defaultStockProvider->getId() !== $stockId condition?
…gacyCatalogInventoryAtSourceItemsSavePlugin
…anagementAllowedForSku
depends on #1236