1

I am creating multiple grouped products which include custom products attributes taken from each simple product associated which i have added to the table within grouped.phtml like so...

<td data-th="<?= $block->escapeHtml(__('Strap Length')) ?>" class="col item">
    <?= $my_attr = $this->getProduct()->getResource()->getAttributeRawValue($_item->getId(),'straplength',$_item->getId()); ?>
</td>

However not all the grouped products will need to contain the same data so I want to hide any reference of this attribute if all of the simple products do not contain any data within that attribute.

including hiding the title within the head of the table...

<th class="col length" scope="col"><?= /* @escapeNotVerified */ __('Strap Length') ?></th>
3
  • What happen if other products have values for that attribute? Jul 31 '18 at 14:53
  • If one or more simple product within a grouped product has data within the attribute then I would like it to show on the table, if none of the simple products have data within the attribute then I don't want to see anything related to that attribute on the table Jul 31 '18 at 15:01
  • Can you post your whole group.phtml code here? Jul 31 '18 at 16:00
0

You manage to work this by using the following code to your group.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; ?>

<div class="table-wrapper grouped">
<table class="table data grouped" id="super-product-table">
    <caption class="table-caption"><?= /* @escapeNotVerified */ __('Grouped product items') ?></caption>

    <?php if ($_hasAssociatedProducts): ?>
    <?php $showStrapwidthTxt = 0; // Flag set to 0 ?>
    <?php $showStraplengthTxt = 0; // Flag set to 0 ?>
    <?php $showBreakingstrainTxt = 0; // Flag set to 0 ?>
    <?php $showSystemstrengthTxt = 0; // Flag set to 0 ?>
    <?php foreach ($_associatedProducts as $_item): ?>
        <?php if($this->getProduct()->getResource()->getAttributeRawValue($_item->getId(),'strapwidth',$_item->getId())){ $showStrapwidthTxt = 1; } ?>
        <?php if($this->getProduct()->getResource()->getAttributeRawValue($_item->getId(),'straplength',$_item->getId())){ $showStraplengthTxt = 1; } ?>
        <?php if($this->getProduct()->getResource()->getAttributeRawValue($_item->getId(),'breakingstrain',$_item->getId())){ $showBreakingstrainTxt = 1; } ?>
        <?php if($this->getProduct()->getResource()->getAttributeRawValue($_item->getId(),'systemstrength',$_item->getId())){ $showSystemstrengthTxt = 1; } ?>
    <?php endforeach; ?>
    <?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>
            </td>

            <?php if($showStrapwidthTxt): ?>
                <td data-th="<?= $block->escapeHtml(__('Strap Width')) ?>" class="col item">
                    <?php if($my_attr = $this->getProduct()->getResource()->getAttributeRawValue($_item->getId(),'strapwidth',$_item->getId())): ?>
                        <?= $my_attr; ?>
                    <?php endif; ?>
                </td>
            <?php endif; ?>

            <?php if($showStraplengthTxt): ?>
                <td data-th="<?= $block->escapeHtml(__('Strap Length')) ?>" class="col item">
                    <?php if($my_attr = $this->getProduct()->getResource()->getAttributeRawValue($_item->getId(),'straplength',$_item->getId())): ?>
                        <?= $my_attr; ?>
                    <?php endif; ?>
                </td>
            <?php endif; ?>

            <?php if($showBreakingstrainTxt): ?>
                <td data-th="<?= $block->escapeHtml(__('Breaking Strain')) ?>" class="col item">
                    <?php if($my_attr = $this->getProduct()->getResource()->getAttributeRawValue($_item->getId(),'breakingstrain',$_item->getId())): ?>
                        <?= $my_attr; ?>
                    <?php endif; ?>
                </td>
            <?php endif; ?>

            <?php if($showSystemstrengthTxt): ?>
                <td data-th="<?= $block->escapeHtml(__('System Strength')) ?>" class="col item">
                    <?php if($my_attr = $this->getProduct()->getResource()->getAttributeRawValue($_item->getId(),'systemstrength',$_item->getId())): ?>
                        <?= $my_attr; ?>
                    <?php endif; ?>
                </td>
            <?php endif; ?>

            <td data-th="<?= $block->escapeHtml(__('Price')) ?>" class="col item">
                <?php if ($block->getCanShowProductPrice($_product)): ?>
                    <?php if ($block->getCanShowProductPrice($_item)): ?>
                        <?= /* @escapeNotVerified */ $block->getProductPrice($_item) ?>
                    <?php endif; ?>
                 <?php endif; ?>
            </td>
            <td data-th="<?= $block->escapeHtml(__('Multiple Discount')) ?>" class="col item">
                <?php if ($block->getCanShowProductPrice($_product)
                    && $block->getCanShowProductPrice($_item)
                    && trim($block->getProductPriceHtml(
                        $_item,
                        \Magento\Catalog\Pricing\Price\TierPrice::PRICE_CODE
                    ))): ?>

                            <?= $block->getProductPriceHtml(
                                $_item,
                                \Magento\Catalog\Pricing\Price\TierPrice::PRICE_CODE
                            ) ?>

                <?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>
            <?php else: ?>
                <div class="stock unavailable" title="<?= /* @escapeNotVerified */ __('Availability') ?>">
                    <span><?= /* @escapeNotVerified */ __('Out of stock') ?></span>
                </div>
            <?php endif; ?>
            </td>
            <?php endif; ?>
        </tr>
    </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; ?>
    <?php //Copied thead from top to bottom ?>
    <thead>
    <tr>
        <th class="col item" scope="col"><?= /* @escapeNotVerified */ __('Product Code') ?></th>

        <?php if($showStrapwidthTxt): ?>
            <th class="col length" scope="col"><?= /* @escapeNotVerified */ __('Strap Width') ?></th>
        <?php endif; ?>
        <?php if($showStraplengthTxt): ?>
            <th class="col length" scope="col"><?= /* @escapeNotVerified */ __('Strap Length') ?></th>
        <?php endif; ?>
        <?php if($showBreakingstrainTxt): ?>
            <th class="col length" scope="col"><?= /* @escapeNotVerified */ __('Breaking Strain') ?></th>
        <?php endif; ?>
        <?php if($showSystemstrengthTxt): ?>
            <th class="col length" scope="col"><?= /* @escapeNotVerified */ __('System Strength') ?></th>
        <?php endif; ?>
        <th class="col indi" scope="col"><?= /* @escapeNotVerified */ __('Individual Price') ?></th>
        <th class="col multi" scope="col"><?= /* @escapeNotVerified */ __('Multiple Discount') ?></th>
        <?php if ($_product->isSaleable()): ?>
            <th class="col qty" scope="col"><?= /* @escapeNotVerified */ __('Qty') ?></th>
        <?php endif; ?>
    </tr>
    </thead>
