Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BackOrderNotifyCustomerCondition.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
12 use Magento\InventorySalesApi\Api\Data\ProductSalabilityErrorInterfaceFactory;
14 use Magento\InventorySalesApi\Api\Data\ProductSalableResultInterfaceFactory;
17 
22 {
26  private $getStockItemConfiguration;
27 
31  private $getStockItemData;
32 
36  private $productSalableResultFactory;
37 
41  private $productSalabilityErrorFactory;
42 
49  public function __construct(
50  GetStockItemConfigurationInterface $getStockItemConfiguration,
51  GetStockItemDataInterface $getStockItemData,
52  ProductSalableResultInterfaceFactory $productSalableResultFactory,
53  ProductSalabilityErrorInterfaceFactory $productSalabilityErrorFactory
54  ) {
55  $this->getStockItemConfiguration = $getStockItemConfiguration;
56  $this->getStockItemData = $getStockItemData;
57  $this->productSalableResultFactory = $productSalableResultFactory;
58  $this->productSalabilityErrorFactory = $productSalabilityErrorFactory;
59  }
60 
64  public function execute(string $sku, int $stockId, float $requestedQty): ProductSalableResultInterface
65  {
66  $stockItemConfiguration = $this->getStockItemConfiguration->execute($sku, $stockId);
67 
68  if ($stockItemConfiguration->getBackorders() === StockItemConfigurationInterface::BACKORDERS_YES_NOTIFY) {
69  $stockItemData = $this->getStockItemData->execute($sku, $stockId);
70  if (null === $stockItemData) {
71  return $this->productSalableResultFactory->create(['errors' => []]);
72  }
73 
74  $backOrderQty = $requestedQty - $stockItemData[GetStockItemDataInterface::QUANTITY];
75  if ($backOrderQty > 0) {
76  $errors = [
77  $this->productSalabilityErrorFactory->create([
78  'code' => 'back_order-not-enough',
79  'message' => __(
80  'We don\'t have as many quantity as you requested, '
81  . 'but we\'ll back order the remaining %1.',
82  $backOrderQty * 1
83  )])
84  ];
85  return $this->productSalableResultFactory->create(['errors' => $errors]);
86  }
87  }
88 
89  return $this->productSalableResultFactory->create(['errors' => []]);
90  }
91 }
__()
Definition: __.php:13
__construct(GetStockItemConfigurationInterface $getStockItemConfiguration, GetStockItemDataInterface $getStockItemData, ProductSalableResultInterfaceFactory $productSalableResultFactory, ProductSalabilityErrorInterfaceFactory $productSalabilityErrorFactory)
$errors
Definition: overview.phtml:9