0

I would like to show the various attributes divided into groups as created in the set of attributes, such as

  • general
    • attribute_label1 :attribute_value1
    • attribute_labe2 :attribute_value2
    • ...
  • other info
    • attribute_label1 :attribute_value1
    • attribute_labe2 :attribute_value2
    • ...

How do I retrieve the attribute groups and their labels?

or is there a module to do this? I have override theme and change catalog_product_view.xml file

<block class="Magento\Catalog\Block\Product\View" name="catalog.product.specification" template="Magento_Catalog::product/view/specification.phtml" after="catalog.product.extra.info"/>

And wants collection within this file specification.phtml

3
  • similar question asked here magento.stackexchange.com/questions/134179/… Sep 15, 2017 at 9:34
  • It is not dynamic.I wants dynamic thats why.I want to list all attribute product group wise there are multiple group and have multiple attribute assign to this,also think about multiple attribute set Sep 15, 2017 at 9:45
  • I did in mag1 but in mag2 little hard Sep 15, 2017 at 9:46

1 Answer 1

4
+50

In catalog_product_view.xml change to:

<block class="Custom\Module\Block\Product\Specification" name="catalog.product.specification" template="Magento_Catalog::product/view/specification.phtml" after="catalog.product.extra.info"/>

File app/code/Custom/Module/Block/Product/Specification.php

<?php
namespace Custom\Module\Block\Product;

class Specification extends \Magento\Catalog\Block\Product\View
{
     /**
      * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory
      */
     protected $groupCollectionFactory;

     public function __construct(
         \Magento\Catalog\Block\Product\Context $context,
         \Magento\Framework\Url\EncoderInterface $urlEncoder,
         \Magento\Framework\Json\EncoderInterface $jsonEncoder,
         \Magento\Framework\Stdlib\StringUtils $string,
         \Magento\Catalog\Helper\Product $productHelper,
         \Magento\Catalog\Model\ProductTypes\ConfigInterface $productTypeConfig,
         \Magento\Framework\Locale\FormatInterface $localeFormat,
         \Magento\Customer\Model\Session $customerSession,
         \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
         \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
         \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $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 .= '<td class="td-full">
                                <table>
                                    <tbody>
                                        <tr>
                                            <th class="even">' . $attribute->getFrontend()->getLabel(). '</th>
                                            <td class="evev-1">' . $attribute->getFrontend()->getValue($product) . '</td>
                                        </tr>
                                    </tbody>
                                </table>                 
                            </td>';

            }
        }

        if($new_html!=''){

            $html .= '<h3>' . $group->getData('attribute_group_name').'</h3>';
            $html .= "<table>
                        <tbody>
                            <tr>";
            $html .= $new_html;
            $html .= "</tr>
                    </tbody>
                </table>";
        }

    }

    return $html;
}
}

And in specification.phtml you can call it

<?php echo $block->getAttributeLabels() ?>
10
  • Notice: Undefined property: Magento\Catalog\Block\Product\View\Interceptor::$groupCollectionFactory--->I have called View for get product on view page how can i sove that? Sep 18, 2017 at 11:47
  • You should DI $groupCollectionFactory as instance of \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory in your class
    – Quan Le
    Sep 18, 2017 at 15:39
  • Please can you edit your answer with my requirement?Thanks Sep 19, 2017 at 4:48
  • @KetanKpBorada I've updated my answer.
    – Quan Le
    Sep 19, 2017 at 5:14
  • Waw Exact Solution !!Yet i've edit this as I don't need attribute which is not allowed on product page and also not display group which has not attribute Sep 19, 2017 at 6:49

Your Answer

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

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