7

How can you add a Qty filed to the the add to cart form on the category listings page.

With the Luma theme the user only has the option to add one product to cart at a time from the product listings page:

enter image description here

Is there a way I can edit the form in my theme at: app/design/frontend/Holy/mytheme/Magento_Catalog/templates/product/list.phtml

<form data-role="tocart-form" action="<?php /* @escapeNotVerified */ echo $postParams['action']; ?>" method="post">
    <input type="hidden" name="product" value="<?php /* @escapeNotVerified */ echo $postParams['data']['product']; ?>">
    <input type="hidden" name="<?php /* @escapeNotVerified */ echo Action::PARAM_NAME_URL_ENCODED; ?>" value="<?php /* @escapeNotVerified */ echo $postParams['data'][Action::PARAM_NAME_URL_ENCODED]; ?>">
    <?php echo $block->getBlockHtml('formkey')?>
    <button type="submit"
            title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>"
            class="action tocart primary">
        <span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
    </button>
</form>

For now it would work even if I could just do this for simple products.

2 Answers 2

19

On this form just add below code

<input type="number"
name="qty"
id="qty"
maxlength="12"
value="<?php /* @escapeNotVerified */ echo '1' ?>"
title="<?php /* @escapeNotVerified */ echo __('Qty') ?>" class="input-text qty" />

and check what is happen :)

7
  • wow, that was much easier than I expected. Thanks! By chance do you know how to add the same for the related products on the product details page? In app/design/frontend/Holy/mytheme/Magento_Catalog/templates/product/list/items.phtml
    – Holly
    Apr 13, 2017 at 17:30
  • I have not been able to update cart with this line present in the phtml Jul 27, 2017 at 15:38
  • Do you now how can i programatically update the value of input Jul 27, 2017 at 17:29
  • @Amit Bera can you please help me on this magento.stackexchange.com/questions/201931/…
    – amith lal
    Nov 17, 2017 at 9:19
  • Do you know how can I add Qty in add to cart form on configurable Product Listings Page in Magento 2.3
    – kiran
    May 16, 2019 at 18:56
1
in the form of add to cart just add this code 

<?php echo $block->getBlockHtml('formkey')?>
<input type="number"
                       name="qty"
                       id="qty"
                       maxlength="12"
                       value="<?php /* @escapeNotVerified */ echo $block->getProductDefaultQty() * 1 ?>"
                       title="<?php /* @escapeNotVerified */ echo __('Qty') ?>" class="input-text qty form-control"
                       data-validate="<?php echo $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"
                       />
<button type="submit" title="<?php /* @escapeNotVerified */ echo __('Add to Cart') ?>" class="action btn-add-cart tocart">
<i class="fa fa-shopping-cart"></i>
<span class="text"><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
</button>
1
  • $block->getProductDefaultQty() & $block->getQuantityValidators() does not work on list page, otherwise this should be better answer. Oct 28, 2022 at 1:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.