How to add Qty in add to cart form on Product Listings Page in Magento 2
Question
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:
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.
La solution
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 :)
Autres conseils
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>
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange