Skip to content

Commit

Permalink
refactor to viewModel and cleanup logic on maxQty
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas committed Oct 29, 2021
1 parent 27f4831 commit e54a8cf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
34 changes: 26 additions & 8 deletions Block/Product/View.php → ViewModel/ProductView.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

/*
* (c) Lucas van Staden <[email protected]>
Expand All @@ -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
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion view/frontend/layout/catalog_product_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<body>
<referenceBlock name="product.info.quantity" remove="true"/>
<referenceBlock name="product.info">
<block class="ProxiBlue\HyvaQtyInput\Block\Product\View"
<block class="Magento\Catalog\Block\Product\View"
as="product.info.quantity"
name="product.info.quantity.enhanced"
template="ProxiBlue_HyvaQtyInput::product/view/quantity.phtml"
Expand Down
10 changes: 7 additions & 3 deletions view/frontend/templates/product/view/quantity.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,27 @@ declare(strict_types=1);
use Magento\Catalog\Block\Product\View;
use Magento\Catalog\Model\Product;
use Magento\Framework\Escaper;
use Hyva\Theme\Model\ViewModelRegistry;

/** @var View $block */
/** @var Escaper $escaper */
/** @var ViewModelRegistry $viewModels */

$productViewModel = $viewModels->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));
}
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));
}
?>
Expand Down

0 comments on commit e54a8cf

Please sign in to comment.