How to add multiple products to a cart in related products grid on product detail page using Qty Box?

The question:

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

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

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 MagentoFrameworkAppActionAction;


 <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 = MagentoFrameworkAppObjectManager::getInstance()->get('MagentoStoreModelStoreManagerInterface');?>
                                        <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>

Method 2

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

?>

<?php
switch ($type = $block->getType()) {

    case 'related-rule':
        if ($exist = $block->hasItems()) {
            $type = 'related';
            $class = $type;

            $image = 'related_products_list';
            $title = __('Related Products');
            $items = $block->getAllItems();
            $limit = $block->getPositionLimit();
            $shuffle = (int) $block->isShuffled();
            $canItemsAddToCart = $block->canItemsAddToCart();

            $showWishlist = true;
            $showCompare = true;
            $showCart = true;
            $templateType = null;
            $description = false;
        }
    break;

    case 'related':
        /** @var MagentoCatalogBlockProductProductListRelated $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 = true;
            $showCompare = true;
            $showCart = true;
            $templateType = null;
            $description = false;
        }
    break;

    case 'other':
    break;
}
?>

<?php if ($exist):?>

    <div class="block <?php /* @escapeNotVerified */ echo $class; ?>" data-mage-init='{"relatedProducts":{"relatedCheckbox":".related.checkbox"}}' data-limit="<?php /* @escapeNotVerified */ echo $limit; ?>" data-shuffle="<?php /* @escapeNotVerified */ echo $shuffle; ?>">

        <div class="block-title title">
            <strong id="block-<?php /* @escapeNotVerified */ echo $class?>-heading" role="heading" aria-level="2"><?php /* @escapeNotVerified */ echo $title; ?></strong>
        </div>

        <div class="block-content content" aria-labelledby="block-<?php /* @escapeNotVerified */ echo $class?>-heading">

            <table class="table table-striped">

                <thead>
                    <th>Image</th>
                    <th>Name</th>
                    <th>Price</th>
                    <th>Actions</th>
                </thead>

                <tboby>

                    <?php 
                    $iterator = 1; 
                    foreach ($items as $_item): 
                        $available = ''; 
                        if (!$_item->isComposite() && $_item->isSaleable() && $type == 'related'): 
                            if (!$_item->getRequiredOptions()): 
                                $available = 'related-available'; 
                            endif; 
                        endif; 
                        echo($iterator++ == 1) ? '<tr>' : '</tr><tr>' ?>

                        <td>
                            <?php /* @escapeNotVerified */ echo '<!-- ' . $image . '-->' ?>
                            <a href="<?php /* @escapeNotVerified */ echo $block->getProductUrl($_item) ?>" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" class="product photo product-item-photo">
                                <?php echo $block->getImage($_item, $image)->toHtml(); ?>
                            </a>
                        </td>

                        <td>
                            <strong class="product name product-item-name">
                                <a class="product-item-link" title="<?php echo $block->escapeHtml($_item->getName()) ?>" href="<?php /* @escapeNotVerified */ echo $block->getProductUrl($_item) ?>" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener">
                                <?php echo $block->escapeHtml($_item->getName()) ?>
                                </a>
                            </strong>
                        </td>

                        <td>
                            <?php /* @escapeNotVerified */ echo $block->getProductPrice($_item); ?>

                                <?php if ($templateType): ?>
                                    <?php echo $block->getReviewsSummaryHtml($_item, $templateType) ?>
                                <?php endif; ?>
                        </td>

                        <?php if ($showCart): ?>
                            <td>
                                <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>
                            </td>
                        <?php endif; ?>
                        <?php echo($iterator == count($items)+1) ? '</tr>' : '' ?>
                    <?php endforeach; ?>
                </tboby>
            </table>
        </div>
    </div>
<?php endif;?>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment