From 93114edc28b859c5063799ea2d6c5cbcf16a9aca Mon Sep 17 00:00:00 2001 From: Danilo Argentiero Date: Sat, 9 Jun 2018 15:48:24 +0200 Subject: [PATCH 1/2] Fix for issue #717 --- .../BackOrderNotifyCustomerCondition.php | 93 +++++++++++++++++++ .../StockState/CheckQuoteItemQtyPlugin.php | 19 +++- 2 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 app/code/Magento/InventorySales/Model/IsProductSalableCondition/BackOrderNotifyCustomerCondition.php diff --git a/app/code/Magento/InventorySales/Model/IsProductSalableCondition/BackOrderNotifyCustomerCondition.php b/app/code/Magento/InventorySales/Model/IsProductSalableCondition/BackOrderNotifyCustomerCondition.php new file mode 100644 index 0000000000000..d8d1538b37898 --- /dev/null +++ b/app/code/Magento/InventorySales/Model/IsProductSalableCondition/BackOrderNotifyCustomerCondition.php @@ -0,0 +1,93 @@ +getStockItemConfiguration = $getStockItemConfiguration; + $this->getStockItemData = $getStockItemData; + $this->productSalableResultFactory = $productSalableResultFactory; + $this->productSalabilityErrorFactory = $productSalabilityErrorFactory; + } + + /** + * @inheritdoc + */ + public function execute(string $sku, int $stockId, float $requestedQty): ProductSalableResultInterface + { + $stockItemConfiguration = $this->getStockItemConfiguration->execute($sku, $stockId); + if (null === $stockItemConfiguration) { + $this->productSalableResultFactory->create(['errors' => []]); + } + + if ($stockItemConfiguration->getBackorders() === StockItemConfigurationInterface::BACKORDERS_YES_NOTIFY) { + $stockItemData = $this->getStockItemData->execute($sku, $stockId); + if (null === $stockItemData) { + $this->productSalableResultFactory->create(['errors' => []]); + } + + $backOrderQty = $requestedQty - $stockItemData[GetStockItemDataInterface::QUANTITY]; + if ($backOrderQty > 0) { + $errors = [ + $this->productSalabilityErrorFactory->create([ + 'code' => 'back_order-not-enough', + 'message' => __( + 'We don\'t have as many quantity as you requested, ' + . 'but we\'ll back order the remaining %1.', + $backOrderQty * 1 + )] + ) + ]; + return $this->productSalableResultFactory->create(['errors' => $errors]); + } + } + + return $this->productSalableResultFactory->create(['errors' => []]); + } +} diff --git a/app/code/Magento/InventorySales/Plugin/StockState/CheckQuoteItemQtyPlugin.php b/app/code/Magento/InventorySales/Plugin/StockState/CheckQuoteItemQtyPlugin.php index d758ceba4351c..0b050d86cdfba 100644 --- a/app/code/Magento/InventorySales/Plugin/StockState/CheckQuoteItemQtyPlugin.php +++ b/app/code/Magento/InventorySales/Plugin/StockState/CheckQuoteItemQtyPlugin.php @@ -14,6 +14,7 @@ use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\Locale\FormatInterface; use Magento\InventoryCatalogApi\Model\GetSkusByProductIdsInterface; +use Magento\InventorySales\Model\IsProductSalableCondition\BackOrderNotifyCustomerCondition; use Magento\InventorySales\Model\IsProductSalableForRequestedQtyCondition\ProductSalabilityError; use Magento\InventorySalesApi\Api\Data\SalesChannelInterface; use Magento\InventorySalesApi\Api\IsProductSalableForRequestedQtyInterface; @@ -52,6 +53,11 @@ class CheckQuoteItemQtyPlugin */ private $storeManager; + /** + * @var BackOrderNotifyCustomerCondition + */ + private $backOrderNotifyCustomerCondition; + /** * @param ObjectFactory $objectFactory * @param FormatInterface $format @@ -59,6 +65,7 @@ class CheckQuoteItemQtyPlugin * @param GetSkusByProductIdsInterface $getSkusByProductIds * @param StockResolverInterface $stockResolver * @param StoreManagerInterface $storeManager + * @param BackOrderNotifyCustomerCondition $backOrderNotifyCustomerCondition */ public function __construct( ObjectFactory $objectFactory, @@ -66,7 +73,8 @@ public function __construct( IsProductSalableForRequestedQtyInterface $isProductSalableForRequestedQty, GetSkusByProductIdsInterface $getSkusByProductIds, StockResolverInterface $stockResolver, - StoreManagerInterface $storeManager + StoreManagerInterface $storeManager, + BackOrderNotifyCustomerCondition $backOrderNotifyCustomerCondition ) { $this->objectFactory = $objectFactory; $this->format = $format; @@ -74,6 +82,7 @@ public function __construct( $this->getSkusByProductIds = $getSkusByProductIds; $this->stockResolver = $stockResolver; $this->storeManager = $storeManager; + $this->backOrderNotifyCustomerCondition = $backOrderNotifyCustomerCondition; } /** @@ -121,6 +130,14 @@ public function aroundCheckQuoteItemQty( } } + $productSalableResult = $this->backOrderNotifyCustomerCondition->execute($productSku, (int)$stockId, $qty); + if ($productSalableResult->getErrors()) { + /** @var ProductSalabilityError $error */ + foreach ($productSalableResult->getErrors() as $error) { + $result->setMessage($error->getMessage()); + } + } + return $result; } From 9818a9b8978fde4cf7f03f058c4b46ffa3443451 Mon Sep 17 00:00:00 2001 From: Francesco Pisello Date: Sun, 10 Jun 2018 09:49:17 +0200 Subject: [PATCH 2/2] Fix phpcs --- .../BackOrderNotifyCustomerCondition.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/InventorySales/Model/IsProductSalableCondition/BackOrderNotifyCustomerCondition.php b/app/code/Magento/InventorySales/Model/IsProductSalableCondition/BackOrderNotifyCustomerCondition.php index d8d1538b37898..ab2d09ea32ce9 100644 --- a/app/code/Magento/InventorySales/Model/IsProductSalableCondition/BackOrderNotifyCustomerCondition.php +++ b/app/code/Magento/InventorySales/Model/IsProductSalableCondition/BackOrderNotifyCustomerCondition.php @@ -48,8 +48,7 @@ public function __construct( GetStockItemDataInterface $getStockItemData, ProductSalableResultInterfaceFactory $productSalableResultFactory, ProductSalabilityErrorInterfaceFactory $productSalabilityErrorFactory - ) - { + ) { $this->getStockItemConfiguration = $getStockItemConfiguration; $this->getStockItemData = $getStockItemData; $this->productSalableResultFactory = $productSalableResultFactory; @@ -81,8 +80,7 @@ public function execute(string $sku, int $stockId, float $requestedQty): Product 'We don\'t have as many quantity as you requested, ' . 'but we\'ll back order the remaining %1.', $backOrderQty * 1 - )] - ) + )]) ]; return $this->productSalableResultFactory->create(['errors' => $errors]); }