Question

I have two simple products associated with a group product at the product description page and have a separate add to cart button with each product I want to add these products to the cart individually But I am unable to When I press the first product to add to cart button both products are added to the cart. You can check the scenario by the following this link

this is my **group.phtml** file

   <?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 $grouped = $_product->load($_product->getId()); ?>                    
<?php $associatedProducts = $grouped->getTypeInstance()->getAssociatedProducts($grouped); ?>

<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 item" scope="col"><?= /* @escapeNotVerified */ __('Product Name') ?></th>
            <?php if ($_product->isSaleable()): ?>
                <th class="col qty" scope="col"><?= /* @escapeNotVerified */ __('Qty') ?></th>
            <?php endif; ?>
        </tr>
        </thead>

        <?php if ($_hasAssociatedProducts): ?>
        <?php foreach ($associatedProducts as $_item): ?>
        <tbody>
            <tr>
                <td data-th="<?= $block->escapeHtml(__('Product Name')) ?>" class="col item">
                    <strong class="product-item-name"><?= $block->escapeHtml($_item->getName()) ?></strong>
                    <?php if ($block->getCanShowProductPrice($_product)): ?>
                        <?php if ($block->getCanShowProductPrice($_item)): ?>
                            <?= /* @escapeNotVerified */ $block->getProductPrice($_item) ?>
                        <?php endif; ?>
                     <?php endif; ?>
                </td>
                <?php if ($_product->isSaleable()): ?>
                <td data-th="<?= $block->escapeHtml(__('Qty')) ?>" class="col qty">
                <?php if ($_item->isSaleable()) : ?>
                    <div class="control qty">
                         <form data-role="tocart-form" action="<?php echo $this->getAddToCartUrl($_item); ?>" method="post"> 
    <?php echo $block->getBlockHtml('formkey')?>
                        <input type="number"
                               name="super_group[<?= /* @escapeNotVerified */ $_item->getId() ?>]"
                               data-selector="super_group[<?= /* @escapeNotVerified */ $_item->getId() ?>]"
                               value="<?= /* @escapeNotVerified */ $_item->getQty() * 1 ?>"
                               title="<?= /* @escapeNotVerified */ __('Qty') ?>"
                               class="input-text qty"
                               data-validate="{'validate-grouped-qty':'#super-product-table'}"
                               data-errors-message-box="#validation-message-box"/>

   <button title="Add to Cart" class="addtocart" onclick="window.forms.product_addtocart_form_6749.submit()">
   Add to Cart
</button>

 </form>
 <form action="https://magento2.petpanic.gr/checkout/cart/add/uenc/aHR0cHM6Ly9tYWdlbnRvMi5wZXRwYW5pYy5nci9za3Vsb2kvYW50aXBhcmFzaXRpa2EtZ2lhLXNrdWxpYS9hbXBvdWxlcy9hbXBvdWxlcy1za3Vsb3UtYW50aXBobGVib3RvbWUtc3BvdC1vbi5odG1s/product/6749/" method="post" id="product_addtocart_form_6749" novalidate="novalidate">
    <input type="hidden" name="product" value="6749">
    <input type="hidden" name="related_product" id="related-products-field" value="">
    <input name="form_key" type="hidden" value="WZFyhGEmonXLbFmp">   
</form>

                   <!--button type="button" onclick="setLocation('<?php //echo $this->getAddToCartUrl($_item) ?>' + 'qty/' + $(this).previous().value)">Add to cart</button-->
                   </div>

                <?php else: ?>
                    <div class="stock unavailable" title="<?= /* @escapeNotVerified */ __('Availability') ?>">
                        <span><?= /* @escapeNotVerified */ __('Out of stock') ?></span>
                    </div>
                <?php endif; ?>
                </td>
                <td class="addtocart">
                <?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$listBlock = $objectManager->get('\Magento\Catalog\Block\Product\ListProduct');
$addToCartUrl =  $listBlock->getAddToCartUrl($_item);
?>
                    <?php /*
                      <form data-role="tocart-form" action="<?php echo $addToCartUrl; ?>" method="post"> 
    <?php echo $block->getBlockHtml('formkey')?>
        <button type="submit" title="Add to Cart" class="addtocart">
           Add to Cart
        </button>

 </form> */ ?>
</td>

                <?php endif; ?>
            </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; ?>
        </tbody>
        <?php endforeach; ?>
        <?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>

I am currently new in Magento, also I tried many solutions but nothing gonna work for me. If there is any solution for this please let me know! Thanks in advanced!

Was it helpful?

Solution

Try below code. It should work.

<?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 $grouped = $_product->load($_product->getId()); ?>                    
<?php $associatedProducts = $grouped->getTypeInstance()->getAssociatedProducts($grouped); ?>

</form>

