Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProcessRegisterProductsSalePlugin.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
19 
24 {
28  private $getSkusByProductIds;
29 
33  private $websiteRepository;
34 
38  private $stockResolver;
39 
43  private $checkItemsQuantity;
44 
48  private $getProductTypesBySkus;
49 
53  private $isSourceItemManagementAllowedForProductType;
54 
63  public function __construct(
64  GetSkusByProductIdsInterface $getSkusByProductIds,
65  WebsiteRepositoryInterface $websiteRepository,
66  StockResolverInterface $stockResolver,
67  CheckItemsQuantity $checkItemsQuantity,
68  GetProductTypesBySkusInterface $getProductTypesBySkus,
69  IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType
70  ) {
71  $this->getSkusByProductIds = $getSkusByProductIds;
72  $this->websiteRepository = $websiteRepository;
73  $this->stockResolver = $stockResolver;
74  $this->checkItemsQuantity = $checkItemsQuantity;
75  $this->getProductTypesBySkus = $getProductTypesBySkus;
76  $this->isSourceItemManagementAllowedForProductType = $isSourceItemManagementAllowedForProductType;
77  }
78 
89  public function aroundRegisterProductsSale(
91  callable $proceed,
92  $items,
93  $websiteId = null
94  ) {
95  if (empty($items)) {
96  return [];
97  }
98  if (null === $websiteId) {
99  throw new LocalizedException(__('$websiteId parameter is required'));
100  }
101 
102  $productSkus = $this->getSkusByProductIds->execute(array_keys($items));
103  $productTypes = $this->getProductTypesBySkus->execute($productSkus);
104 
105  $itemsBySku = [];
106  foreach ($productSkus as $productId => $sku) {
107  if (false === $this->isSourceItemManagementAllowedForProductType->execute($productTypes[$sku])) {
108  continue;
109  }
110  $itemsBySku[$sku] = $items[$productId];
111  }
112 
113  $websiteCode = $this->websiteRepository->getById($websiteId)->getCode();
114  $stockId = $this->stockResolver->execute(SalesChannelInterface::TYPE_WEBSITE, $websiteCode)->getStockId();
115  $this->checkItemsQuantity->execute($itemsBySku, $stockId);
116  return [];
117  }
118 }
aroundRegisterProductsSale(RegisterProductSaleInterface $subject, callable $proceed, $items, $websiteId=null)
__()
Definition: __.php:13
__construct(GetSkusByProductIdsInterface $getSkusByProductIds, WebsiteRepositoryInterface $websiteRepository, StockResolverInterface $stockResolver, CheckItemsQuantity $checkItemsQuantity, GetProductTypesBySkusInterface $getProductTypesBySkus, IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType)
if(!isset($_GET['website_code'])) $websiteCode
Definition: website.php:11
$items