From e54a8cf8c849829f3c76e629e34e310a65656f66 Mon Sep 17 00:00:00 2001 From: Lucas Date: Fri, 29 Oct 2021 01:42:00 +0000 Subject: [PATCH] refactor to viewModel and cleanup logic on maxQty --- .../View.php => ViewModel/ProductView.php | 34 ++++++++++++++----- view/frontend/layout/catalog_product_view.xml | 2 +- .../templates/product/view/quantity.phtml | 10 ++++-- 3 files changed, 34 insertions(+), 12 deletions(-) rename Block/Product/View.php => ViewModel/ProductView.php (52%) diff --git a/Block/Product/View.php b/ViewModel/ProductView.php similarity index 52% rename from Block/Product/View.php rename to ViewModel/ProductView.php index d3abbf9..e27c460 100755 --- a/Block/Product/View.php +++ b/ViewModel/ProductView.php @@ -1,4 +1,6 @@ - @@ -7,13 +9,31 @@ * with this source code in the file LICENSE. */ -namespace ProxiBlue\HyvaQtyInput\Block\Product; +namespace ProxiBlue\HyvaQtyInput\ViewModel; +use Magento\CatalogInventory\Api\StockRegistryInterface; +use Magento\Framework\View\Element\Block\ArgumentInterface; -class View extends \Magento\Catalog\Block\Product\View +class ProductView implements ArgumentInterface { /** - * Gets minimal sales quantity + * Although depricated, magento core is still using this: Magento\Catalog\Block\Product\AbstractProduct + * + * @var \Magento\CatalogInventory\Api\StockRegistryInterface + */ + protected $stockRegistry; + + /** + * ProductView constructor. + * + * @param StockRegistryInterface $stockRegistry + */ + public function __construct(StockRegistryInterface $stockRegistry) { + $this->stockRegistry = $stockRegistry; + } + + /** + * Gets max sales quantity * * @param \Magento\Catalog\Model\Product $product * @return Integer @@ -22,10 +42,8 @@ public function getMaxQty($product) { $stockItem = $this->stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId()); $maxSaleQty = $stockItem->getMaxSaleQty(); - if ($maxSaleQty > 0 && $maxSaleQty < 100000) { - $product->addData(['max_sale_qty' => $maxSaleQty]); - } else { - $maxSaleQty = 100000; + if($maxSaleQty == 0) { + $maxSaleQty = 10000; // a large default, as for some reason some result to 0, so this is a protection for max qty calculations } return $maxSaleQty; } diff --git a/view/frontend/layout/catalog_product_view.xml b/view/frontend/layout/catalog_product_view.xml index 123bfaa..bac38e9 100755 --- a/view/frontend/layout/catalog_product_view.xml +++ b/view/frontend/layout/catalog_product_view.xml @@ -14,7 +14,7 @@ - require(\ProxiBlue\HyvaQtyInput\ViewModel\ProductView::class); /** @var Product $product */ $product = $block->getProduct(); $minQty = $block->getMinimalQty($product) * 1; -$maxQty = $block->getMaxQty($product) * 1; -$qtyStep = $block->getQtyStep($product) * 1; +$maxQty = $productViewModel->getMaxQty($product) * 1; +$qtyStep = $productViewModel->getQtyStep($product) * 1; $message = []; if ($minQty > 1) { $message[] = $escaper->escapeHtml(__('Minimum quantity of %1', $minQty)); @@ -28,7 +32,7 @@ if ($minQty > 1) { if ($qtyStep > 1) { $message[] = $escaper->escapeHtml(__('Quantity increment of %1', $qtyStep)); } -if ($product->getData('max_sale_qty') > 0 && $product->getData('max_sale_qty') < 1000) { +if ($maxQty > 0 && $maxQty < 999) { // we don't want to display message if it is above 1000 as real world usage don't go there - need to be a config option! $message[] = $escaper->escapeHtml(__('Maximum quantity of %1', $maxQty)); } ?>