-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathGetIsManageStockForProduct.php
More file actions
101 lines (93 loc) · 3.33 KB
/
GetIsManageStockForProduct.php
File metadata and controls
101 lines (93 loc) · 3.33 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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\InventorySalesAdminUi\Model;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\InventoryConfigurationApi\Exception\SkuIsNotAssignedToStockException;
use Magento\InventorySalesAdminUi\Model\ResourceModel\GetAssignedStockIdsBySku;
use Magento\InventoryApi\Api\StockRepositoryInterface;
use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
/**
* Class to determine if a product has Manage Stock option on or off
*/
class GetIsManageStockForProduct
{
/**
* @var GetSalableQuantityDataBySku
*/
private $getSalableQuantityDataBySku;
/**
* @var StockRepositoryInterface
*/
private $stockRepository;
/**
* @var GetStockItemConfigurationInterface
*/
private $getStockItemConfiguration;
/**
* @param GetSalableQuantityDataBySku $getSalableQuantityDataBySku
* @param StockRepositoryInterface $stockRepository
* @param GetStockItemConfigurationInterface $getStockItemConfiguration
*/
public function __construct(
GetSalableQuantityDataBySku $getSalableQuantityDataBySku,
StockRepositoryInterface $stockRepository,
GetStockItemConfigurationInterface $getStockItemConfiguration
) {
$this->getSalableQuantityDataBySku = $getSalableQuantityDataBySku;
$this->stockRepository = $stockRepository;
$this->getStockItemConfiguration = $getStockItemConfiguration;
}
/**
* Determine if a product has Manage Stock option on or off
*
* @param string $sku
* @param string $websiteCode
* @return bool|null
* @throws LocalizedException
* @throws NoSuchEntityException
* @throws SkuIsNotAssignedToStockException
*/
public function execute(string $sku, string $websiteCode): ?bool
{
$isManageStock = null;
$stockIds = $this->getProductStockIds($sku);
foreach ($stockIds as $stockId) {
$stock = $this->stockRepository->get($stockId);
$salesChannels = $stock->getExtensionAttributes()->getSalesChannels();
foreach ($salesChannels as $salesChannel) {
if ($salesChannel->getType() === SalesChannelInterface::TYPE_WEBSITE
&& $salesChannel->getCode() === $websiteCode
) {
$stockItemConfiguration = $this->getStockItemConfiguration->execute($sku, $stockId);
$isManageStock = $stockItemConfiguration->isManageStock();
}
}
}
return $isManageStock;
}
/**
* Return product stock ids
*
* @param string $sku
* @return array
* @throws LocalizedException
* @throws NoSuchEntityException
* @throws SkuIsNotAssignedToStockException
* @throws \Magento\Framework\Exception\InputException
*/
private function getProductStockIds(string $sku): array
{
$stockIds = [];
$stocksInfo = $this->getSalableQuantityDataBySku->execute($sku);
foreach ($stocksInfo as $stockInfo) {
$stockIds[] = (int)$stockInfo['stock_id'];
}
return $stockIds;
}
}