Skip to content

Commit

Permalink
adjust to add quty limits messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas committed Sep 29, 2021
1 parent f35c4d6 commit c204373
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 29 deletions.
9 changes: 7 additions & 2 deletions Block/Product/View.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ public function getMaxQty($product)
{
$stockItem = $this->stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId());
$maxSaleQty = $stockItem->getMaxSaleQty();
return $maxSaleQty > 0 ? $maxSaleQty : 10000000;
if ($maxSaleQty > 0 && $maxSaleQty < 100000) {
$product->addData(['max_sale_qty' => $maxSaleQty]);
} else {
$maxSaleQty = 100000;
}
return $maxSaleQty;
}

/**
Expand All @@ -35,7 +40,7 @@ public function getQtyStep($product)
{
$stockItem = $this->stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId());
$qtyStep = 1;
if($stockItem->getEnableQtyIncrements()) {
if ($stockItem->getEnableQtyIncrements()) {
$qtyStep = $stockItem->getQtyIncrements();
}
return $qtyStep;
Expand Down
Empty file modified README.md
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion composer.json
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "proxi-blue/hyva-qtyinput",
"version": "1.0.2",
"version": "1.0.3",
"description": "Enhance QTY input to honour min/max and increment values",
"type": "magento2-module",
"require": {
Expand Down
Empty file modified etc/module.xml
100644 → 100755
Empty file.
Empty file modified registration.php
100644 → 100755
Empty file.
72 changes: 46 additions & 26 deletions view/frontend/templates/product/view/quantity.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ $product = $block->getProduct();
$minQty = $block->getMinimalQty($product) * 1;
$maxQty = $block->getMaxQty($product) * 1;
$qtyStep = $block->getQtyStep($product) * 1;

$message = [];
if ($minQty > 0) {
$message[] = $escaper->escapeHtml(__('Minimum quantity of %1', $minQty));
}
if ($qtyStep > 0) {
$message[] = $escaper->escapeHtml(__('Quantity increment of %1', $qtyStep));
}
if ($product->getData('max_sale_qty') > 0) {
$message[] = $escaper->escapeHtml(__('Maximum quantity of %1', $maxQty));
}
?>

<script>
Expand Down Expand Up @@ -82,30 +91,41 @@ $qtyStep = $block->getQtyStep($product) * 1;
};
}
</script>
<div x-data="initQtyField()"
class="">
<?php if ($product->isSaleable()): ?>
<div class="mr-2">
<label for="qty[<?= (int)$product->getId() ?>]"
class="sr-only"
>
<?= $escaper->escapeHtml(__('Quantity')) ?>
</label>
<input name="qty"
@private-content-loaded.window="onGetCartData($dispatch)"
id="qty[<?= (int)$product->getId() ?>]"
form="product_addtocart_form"
type="number"
pattern-="[0-9]{0,4}"
:min="minQty"
:max="maxQty"
:value="qty"
:step="qtyStep"
class="form-input px-2 py-2 w-20 text-center"
x-model.number="qty"
@change="checkQtyRange($dispatch)"
/>
</div>
<div class="block mb-2">
<?php if (count($message) > 0): ?>
<ul style="list-style-type: disc;">
<?= __('This product has quantity limits:') ?>
<?php
foreach ($message as $qtyMessage):
?>
<li class="ml-4"><?= $qtyMessage ?></li>
<?php
endforeach;
?>
</ul>
<?php endif; ?>
</div>

<div x-data="initQtyField()"
class="inline w-1/3 mr-2">
<div class="inline mr-2">
<label for="qty[<?= (int)$product->getId() ?>]"
class="sr-only"
>
<?= $escaper->escapeHtml(__('Quantity')) ?>
</label>
<input name="qty"
@private-content-loaded.window="onGetCartData($dispatch)"
id="qty[<?= (int)$product->getId() ?>]"
form="product_addtocart_form"
type="number"
pattern-="[0-9]{0,4}"
:min="minQty"
:max="maxQty"
:value="qty"
:step="qtyStep"
class="form-input px-2 py-2 w-20 text-center"
x-model.number="qty"
@change="checkQtyRange($dispatch)"
/>
</div>
</div>

0 comments on commit c204373

Please sign in to comment.