Skip to content

Commit 17b5409

Browse files
authored
Optimize condition & prevent exception on specific product types
1 parent 27922f1 commit 17b5409

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

Plugin/Block/AbstractStockQtyPlugin.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Opengento\InventoryStockQty\Plugin\Block;
99

10+
use Magento\Catalog\Model\Product;
1011
use Magento\CatalogInventory\Block\Stockqty\AbstractStockqty;
1112
use Magento\Framework\Exception\InputException;
1213
use Magento\Framework\Exception\LocalizedException;
@@ -37,16 +38,9 @@ public function __construct(
3738
public function aroundIsMsgVisible(AbstractStockqty $subject, callable $proceed): bool
3839
{
3940
$product = $subject->getProduct();
40-
$sku = $product->getSku();
41-
$stockId = (int)$this->stockByWebsiteId->execute((int)$product->getStore()->getWebsiteId())->getStockId();
42-
$stockItemConfig = $this->getStockItemConfiguration->execute($sku, $stockId);
4341

44-
return $stockItemConfig->isManageStock()
45-
&& $this->isSourceItemManagementAllowedForProductType->execute($product->getTypeId())
46-
&& $this->isSalableQtyAvailableForDisplaying->execute(
47-
$this->getProductSalableQty->execute($sku, $stockId),
48-
$stockItemConfig
49-
);
42+
return $this->isSourceItemManagementAllowedForProductType->execute($product->getTypeId())
43+
&& $this->isSalableQtyAvailable($product);
5044
}
5145

5246
/**
@@ -65,4 +59,21 @@ public function aroundGetStockQtyLeft(AbstractStockqty $subject, callable $proce
6559
)->getStockId()
6660
);
6761
}
62+
63+
/**
64+
* @throws SkuIsNotAssignedToStockException
65+
* @throws InputException
66+
* @throws LocalizedException
67+
*/
68+
private function isSalableQtyAvailable(Product $product): bool
69+
{
70+
$sku = $product->getSku();
71+
$stockId = (int)$this->stockByWebsiteId->execute((int)$product->getStore()->getWebsiteId())->getStockId();
72+
$stockItemConfig = $this->getStockItemConfiguration->execute($sku, $stockId);
73+
74+
return $stockItemConfig->isManageStock() && $this->isSalableQtyAvailableForDisplaying->execute(
75+
$this->getProductSalableQty->execute($sku, $stockId),
76+
$stockItemConfig
77+
);
78+
}
6879
}

0 commit comments

Comments
 (0)