-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathGetBundleProductStockStatus.php
More file actions
151 lines (137 loc) · 5.35 KB
/
GetBundleProductStockStatus.php
File metadata and controls
151 lines (137 loc) · 5.35 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\InventoryBundleProduct\Model;
use Magento\Bundle\Api\Data\OptionInterface;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Framework\Exception\LocalizedException;
use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
use Magento\InventoryConfigurationApi\Exception\SkuIsNotAssignedToStockException;
use Magento\InventorySalesApi\Api\AreProductsSalableForRequestedQtyInterface;
use Magento\InventorySalesApi\Api\Data\IsProductSalableForRequestedQtyRequestInterfaceFactory;
use Magento\InventorySalesApi\Api\Data\IsProductSalableForRequestedQtyResultInterface;
/**
* Get bundle product stock status service.
*/
class GetBundleProductStockStatus
{
/**
* @var GetProductSelections
*/
private $getProductSelection;
/**
* @var AreProductsSalableForRequestedQtyInterface
*/
private $areProductsSalableForRequestedQty;
/**
* @var IsProductSalableForRequestedQtyRequestInterfaceFactory
*/
private $isProductSalableForRequestedQtyRequestFactory;
/**
* @var GetStockItemConfigurationInterface
*/
private $getStockItemConfiguration;
/**
* @param GetProductSelections $getProductSelection
* @param AreProductsSalableForRequestedQtyInterface $areProductsSalableForRequestedQty
* @param IsProductSalableForRequestedQtyRequestInterfaceFactory $isProductSalableForRequestedQtyRequestFactory
* @param GetStockItemConfigurationInterface $getStockItemConfiguration
*/
public function __construct(
GetProductSelections $getProductSelection,
AreProductsSalableForRequestedQtyInterface $areProductsSalableForRequestedQty,
IsProductSalableForRequestedQtyRequestInterfaceFactory $isProductSalableForRequestedQtyRequestFactory,
GetStockItemConfigurationInterface $getStockItemConfiguration
) {
$this->getProductSelection = $getProductSelection;
$this->areProductsSalableForRequestedQty = $areProductsSalableForRequestedQty;
$this->isProductSalableForRequestedQtyRequestFactory = $isProductSalableForRequestedQtyRequestFactory;
$this->getStockItemConfiguration = $getStockItemConfiguration;
}
/**
* Provides bundle product stock status.
*
* @param ProductInterface $product
* @param OptionInterface[] $bundleOptions
* @param int $stockId
*
* @return bool
* @throws LocalizedException
* @throws SkuIsNotAssignedToStockException
*/
public function execute(ProductInterface $product, array $bundleOptions, int $stockId): bool
{
//get non processed bundle product sku.
$stockItemConfiguration = $this->getStockItemConfiguration->execute($product->getDataByKey('sku'), $stockId);
if (!$stockItemConfiguration->getExtensionAttributes()->getIsInStock()) {
return false;
}
$isSalable = false;
foreach ($bundleOptions as $option) {
$hasSalable = false;
$results = $this->getAreSalableSelections($product, $option, $stockId);
foreach ($results as $result) {
if ($result->isSalable()) {
$hasSalable = true;
break;
}
}
if ($hasSalable) {
$isSalable = true;
}
if (!$hasSalable && $option->getRequired()) {
$isSalable = false;
break;
}
}
return $isSalable;
}
/**
* Get bundle product selection qty.
*
* @param Product $product
* @param int $stockId
* @return float
* @throws LocalizedException
* @throws SkuIsNotAssignedToStockException
*/
private function getRequestedQty(Product $product, int $stockId): float
{
if ((int)$product->getSelectionCanChangeQty()) {
$stockItemConfiguration = $this->getStockItemConfiguration->execute((string)$product->getSku(), $stockId);
return $stockItemConfiguration->getMinSaleQty();
}
return (float)$product->getSelectionQty();
}
/**
* Get are bundle product selections salable.
*
* @param ProductInterface $product
* @param OptionInterface $option
* @param int $stockId
* @return IsProductSalableForRequestedQtyResultInterface[]
* @throws LocalizedException
* @throws SkuIsNotAssignedToStockException
*/
private function getAreSalableSelections(ProductInterface $product, OptionInterface $option, int $stockId): array
{
$bundleSelections = $this->getProductSelection->execute($product, $option);
$skuRequests = [];
foreach ($bundleSelections->getItems() as $selection) {
if ((int)$selection->getStatus() === Status::STATUS_ENABLED) {
$skuRequests[] = $this->isProductSalableForRequestedQtyRequestFactory->create(
[
'sku' => (string)$selection->getSku(),
'qty' => $this->getRequestedQty($selection, $stockId),
]
);
}
}
return $this->areProductsSalableForRequestedQty->execute($skuRequests, $stockId);
}
}