-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathAreProductsSalablePlugin.php
More file actions
75 lines (68 loc) · 2.57 KB
/
AreProductsSalablePlugin.php
File metadata and controls
75 lines (68 loc) · 2.57 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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\InventoryAdvancedCheckout\Plugin\Model;
use Magento\AdvancedCheckout\Model\AreProductsSalableForRequestedQtyInterface;
use Magento\AdvancedCheckout\Model\Data\IsProductsSalableForRequestedQtyResultFactory;
use Magento\AdvancedCheckout\Model\Data\ProductQuantity;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
/**
* Provides multi-sourcing capabilities for Advanced Checkout Order By SKU feature.
*/
class AreProductsSalablePlugin
{
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @var IsProductsSalableForRequestedQtyResultFactory
*/
private $isProductsSalableForRequestedQtyResultFactory;
/**
* @param ProductRepositoryInterface $productRepository
* @param IsProductsSalableForRequestedQtyResultFactory $isProductsSalableForRequestedQtyResultFactory
*/
public function __construct(
ProductRepositoryInterface $productRepository,
IsProductsSalableForRequestedQtyResultFactory $isProductsSalableForRequestedQtyResultFactory
) {
$this->productRepository = $productRepository;
$this->isProductsSalableForRequestedQtyResultFactory = $isProductsSalableForRequestedQtyResultFactory;
}
/**
* Get products salable status for given sku requests.
*
* @param AreProductsSalableForRequestedQtyInterface $subject
* @param callable $proceed
* @param ProductQuantity[] $productQuantities
* @param int $websiteId
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundExecute(
AreProductsSalableForRequestedQtyInterface $subject,
callable $proceed,
array $productQuantities,
int $websiteId
): array {
$result = [];
foreach ($productQuantities as $productQuantity) {
try {
$product = $this->productRepository->get($productQuantity->getSku());
$result[] = $this->isProductsSalableForRequestedQtyResultFactory->create(
['sku' => $product->getSku(), 'isSalable' => $product->isSalable()]
);
} catch (NoSuchEntityException $e) {
$result[] = $this->isProductsSalableForRequestedQtyResultFactory->create(
['sku' => $productQuantity->getSku(), 'isSalable' => false]
);
}
}
return $result;
}
}