Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProcessBackItemQtyPlugin.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
16 use Magento\InventorySalesApi\Api\Data\ItemToSellInterfaceFactory;
18 use Magento\InventorySalesApi\Api\Data\SalesChannelInterfaceFactory;
19 use Magento\InventorySalesApi\Api\Data\SalesEventInterfaceFactory;
22 
27 {
31  private $getSkusByProductIds;
32 
36  private $salesEventFactory;
37 
41  private $salesChannelFactory;
42 
46  private $itemsToSellFactory;
47 
51  private $websiteRepository;
52 
56  private $placeReservationsForSalesEvent;
57 
61  private $isSourceItemManagementAllowedForProductType;
62 
66  private $getProductTypesBySkus;
67 
78  public function __construct(
79  GetSkusByProductIdsInterface $getSkusByProductIds,
80  SalesEventInterfaceFactory $salesEventFactory,
81  SalesChannelInterfaceFactory $salesChannelFactory,
82  ItemToSellInterfaceFactory $itemsToSellFactory,
83  WebsiteRepositoryInterface $websiteRepository,
84  PlaceReservationsForSalesEventInterface $placeReservationsForSalesEvent,
85  IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType,
86  GetProductTypesBySkusInterface $getProductTypesBySkus
87  ) {
88  $this->getSkusByProductIds = $getSkusByProductIds;
89  $this->salesEventFactory = $salesEventFactory;
90  $this->salesChannelFactory = $salesChannelFactory;
91  $this->itemsToSellFactory = $itemsToSellFactory;
92  $this->websiteRepository = $websiteRepository;
93  $this->placeReservationsForSalesEvent = $placeReservationsForSalesEvent;
94  $this->isSourceItemManagementAllowedForProductType = $isSourceItemManagementAllowedForProductType;
95  $this->getProductTypesBySkus = $getProductTypesBySkus;
96  }
97 
109  public function aroundBackItemQty(
110  StockManagement $subject,
111  callable $proceed,
112  $productId,
113  $qty,
114  $scopeId = null
115  ): bool {
116  if (null === $scopeId) {
117  throw new LocalizedException(__('$scopeId is required'));
118  }
119 
120  try {
121  $productSku = $this->getSkusByProductIds->execute([$productId])[$productId];
122  } catch (NoSuchEntityException $e) {
130  return true;
131  }
132  $productType = $this->getProductTypesBySkus->execute([$productSku])[$productSku];
133 
134  if (true === $this->isSourceItemManagementAllowedForProductType->execute($productType)) {
135  $websiteCode = $this->websiteRepository->getById((int)$scopeId)->getCode();
136  $salesChannel = $this->salesChannelFactory->create([
137  'data' => [
139  'code' => $websiteCode
140  ]
141  ]);
142 
143  $salesEvent = $this->salesEventFactory->create([
144  'type' => 'back_item_qty',
145  'objectType' => 'legacy_stock_management_api',
146  'objectId' => 'none'
147  ]);
148 
149  $itemToSell = $this->itemsToSellFactory->create([
150  'sku' => $productSku,
151  'qty' => (float)$qty
152  ]);
153 
154  $this->placeReservationsForSalesEvent->execute([$itemToSell], $salesChannel, $salesEvent);
155  }
156 
157  return true;
158  }
159 }
aroundBackItemQty(StockManagement $subject, callable $proceed, $productId, $qty, $scopeId=null)
__construct(GetSkusByProductIdsInterface $getSkusByProductIds, SalesEventInterfaceFactory $salesEventFactory, SalesChannelInterfaceFactory $salesChannelFactory, ItemToSellInterfaceFactory $itemsToSellFactory, WebsiteRepositoryInterface $websiteRepository, PlaceReservationsForSalesEventInterface $placeReservationsForSalesEvent, IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType, GetProductTypesBySkusInterface $getProductTypesBySkus)
__()
Definition: __.php:13
if(!isset($_GET['website_code'])) $websiteCode
Definition: website.php:11