-
Notifications
You must be signed in to change notification settings - Fork 244
/
UpdateConfigurableProductParentStockStatus.php
89 lines (81 loc) · 2.84 KB
/
UpdateConfigurableProductParentStockStatus.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\InventoryConfigurableProduct\Plugin\InventoryApi;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\SourceItemsSaveInterface;
use Magento\InventoryCatalogApi\Model\GetProductIdsBySkusInterface;
use Magento\InventoryCatalogApi\Model\IsSingleSourceModeInterface;
use Magento\ConfigurableProduct\Model\Inventory\ChangeParentStockStatus;
use Magento\Store\Model\StoreManagerInterface;
/**
* Update configurable product parent stock status
*
* Update product status based on the available stock of the child product
*/
class UpdateConfigurableProductParentStockStatus
{
/**
* @var ChangeParentStockStatus
*/
private $changeParentStockStatus;
/**
* @var GetProductIdsBySkusInterface
*/
private $getProductIdsBySkus;
/**
* @var IsSingleSourceModeInterface
*/
private $isSingleSourceMode;
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* @param GetProductIdsBySkusInterface $getProductIdsBySkus
* @param ChangeParentStockStatus $changeParentStockStatus
* @param IsSingleSourceModeInterface $isSingleSourceMode
* @param StoreManagerInterface $storeManager
*/
public function __construct(
GetProductIdsBySkusInterface $getProductIdsBySkus,
ChangeParentStockStatus $changeParentStockStatus,
IsSingleSourceModeInterface $isSingleSourceMode,
StoreManagerInterface $storeManager
) {
$this->getProductIdsBySkus = $getProductIdsBySkus;
$this->changeParentStockStatus = $changeParentStockStatus;
$this->isSingleSourceMode = $isSingleSourceMode;
$this->storeManager = $storeManager;
}
/**
* Make configurable product out of stock if all its children out of stock
*
* @param SourceItemsSaveInterface $subject
* @param void $result
* @param SourceItemInterface[] $sourceItems
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterExecute(SourceItemsSaveInterface $subject, $result, array $sourceItems): void
{
$productIds = [];
if ($this->isSingleSourceMode->execute() && $this->storeManager->hasSingleStore()) {
foreach ($sourceItems as $sourceItem) {
$sku = $sourceItem->getSku();
try {
$productIds[] = (int)$this->getProductIdsBySkus->execute([$sku])[$sku];
} catch (NoSuchEntityException $e) {
$productIds = [];
}
}
if ($productIds) {
$this->changeParentStockStatus->execute($productIds);
}
}
}
}