Magento 2.1.0 – How to Add Multiple Products to Cart from Related Products Grid

magento2.1.0

I want to add Qty box in related products. It works when I add any related product but I need to add multiple product at a time so what should I do ?

enter image description here

Best Answer

1 :- In related product you only need to give $showCart = true; value in your template file then you will be able to add to cart related products.

2 :-The default catalog template file name is items.phtml.

3 :- But if you want to add Qty box then you need to add form there and need to use same action.Please use this code and customize as per your need and let me know if you have any query.

const PARAM_NAME_BASE64_URL = 'r64';
const PARAM_NAME_URL_ENCODED = 'uenc';
use Magento\Framework\App\Action\Action;


 <td>
      <div class="product actions product-item-actions">
                                    <?php if ($showCart): ?>
                                       <?php $postParams = $block->getAddToCartPostParams($_item);?>
                                       <form  data-role="tocart-form" action="<?php echo $block->getAddToCartUrl($_item); ?>" 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]; ?>">
                                        <div class="control">
                                            <input type="number" name="qty" id="qty" maxlength="12"
                                            style="width:150px;float: left;margin-right: 35px;"
                                            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())); ?>"
                                            />
                                        </div>
                                        <?php echo $block->getBlockHtml('formkey'); ?>
                                        <?php $storeManager = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Store\Model\StoreManagerInterface');?>
                                        <button type="submit"
                                        title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>"
                                        class="action tocart primary" style="float:left;">
                                        <span><?php /* @escapeNotVerified */echo __('Add to Cart'); ?></span>
                                    </button>
                                </form>