7

In magento2 i want to add "Add to cart" button with qty for every single product of Grouped Product.
Is it possible? How?
I see that the form is only one.
I also need to remove the standard "Add to cart" button after the form. See below attachment.

enter image description here

1

1 Answer 1

0

I already did this on my project

app/design/frontend/Custom/theme/Magento_GroupedProduct/layout/catalog_product_view_type_grouped.xml

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="product.info.addtocart" remove="true" />
</page>

app/design/frontend/Custom/theme/Magento_GroupedProduct/templates/product/view/type/grouped.phtml

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

// @codingStandardsIgnoreFile

/**
 * Grouped product data template
 *
 * @var $block \Magento\Catalog\Block\Product\View\BaseImage
 * @var $block \Magento\GroupedProduct\Block\Product\View\Type\Grouped
 */
?>
<?php $block->setPreconfiguredValue(); ?>
<?php $_product = $block->getProduct(); ?>
<?php $_associatedProducts = $block->getAssociatedProducts(); ?>
<?php $_hasAssociatedProducts = count($_associatedProducts) > 0; ?>

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$listBlock = $objectManager->get('\Magento\Catalog\Block\Product\ListProduct');

?>

<div class="table-wrapper grouped">
    <table class="table data grouped" id="super-product-table">
        <caption class="table-caption"><?= /* @escapeNotVerified */ __('Grouped product items') ?></caption>
        <thead>
        <tr>
            <th class="col sku" scope="col"><?= /* @escapeNotVerified */ __('SKU') ?></th>
            <th class="col name" scope="col"><?= /* @escapeNotVerified */ __('Product Name') ?></th>
            <th class="col qty" scope="col"><?= /* @escapeNotVerified */ __('Qty') ?></th>
            <th class="col price" scope="col"><?= /* @escapeNotVerified */ __('Price') ?></th>
            <th class="col button" scope="col"><?= /* @escapeNotVerified */ __('Add to cart') ?></th>
        </tr>
        </thead>

        <?php if ($_hasAssociatedProducts): ?>
            <tbody>
        <?php
            /** @var \Magento\Catalog\Model\Product $_item */
            foreach ($_associatedProducts as $_item):
         ?>
            <tr>
                <td data-th="<?= $block->escapeHtml(__('SKU')) ?>" class="col sku">
                    <strong class="product-item-name"><?= $block->escapeHtml($_item->getSku()) ?></strong>
                </td>
                <td data-th="<?= $block->escapeHtml(__('Product Name')) ?>" class="col name">
                    <strong class="product-item-name"><?= $block->escapeHtml($_item->getName()) ?></strong>
                </td>
                <td data-th="<?= $block->escapeHtml(__('Qty')) ?>" class="col qty">
                    <div class="control qty">
                        <form data-role="tocart-form" action="<?php echo $listBlock->getAddToCartUrl($_product); ?>" method="post" id="addtocart_<?= /* @escapeNotVerified */ $_item->getId() ?>">
                            <?php echo $block->getBlockHtml('formkey')?>

                            <input type="number" <?php if (!$_item->isSaleable()) : ?>disabled="disabled"<?php endif; ?>
                                name="super_group[<?= /* @escapeNotVerified */ $_item->getId() ?>]"
                                data-selector="super_group[<?= /* @escapeNotVerified */ $_item->getId() ?>]"
                                value="<?= /* @escapeNotVerified */ $_item->isSaleable() ? 1 : 0 ?>"
                                title="<?= /* @escapeNotVerified */ __('Qty') ?>"
                                class="input-text qty"
                                step="1" min="0"
                                data-validate="{'validate-grouped-qty':'#super-product-table'}"
                                data-errors-message-box="#validation-message-box"/>
                        </form>
                    </div>
                </td>
                <td data-th="<?= $block->escapeHtml(__('Price')) ?>" class="col price">
                    <?php if ($block->getCanShowProductPrice($_product)): ?>
                        <?php if ($block->getCanShowProductPrice($_item)): ?>
                            <?= /* @escapeNotVerified */ $block->getProductPrice($_item) ?>
                        <?php endif; ?>
                    <?php endif; ?>
                </td>
                <td data-th="<?= $block->escapeHtml(__('Add to cart')) ?>" class="col button">
                    <div class="actions">
                        <?php if ($_item->isSaleable()) : ?>
                        <button type="submit"
                                title="<?= /* @escapeNotVerified */ __('Add to Cart') ?>"
                                class="action primary tocart"
                                form="addtocart_<?= /* @escapeNotVerified */ $_item->getId() ?>">
                            <span><?= /* @escapeNotVerified */ __('Add to Cart') ?></span>
                        </button>
                        <?php else: ?>
                            <div class="stock unavailable" title="<?= /* @escapeNotVerified */ __('Availability') ?>">
                                <span><?= /* @escapeNotVerified */ __('Out of stock') ?></span>
                            </div>
                        <?php endif; ?>
                    </div>
                </td>
            </tr>
            <?php if ($block->getCanShowProductPrice($_product)
                && $block->getCanShowProductPrice($_item)
                && trim($block->getProductPriceHtml(
                    $_item,
                    \Magento\Catalog\Pricing\Price\TierPrice::PRICE_CODE
                ))): ?>
                <tr class="row-tier-price">
                    <td colspan="2">
                        <?= $block->getProductPriceHtml(
                            $_item,
                            \Magento\Catalog\Pricing\Price\TierPrice::PRICE_CODE
                        ) ?>
                    </td>
                </tr>
            <?php endif; ?>
        <?php endforeach; ?>
            </tbody>
        <?php else: ?>
        <tbody>
            <tr>
                <td class="unavailable"
                    colspan="<?php if ($_product->isSaleable()): ?>4<?php else : ?>3<?php endif; ?>">
                    <?= /* @escapeNotVerified */ __('No options of this product are available.') ?>
                </td>
            </tr>
        </tbody>
        <?php endif; ?>
    </table>
</div>
<div id="validation-message-box"></div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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