4

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

5

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>
5
  • 1
    I guess something is missing in the code, please check and let me know because its disturbing my whole design and not working that well as expected. I think there is something wrong in the hidden fields that you have used. – Sanjay Chaudhary Nov 4 '16 at 4:52
  • 1
    My mistake, I just figured out you have revised the code . Those contants were missing and that was disturbing the whole code. Its working fine now. Thanks ! – Sanjay Chaudhary Nov 4 '16 at 4:59
  • @manish not worked for me. i assume that you added code in items.phtml. my Version 2.1.3. i am working on same thing – Jai Feb 2 '17 at 8:17
  • @Jai Has it been solved ? – Manish Goswami Feb 3 '17 at 6:31
  • yes its solved. – Jai Feb 3 '17 at 7:37
3
<?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 \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 = 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) ?>" 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) ?>">
                                <?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;?>
6
  • not worked for me. i assume that you added code in items.phtml. my Version 2.1.3. i am working on same thing. – Jai Feb 2 '17 at 8:16
  • @Jai yes I added in items.phtml file, make sure you change $showCart = true; – Sanjay Chaudhary Feb 2 '17 at 10:05
  • Add-to-cart is already working but Qty is not working not with above provided solution.and mine code of file is also different. But Qty is adding only 1 item instead of filled in QTY box. – Jai Feb 2 '17 at 10:10
  • Yes. I overwrite template and enable $showCart = true even thats working on my site from so many days. Now QTY to added there. I skipped 2 lines of solutuon becz thats creating erro.: const PARAM_NAME_BASE64_URL = 'r64'; const PARAM_NAME_URL_ENCODED = 'uenc'; – Jai Feb 2 '17 at 10:20
  • Addding line "use Magento\Framework\App\Action\Action; " also make no differnce. Can you share your complete items.php with me? so I can compare what I am missing. Thanks – Jai Feb 2 '17 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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