|
10 | 10 |
|
11 | 11 | use Magento\Catalog\Model\Product;
|
12 | 12 | use Magento\CatalogInventory\Api\Data\StockStatusInterface;
|
13 |
| -use Magento\CatalogInventory\Api\StockStatusCriteriaInterfaceFactory; |
14 |
| -use Magento\CatalogInventory\Api\StockStatusRepositoryInterface; |
15 | 13 | use Magento\GroupedProduct\Model\Product\Type\Grouped;
|
| 14 | +use Magento\CatalogInventory\Api\StockRegistryInterface; |
| 15 | +use Magento\Framework\DataObject; |
16 | 16 |
|
17 | 17 | /**
|
18 |
| - * Removes out of stock products from cart candidates when appropriate |
| 18 | + * Removes out of stock products from cart candidates when appropriate. |
19 | 19 | */
|
20 | 20 | class OutOfStockFilter
|
21 | 21 | {
|
22 | 22 | /**
|
23 |
| - * @var StockStatusRepositoryInterface |
| 23 | + * @var StockRegistryInterface |
24 | 24 | */
|
25 |
| - private $stockStatusRepository; |
| 25 | + private $stockRegistry; |
26 | 26 |
|
27 | 27 | /**
|
28 |
| - * @var StockStatusCriteriaInterfaceFactory |
| 28 | + * @param StockRegistryInterface $stockRegistry |
29 | 29 | */
|
30 |
| - private $criteriaInterfaceFactory; |
31 |
| - |
32 |
| - /** |
33 |
| - * @param StockStatusRepositoryInterface $stockStatusRepository |
34 |
| - * @param StockStatusCriteriaInterfaceFactory $criteriaInterfaceFactory |
35 |
| - */ |
36 |
| - public function __construct( |
37 |
| - StockStatusRepositoryInterface $stockStatusRepository, |
38 |
| - StockStatusCriteriaInterfaceFactory $criteriaInterfaceFactory |
39 |
| - ) { |
40 |
| - $this->stockStatusRepository = $stockStatusRepository; |
41 |
| - $this->criteriaInterfaceFactory = $criteriaInterfaceFactory; |
| 30 | + public function __construct(StockRegistryInterface $stockRegistry) |
| 31 | + { |
| 32 | + $this->stockRegistry = $stockRegistry; |
42 | 33 | }
|
43 | 34 |
|
44 | 35 | /**
|
45 |
| - * Removes out of stock products for requests that don't specify the super group |
| 36 | + * Removes out of stock products for requests that don't specify the super group. |
46 | 37 | *
|
47 | 38 | * @param Grouped $subject
|
48 | 39 | * @param array|string $result
|
49 |
| - * @param \Magento\Framework\DataObject $buyRequest |
| 40 | + * @param DataObject $buyRequest |
50 | 41 | * @return string|array
|
51 | 42 | * @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
52 | 43 | */
|
53 |
| - public function afterPrepareForCartAdvanced( |
54 |
| - Grouped $subject, |
55 |
| - $result, |
56 |
| - \Magento\Framework\DataObject $buyRequest |
57 |
| - ) { |
| 44 | + public function afterPrepareForCartAdvanced(Grouped $subject, $result, DataObject $buyRequest) |
| 45 | + { |
58 | 46 | if (!is_array($result) && $result instanceof Product) {
|
59 | 47 | $result = [$result];
|
60 | 48 | }
|
61 | 49 |
|
62 | 50 | // Only remove out-of-stock products if no quantities were specified
|
63 | 51 | if (is_array($result) && !empty($result) && !$buyRequest->getData('super_group')) {
|
64 |
| - $productIds = []; |
65 |
| - $productIdMap = []; |
66 |
| - |
67 | 52 | foreach ($result as $index => $cartItem) {
|
68 |
| - $productIds[] = $cartItem->getId(); |
69 |
| - $productIdMap[$cartItem->getId()] = $index; |
70 |
| - } |
71 |
| - |
72 |
| - $criteria = $this->criteriaInterfaceFactory->create(); |
73 |
| - $criteria->setProductsFilter($productIds); |
74 |
| - |
75 |
| - $stockStatusCollection = $this->stockStatusRepository->getList($criteria); |
76 |
| - foreach ($stockStatusCollection->getItems() as $status) { |
77 |
| - /** @var $status StockStatusInterface */ |
78 |
| - if ($status->getStockStatus() == StockStatusInterface::STATUS_OUT_OF_STOCK) { |
79 |
| - unset($result[$productIdMap[$status->getProductId()]]); |
| 53 | + $prodId = $cartItem->getId(); |
| 54 | + $productStockStatus = $this->stockRegistry->getProductStockStatus($prodId); |
| 55 | + if ($productStockStatus == StockStatusInterface::STATUS_OUT_OF_STOCK) { |
| 56 | + unset($result[$index]); |
80 | 57 | }
|
81 | 58 | }
|
82 |
| - |
83 |
| - unset($productIdMap); |
84 | 59 | }
|
85 | 60 |
|
86 | 61 | return $result;
|
|
0 commit comments