<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 item" scope="col"><?= /* @escapeNotVerified */ __('Product Name') ?></th>
                <?php if ($_product->isSaleable()): ?>
                    <th class="col qty" scope="col"><?= /* @escapeNotVerified */ __('Qty') ?></th>
                <?php endif; ?>
            </tr>
        </thead>

        <?php if ($_hasAssociatedProducts): ?>
        <?php foreach ($associatedProducts as $_item): ?>
        <tbody>
            <tr>
                <td data-th="<?= $block->escapeHtml(__('Product Name')) ?>" class="col item">
                    <strong class="product-item-name"><?= $block->escapeHtml($_item->getName()) ?></strong>
                    <?php if ($block->getCanShowProductPrice($_product)): ?>
                        <?php if ($block->getCanShowProductPrice($_item)): ?>
                            <?= /* @escapeNotVerified */ $block->getProductPrice($_item) ?>
                        <?php endif; ?>
                     <?php endif; ?>
                </td>
                <?php if ($_product->isSaleable()): ?>
                    <td data-th="<?= $block->escapeHtml(__('Qty')) ?>" class="col qty">
                        <?php if ($_item->isSaleable()) : ?>
                            <div class="control qty">
                                <form data-role="tocart-form" action="<?php echo $this->getAddToCartUrl($_item); ?>" method="post">
                                    <input type="hidden" name="product" value="<?php echo $_item->getId(); ?>">
                                    <input type="hidden" name="related_product" id="related-products-field" value="">
                                    <input type="number"
                                       name="qty"
                                       value="<?= /* @escapeNotVerified */ $_item->getQty() * 1 ?>"
                                       title="<?= /* @escapeNotVerified */ __('Qty') ?>"
                                       class="input-text qty required"
                                       />

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

                                    <!--
                                    <input type="number"
                                       name="super_group[<?= /* @escapeNotVerified */ $_item->getId() ?>]"
                                       data-selector="super_group[<?= /* @escapeNotVerified */ $_item->getId() ?>]"
                                       value="<?= /* @escapeNotVerified */ $_item->getQty() * 1 ?>"
                                       title="<?= /* @escapeNotVerified */ __('Qty') ?>"
                                       class="input-text qty"
                                       data-validate="{'validate-grouped-qty':'#super-product-table'}"
                                       data-errors-message-box="#validation-message-box"/>
                                    -->
                                    <button type="submit" title="Add to Cart" class="addtocart">Add to Cart</button>
                                </form>
                            </div>

                        <?php else: ?>
                            <div class="stock unavailable" title="<?= /* @escapeNotVerified */ __('Availability') ?>">
                                <span><?= /* @escapeNotVerified */ __('Out of stock') ?></span>
                            </div>
                        <?php endif; ?>
                    </td>
                    <td class="addtocart">
                    </td>
                <?php endif; ?>
            </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; ?>
        </tbody>
        <?php endforeach; ?>
        <?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>

<form>

Please note that this code will not add group product to cart, only simple product associated with group will be added as per your theme.

OTHER TIPS

As far I Understood by looking at your shared link, you have set the default quantity of the simple products to 1 of Group product. In that case when user click on Add to cart button it will automatically add all simple product products of grouped product. First you need to set it to 0. So it will add only those simple products which has quantity greater than 0 when user click on the Add to Cart button. For Reference please see the screenshot

enter image description here

It is not possible or I would rather say against the nature of magento to add simple products of grouped product in isolation. if you want to treat simple product individually why you made a part of these product as a grouped product then there is no meaning of grouped product.

Or if you really want to do that, replace my code with your one

<?php foreach ($_associatedProducts as $_item): ?>
    <tbody>
        <tr>
            <td data-th="<?= $block->escapeHtml(__('Product Name')) ?>" class="col item">
                <strong class="product-item-name"><?= $block->escapeHtml($_item->getName()) ?></strong>
                <?php if ($block->getCanShowProductPrice($_product)): ?>
                    <?php if ($block->getCanShowProductPrice($_item)): ?>
                        <?= /* @escapeNotVerified */ $block->getProductPrice($_item) ?>
                    <?php endif; ?>
                 <?php endif; ?>
            </td>
            <?php if ($_product->isSaleable()): ?>
            <td data-th="<?= $block->escapeHtml(__('Qty')) ?>" class="col qty">
            <?php if ($_item->isSaleable()) : ?>
                <div class="control qty">
                    <input type="number"
                           name="super_group[<?= /* @escapeNotVerified */ $_item->getId() ?>]"
                           data-selector="super_group[<?= /* @escapeNotVerified */ $_item->getId() ?>]"
                           value="<?= /* @escapeNotVerified */ $_item->getQty() * 1 ?>"
                           title="<?= /* @escapeNotVerified */ __('Qty') ?>"
                           class="input-text qty"
                           data-validate="{'validate-grouped-qty':'#super-product-table'}"
                           data-errors-message-box="#validation-message-box"/>
                </div>
 <button type="button" onclick="window.location='<?php echo $this->getAddToCartUrl($_item) ?>' + 'qty/' + jQuery(this).prev().children().val()">Add to cart</button>
            <?php else: ?>
                <div class="stock unavailable" title="<?= /* @escapeNotVerified */ __('Availability') ?>">
                    <span><?= /* @escapeNotVerified */ __('Out of stock') ?></span>
                </div>
            <?php endif; ?>
            </td>
            <?php endif; ?>
        </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; ?>
    </tbody>
    <?php endforeach; ?>

I hope you got my point and it will help

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top