#StackBounty: #magento2.3 #compare #attribute-group Magento 2.3 – Add attribute group name on compare products page

Bounty: 150

We want to implement the attribute group name on the compare product page and separate all attributes based on the attribute group.

We use the following code to show the attribute group name on the specifications section on the product page.

How can we implement this on the product compare page?

CODE;

<?php
namespace AttributeGroupModuleBlockProduct;

class Specification extends MagentoCatalogBlockProductView
{
     /**
      * @var MagentoEavModelResourceModelEntityAttributeGroupCollectionFactory
      */
     protected $groupCollectionFactory;

     public function __construct(
         MagentoCatalogBlockProductContext $context,
         MagentoFrameworkUrlEncoderInterface $urlEncoder,
         MagentoFrameworkJsonEncoderInterface $jsonEncoder,
         MagentoFrameworkStdlibStringUtils $string,
         MagentoCatalogHelperProduct $productHelper,
         MagentoCatalogModelProductTypesConfigInterface $productTypeConfig,
         MagentoFrameworkLocaleFormatInterface $localeFormat,
         MagentoCustomerModelSession $customerSession,
         MagentoCatalogApiProductRepositoryInterface $productRepository,
         MagentoFrameworkPricingPriceCurrencyInterface $priceCurrency,
         MagentoEavModelResourceModelEntityAttributeGroupCollectionFactory $groupCollectionFactory,
         array $data = []
    ) {
        parent::__construct($context, $urlEncoder, $jsonEncoder, $string,
        $productHelper, $productTypeConfig, $localeFormat, $customerSession,
        $productRepository, $priceCurrency, $data);
        $this->groupCollectionFactory = $groupCollectionFactory;
    }


    public function getAttributeLabels()
     {
    $product = $this->getProduct();

    $attributeSetId = $product->getAttributeSetId();

    $groupCollection = $this->groupCollectionFactory->create()
        ->setAttributeSetFilter($attributeSetId)
        ->setSortOrder()
        ->load();

    $html = '';
    foreach ($groupCollection as $group) {
        $attributes = $product->getAttributes($group->getId(), true);



        $new_html = "";
        foreach ($attributes as $key => $attribute) {
            if($attribute->getIsVisibleOnFront() && $attribute->getFrontend()->getValue($product) !="" && $attribute->getFrontend()->getValue($product) !="Non"){

                $new_html .= '<div class="product-specs-list-item">
                                <dt class="product-specs-item-title">' . $attribute->getStoreLabel(). '</dt>
                                <dl class="product-specs-item-spec">' . $attribute->getFrontend()->getValue($product) . '</dl>              
                            </div>';

            }
        }

        if($new_html!=''){

            $html .= "<div class='grid-section-xs-specification active'>";
            $html .= '<h3 class="noselect toggle-specification">' . $group->getData('attribute_group_name').'</h3>';
            $html .= "<dl class='product-specs'>";
            $html .= $new_html;
            $html .= "</dl>
            </div>";
        }

    }

    return $html;
}
}?>


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.