Skip to content

Commit 616b243

Browse files
author
Mastiuhin Olexandr
committed
MC-17524: Child product is not addable to Cart from PLP if it does not have default source assigned
1 parent 83f5867 commit 616b243

File tree

2 files changed

+129
-175
lines changed

2 files changed

+129
-175
lines changed

app/code/Magento/GroupedCatalogInventory/Plugin/OutOfStockFilter.php

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,77 +10,52 @@
1010

1111
use Magento\Catalog\Model\Product;
1212
use Magento\CatalogInventory\Api\Data\StockStatusInterface;
13-
use Magento\CatalogInventory\Api\StockStatusCriteriaInterfaceFactory;
14-
use Magento\CatalogInventory\Api\StockStatusRepositoryInterface;
1513
use Magento\GroupedProduct\Model\Product\Type\Grouped;
14+
use Magento\CatalogInventory\Api\StockRegistryInterface;
15+
use Magento\Framework\DataObject;
1616

1717
/**
18-
* Removes out of stock products from cart candidates when appropriate
18+
* Removes out of stock products from cart candidates when appropriate.
1919
*/
2020
class OutOfStockFilter
2121
{
2222
/**
23-
* @var StockStatusRepositoryInterface
23+
* @var StockRegistryInterface
2424
*/
25-
private $stockStatusRepository;
25+
private $stockRegistry;
2626

2727
/**
28-
* @var StockStatusCriteriaInterfaceFactory
28+
* @param StockRegistryInterface $stockRegistry
2929
*/
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;
4233
}
4334

4435
/**
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.
4637
*
4738
* @param Grouped $subject
4839
* @param array|string $result
49-
* @param \Magento\Framework\DataObject $buyRequest
40+
* @param DataObject $buyRequest
5041
* @return string|array
5142
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
5243
*/
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+
{
5846
if (!is_array($result) && $result instanceof Product) {
5947
$result = [$result];
6048
}
6149

6250
// Only remove out-of-stock products if no quantities were specified
6351
if (is_array($result) && !empty($result) && !$buyRequest->getData('super_group')) {
64-
$productIds = [];
65-
$productIdMap = [];
66-
6752
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]);
8057
}
8158
}
82-
83-
unset($productIdMap);
8459
}
8560

8661
return $result;

0 commit comments

Comments
 (0)