I am trying to make quantity increment plus minus button on product listing page for magento 2. I have see php cuong made a good module but that only works on details page. How I can add functionality on listing page.
1 Answer
Add the below code to your list.phtml before the "Add to Cart" button.
<div class="field qty">
<div class="control qty-change">
<?php $id = "qty_".$_product->getId(); ?>
<button type="button" class="reduced items" onclick="var result = document.getElementById('<?= $id ?>'); var qty = result.value; if( !isNaN( qty ) && qty > 1 ) result.value--;return false;">-</button>
<input type="text"
name="qty"
id="<?= $id ?>"
value="1"
title="<?= /* @escapeNotVerified */ __('Qty') ?>"
class="input-text qty"
data-validate="<?= $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"
/>
<button type="button" class="increase items" onclick="var result = document.getElementById('<?= $id ?>'); var qty = result.value; if( !isNaN( qty )) result.value++;return false;">+</button>
</div>
</div>
I Hope this will work for you.