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.

6

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 :)

  • 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 '17 at 17:30
  • I have not been able to update cart with this line present in the phtml – Witalo Benicio Jul 27 '17 at 15:38
  • Do you now how can i programatically update the value of input – Witalo Benicio Jul 27 '17 at 17:29
  • @Amit Bera can you please help me on this magento.stackexchange.com/questions/201931/… – Amith Nov 17 '17 at 9:19
0
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>

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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