|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Bundle\Model\Inventory; |
| 9 | + |
| 10 | +use Magento\Bundle\Model\Product\Type; |
| 11 | +use Magento\CatalogInventory\Api\Data\StockItemInterface; |
| 12 | +use Magento\CatalogInventory\Api\StockConfigurationInterface; |
| 13 | +use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory; |
| 14 | +use Magento\CatalogInventory\Api\StockItemRepositoryInterface; |
| 15 | + |
| 16 | +/*** |
| 17 | + * Update stock status of bundle products based on children products stock status |
| 18 | + */ |
| 19 | +class ChangeParentStockStatus |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var Type |
| 23 | + */ |
| 24 | + private $bundleType; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var StockItemCriteriaInterfaceFactory |
| 28 | + */ |
| 29 | + private $criteriaInterfaceFactory; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var StockItemRepositoryInterface |
| 33 | + */ |
| 34 | + private $stockItemRepository; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var StockConfigurationInterface |
| 38 | + */ |
| 39 | + private $stockConfiguration; |
| 40 | + |
| 41 | + /** |
| 42 | + * @param StockItemCriteriaInterfaceFactory $criteriaInterfaceFactory |
| 43 | + * @param StockItemRepositoryInterface $stockItemRepository |
| 44 | + * @param StockConfigurationInterface $stockConfiguration |
| 45 | + * @param Type $bundleType |
| 46 | + */ |
| 47 | + public function __construct( |
| 48 | + StockItemCriteriaInterfaceFactory $criteriaInterfaceFactory, |
| 49 | + StockItemRepositoryInterface $stockItemRepository, |
| 50 | + StockConfigurationInterface $stockConfiguration, |
| 51 | + Type $bundleType |
| 52 | + ) { |
| 53 | + $this->bundleType = $bundleType; |
| 54 | + $this->criteriaInterfaceFactory = $criteriaInterfaceFactory; |
| 55 | + $this->stockItemRepository = $stockItemRepository; |
| 56 | + $this->stockConfiguration = $stockConfiguration; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Update stock status of bundle products based on children products stock status |
| 61 | + * |
| 62 | + * @param array $childrenIds |
| 63 | + * @return void |
| 64 | + */ |
| 65 | + public function execute(array $childrenIds): void |
| 66 | + { |
| 67 | + $parentIds = $this->bundleType->getParentIdsByChild($childrenIds); |
| 68 | + foreach (array_unique($parentIds) as $productId) { |
| 69 | + $this->processStockForParent((int)$productId); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Update stock status of bundle product based on children products stock status |
| 75 | + * |
| 76 | + * @param int $productId |
| 77 | + * @return void |
| 78 | + */ |
| 79 | + private function processStockForParent(int $productId): void |
| 80 | + { |
| 81 | + $stockItems = $this->getStockItems([$productId]); |
| 82 | + $parentStockItem = $stockItems[$productId] ?? null; |
| 83 | + if ($parentStockItem) { |
| 84 | + $childrenIsInStock = $this->isChildrenInStock($productId); |
| 85 | + if ($this->isNeedToUpdateParent($parentStockItem, $childrenIsInStock)) { |
| 86 | + $parentStockItem->setIsInStock($childrenIsInStock); |
| 87 | + $parentStockItem->setStockStatusChangedAuto(1); |
| 88 | + $this->stockItemRepository->save($parentStockItem); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Returns stock status of bundle product based on children stock status |
| 95 | + * |
| 96 | + * Returns TRUE if any of the following conditions is true: |
| 97 | + * - At least one product is in-stock in each required option |
| 98 | + * - Any product is in-stock (if all options are optional) |
| 99 | + * |
| 100 | + * @param int $productId |
| 101 | + * @return bool |
| 102 | + */ |
| 103 | + private function isChildrenInStock(int $productId) : bool |
| 104 | + { |
| 105 | + $childrenIsInStock = false; |
| 106 | + $childrenIds = $this->bundleType->getChildrenIds($productId, true); |
| 107 | + $stockItems = $this->getStockItems(array_merge(...array_values($childrenIds))); |
| 108 | + foreach ($childrenIds as $childrenIdsPerOption) { |
| 109 | + $childrenIsInStock = false; |
| 110 | + foreach ($childrenIdsPerOption as $id) { |
| 111 | + $stockItem = $stockItems[$id] ?? null; |
| 112 | + if ($stockItem && $stockItem->getIsInStock()) { |
| 113 | + $childrenIsInStock = true; |
| 114 | + break; |
| 115 | + } |
| 116 | + } |
| 117 | + if (!$childrenIsInStock) { |
| 118 | + break; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return $childrenIsInStock; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Check if parent item should be updated |
| 127 | + * |
| 128 | + * @param StockItemInterface $parentStockItem |
| 129 | + * @param bool $childrenIsInStock |
| 130 | + * @return bool |
| 131 | + */ |
| 132 | + private function isNeedToUpdateParent( |
| 133 | + StockItemInterface $parentStockItem, |
| 134 | + bool $childrenIsInStock |
| 135 | + ): bool { |
| 136 | + return $parentStockItem->getIsInStock() !== $childrenIsInStock && |
| 137 | + ($childrenIsInStock === false || $parentStockItem->getStockStatusChangedAuto()); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Get stock items for provided product IDs |
| 142 | + * |
| 143 | + * @param array $productIds |
| 144 | + * @return StockItemInterface[] |
| 145 | + */ |
| 146 | + private function getStockItems(array $productIds): array |
| 147 | + { |
| 148 | + $criteria = $this->criteriaInterfaceFactory->create(); |
| 149 | + $criteria->setScopeFilter($this->stockConfiguration->getDefaultScopeId()); |
| 150 | + $criteria->setProductsFilter(array_unique($productIds)); |
| 151 | + $stockItemCollection = $this->stockItemRepository->getList($criteria); |
| 152 | + |
| 153 | + $stockItems = []; |
| 154 | + foreach ($stockItemCollection->getItems() as $stockItem) { |
| 155 | + $stockItems[$stockItem->getProductId()] = $stockItem; |
| 156 | + } |
| 157 | + |
| 158 | + return $stockItems; |
| 159 | + } |
| 160 | +} |
0 commit comments