Magento – Magento 2 – Quantity Field on Related Products Listing

addtocartmagento2product-listrelated-productstemplate

I'm trying to add a quantity field to the related products section on my product page. I'm overriding module-catalog/view/frontend/templates/product/list/items.phtml in my theme. So far, here is my 'related' case section:

case 'related':

   /** @var \Magento\Catalog\Block\Product\ProductList\Related $block */
    if ($exist = $block->getItems()->getSize()) {
        $type = 'related';
        $class = $type;
        $image = 'related_products_list';
        $title = __('Related Products');
        $items = $block->getItems();
        $limit = 0;
        $shuffle = 0;
        $canItemsAddToCart = $block->canItemsAddToCart();
        $showWishlist = false;
        $showCompare = false;
        $showCart = true;
        $templateType = null;
        $description = false;
    }
 break;

I set $showCart to true so I can now see an add to cart button under the related product, and it is functioning by adding one item to the cart. I tried adding the following code above the button in items.phtml for the quantity field:

<div class="product-qty">
 <label><?php echo __('Quantity:'); ?></label>
 <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"
     data-validate="<?php echo $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"/>

Once I add this code, no matter what I put in the quantity field, it always adds one product. Is there something I'm missing for the add to cart button to add the quantity I enter in the quantity field?

Best Answer

Replace the content of

if ($showCart) ...... endif

with below code.

<?php if ($showCart): ?>

<div class="product actions product-item-actions">
    <form  data-role="tocart-form" action="<?php echo $block->getAddToCartUrl($_item); ?>" method="post">
    <div style="float: left;">
        <input type="number"
               name="qty"
               id="qty"
               style="width:50px;"
               maxlength="12"
               value="<?php /* @escapeNotVerified */ echo $block->getProductDefaultQty() * 1 ?>"
               title="<?php /* @escapeNotVerified */ echo __('Qty') ?>" class="input-text qty"
               data-validate="<?php echo $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"
               />

        <?php echo $block->getBlockHtml('formkey'); ?>

        <button type="submit" title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>" class="action tocart primary" style="float:right;margin-left: 10px;">
            <span><?php /* @escapeNotVerified */echo __('Add to Cart'); ?></span>
        </button>
    </div>
    </form>
</div>

It worked for me in magento version 2.1.3. Source post: How to add multiple products to a cart in related products grid on product detail page using Qty Box?

Hope it may help someone.