</table>
</div>
<div id="validation-message-box"></div>
4
  • Yes please. Is this working correctly for you? Aug 1 '18 at 8:15
  • That's great thanks! Just one thing, if one of the products within the group has an attribute and another doesn't it messes up my table... see sreenshot imgur.com/a/daJ9WbN Is there a way to fix this? Aug 1 '18 at 10:49
  • Can you update your group.phtml whole code? Aug 1 '18 at 10:51
  • Check my updated answer. Aug 1 '18 at 11:51
0
<?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; ?>

<div class="table-wrapper grouped">
<table class="table data grouped" id="super-product-table">
    <caption class="table-caption"><?= /* @escapeNotVerified */ __('Grouped product items') ?></caption>

    <?php if ($_hasAssociatedProducts): ?>
    <?php $showStrapwidthTxt = 0; // Flag set to 0 ?>
    <?php $showStraplengthTxt = 0; // Flag set to 0 ?>
    <?php $showBreakingstrainTxt = 0; // Flag set to 0 ?>
    <?php $showSystemstrengthTxt = 0; // Flag set to 0 ?>
    <?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>
            </td>
            <?php if($my_attr = $this->getProduct()->getResource()->getAttributeRawValue($_item->getId(),'strapwidth',$_item->getId())): ?>
            <?php $showStrapwidthTxt = 1; //Set flag to 1 ?>
                <td data-th="<?= $block->escapeHtml(__('Strap Width')) ?>" class="col item">
                <?= $my_attr; ?>
                </td>
            <?php endif; ?>
            <?php if($my_attr = $this->getProduct()->getResource()->getAttributeRawValue($_item->getId(),'straplength',$_item->getId())): ?>
            <?php $showStraplengthTxt = 1; //Set flag to 1 ?>
                <td data-th="<?= $block->escapeHtml(__('Strap Length')) ?>" class="col item">
                <?= $my_attr; ?>
                </td>
            <?php endif; ?>
            <?php if($my_attr = $this->getProduct()->getResource()->getAttributeRawValue($_item->getId(),'breakingstrain',$_item->getId())): ?>
            <?php $showBreakingstrainTxt = 1; //Set flag to 1 ?>
                <td data-th="<?= $block->escapeHtml(__('Breaking Strain')) ?>" class="col item">
                <?= $my_attr; ?>
                </td>
            <?php endif; ?>
            <?php if($my_attr = $this->getProduct()->getResource()->getAttributeRawValue($_item->getId(),'systemstrength',$_item->getId())): ?>
            <?php $showSystemstrengthTxt = 1; //Set flag to 1 ?>
                <td data-th="<?= $block->escapeHtml(__('System Strength')) ?>" class="col item">
                <?= $my_attr; ?>
                </td>
            <?php endif; ?>
            <td data-th="<?= $block->escapeHtml(__('Price')) ?>" class="col item">
                <?php if ($block->getCanShowProductPrice($_product)): ?>
                    <?php if ($block->getCanShowProductPrice($_item)): ?>
                        <?= /* @escapeNotVerified */ $block->getProductPrice($_item) ?>
                    <?php endif; ?>
                 <?php endif; ?>
            </td>
            <td data-th="<?= $block->escapeHtml(__('Multiple Discount')) ?>" class="col item">
                <?php if ($block->getCanShowProductPrice($_product)
                    && $block->getCanShowProductPrice($_item)
                    && trim($block->getProductPriceHtml(
                        $_item,
                        \Magento\Catalog\Pricing\Price\TierPrice::PRICE_CODE
                    ))): ?>

                            <?= $block->getProductPriceHtml(
                                $_item,
                                \Magento\Catalog\Pricing\Price\TierPrice::PRICE_CODE
                            ) ?>

                <?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>
            <?php else: ?>
                <div class="stock unavailable" title="<?= /* @escapeNotVerified */ __('Availability') ?>">
                    <span><?= /* @escapeNotVerified */ __('Out of stock') ?></span>
                </div>
            <?php endif; ?>
            </td>
            <?php endif; ?>
        </tr>
    </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; ?>
    <?php //Copied thead from top to bottom ?>
    <thead>
    <tr>
        <th class="col item" scope="col"><?= /* @escapeNotVerified */ __('Product Code') ?></th>

        <?php if($showStrapwidthTxt): ?>
            <th class="col length" scope="col"><?= /* @escapeNotVerified */ __('Strap Width') ?></th>
        <?php endif; ?>
        <?php if($showStraplengthTxt): ?>
            <th class="col length" scope="col"><?= /* @escapeNotVerified */ __('Strap Length') ?></th>
        <?php endif; ?>
        <?php if($showBreakingstrainTxt): ?>
            <th class="col length" scope="col"><?= /* @escapeNotVerified */ __('Breaking Strain') ?></th>
        <?php endif; ?>
        <?php if($showSystemstrengthTxt): ?>
            <th class="col length" scope="col"><?= /* @escapeNotVerified */ __('System Strength') ?></th>
        <?php endif; ?>
        <th class="col indi" scope="col"><?= /* @escapeNotVerified */ __('Individual Price') ?></th>
        <th class="col multi" scope="col"><?= /* @escapeNotVerified */ __('Multiple Discount') ?></th>
        <?php if ($_product->isSaleable()): ?>
            <th class="col qty" scope="col"><?= /* @escapeNotVerified */ __('Qty') ?></th>
        <?php endif; ?>
    </tr>
    </thead>
</table>
</div>
<div id="validation-message-box"></div>

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.