cancel
Showing results for 
Search instead for 
Did you mean: 

Sort attributes by attribute groups on product page

SOLVED
   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

Sort attributes by attribute groups on product page

Hi,

I've got quite a few attributes for every of my products on my website. Hence, I want to sort these attributes by the attribute groups that I put them in, in the admin panel. So is there a way that I can show the attribute groups on my product page?

 

Thank you in advance.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Sort attributes by attribute groups on product page

Hello emil_faurholdt

Magebull/MultiSlider/Block/Test.php

 

<?php
namespace Magebull\MultiSlider\Block;

class Test extends \Magento\Framework\View\Element\Template
{

  protected $_productloader;
  protected $request;
  protected $_groupCollection;
  protected $_registry;

  public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Model\ProductFactory $_productloader,
        \Magento\Framework\Registry $_registry,
        \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $_groupCollection,
        \Magento\Framework\App\Request\Http $request

    ) {


        $this->_productloader = $_productloader;
        $this->request = $request;
         $this->_registry = $_registry;
        $this->_groupCollection = $_groupCollection;
        parent::__construct($context);
    }
//get the product
    public function getLoadProduct()
    {            
        $product = $this->_registry->registry('current_product'); 
        return $this->_productloader->create()->load($product->getId());
    }
//Get attribute group id
    public function getAttributeGroupId($attributeSetId)
    {
         $groupCollection = $this->_groupCollection->create();
         $groupCollection->addFieldToFilter('attribute_set_id',$attributeSetId);
         $groupCollection->addFieldToFilter('attribute_group_name','Grid Attributes');
         
         
         return $groupCollection->getFirstItem();

    }
    //Get all attribute groups
    public function getAttributeGroups($attributeSetId)
    {
         $groupCollection = $this->_groupCollection->create();
         $groupCollection->addFieldToFilter('attribute_set_id',$attributeSetId);
         
         $groupCollection->setOrder('sort_order','ASC');
         return $groupCollection;

    }
//get attribute by groups
 public function getGroupAttributes($pro,$groupId, $productAttributes){
        $data=[];
        $no =__('No');
        foreach ($productAttributes as $attribute){
   
          if ($attribute->isInGroup($pro->getAttributeSetId(), $groupId) && $attribute->getIsVisibleOnFront() ){
              if($attribute->getFrontend()->getValue($pro) && $attribute->getFrontend()->getValue($pro)!='' && $attribute->getFrontend()->getValue($pro)!=$no){
                $data[]=$attribute;
              }
          }

        }
 
  return $data;
 }
 
 }

Magebull/MultiSlider/etc/module.xml

 

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Magebull_MultiSlider" setup_version="0.0.1"/>
</config>

/Magebull/MultiSlider/view/frontend/layout/catalog_product_view.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page layout="1column" 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.details">
    		   <block class="Magebull\MultiSlider\Block\Test" name="example.tabs" template="Magebull_MultiSlider::test1.phtml" group="detailed_info">
              <arguments>
                  <argument translate="true" name="title" xsi:type="string">Your Title</argument>
              </arguments>
          </block>
    	</referenceBlock>
    </body>    
</page>

 

/Magebull/MultiSlider/view/frontend/templates/test1.phtml

 

<?php 

$_product = $block->getLoadProduct();
$groupid = $block->getAttributeGroupId($_product->getAttributeSetId());
$attributesgroups = $block->getAttributeGroups($_product->getAttributeSetId());

$productAttributes = $_product->getAttributes();




?>
 
<?php
foreach ($attributesgroups as $attributesgroup):


 $attributes = $block->getGroupAttributes($_product,$attributesgroup->getAttributeGroupId(),$productAttributes);
 

 
 
 if($attributes){ ?>

 

   
            <h3 class="col label" scope="row"><?php echo $attributesgroup->getAttributeGroupName() ?></h3>
           
           
            <div class="additional-attributes-wrapper table-wrapper block">
        <table class="data table additional-attributes" id="product-attribute-specs-table">
            <tbody>
           
    <?php
    foreach ($attributes as $attribute): ?>
       
       
       
             <tr>
                    <td class="col label" scope="row"><?php echo $attribute->getFrontendLabel() ?></td>
                    <td class="col data feature" data-th="<?php echo $attribute->getFrontendLabel() ?>"><?php /* @escapeNotVerified */ echo $attribute->getFrontend()->getValue($_product) ?></td>
                </tr>
           
            
       
        <?php    
    endforeach; ?>
           </tbody>
        </table>
        </div>
 <?php }
endforeach;

?>

Magebull/MultiSlider/registration.php

 

<?php
    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'Magebull_MultiSlider',
        __DIR__
    );


Kindlt find below link to download extension.


Url: https://we.tl/bxUdgFcTyz

If you've found my answer useful, please give "Kudos" or "Accept as Solution"

 

 
 
 

View solution in original post

5 REPLIES 5

Re: Sort attributes by attribute groups on product page

Hello emil_faurholdt

 

if you want display attribute group name in product tab.

 

Create a block with following code

protected $_groupCollection; 


  public function __construct(

...............
        \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $_groupCollection

.............
    ) {




        $this->_groupCollection = $_groupCollection;
        parent::__construct($context);
    }
public function getAttributeGroupId($attributeSetId)
    {
         $groupCollection = $this->_groupCollection->create();
         $groupCollection->addFieldToFilter('attribute_set_id',$attributeSetId);
         $groupCollection->addFieldToFilter('attribute_group_name','Grid Attributes');
         
         
         return $groupCollection->getFirstItem(); 

    }
    
    public function getAttributeGroups($attributeSetId)
    {
         $groupCollection = $this->_groupCollection->create();
         $groupCollection->addFieldToFilter('attribute_set_id',$attributeSetId);
         
         $groupCollection->setOrder('sort_order','ASC');
         return $groupCollection; 

    }
 public function getGroupAttributes($pro,$groupId, $productAttributes){
        $data=[];
        $no =__('No');
        foreach ($productAttributes as $attribute){
    
          if ($attribute->isInGroup($pro->getAttributeSetId(), $groupId) && $attribute->getIsVisibleOnFront() ){
              if($attribute->getFrontend()->getValue($pro) && $attribute->getFrontend()->getValue($pro)!='' && $attribute->getFrontend()->getValue($pro)!=$no){
                $data[]=$attribute;
              }
          }

        }
  
  return $data;
 }
 

 

Now call that in your phtml file

 

$groupid=$block->getAttributeGroupId($_product->getAttributeSetId());
$attributesgroups=$block->getAttributeGroups($_product->getAttributeSetId());

$productAttributes=$product->getAttributes();

$i=0;

 $countAttributes=$block->getCountAttributes($product,$attributesgroups,$productAttributes);
?>
 
<?php
foreach ($attributesgroups as $attributesgroup):


 $attributes=$block->getGroupAttributes($product,$attributesgroup->getAttributeGroupId(),$productAttributes);
 

 
 
 if($attributes){ ?>

 

   
            <h3 class="col label" scope="row"><?php echo $attributesgroup->getAttributeGroupName() ?></h3>
           
           
            <div class="additional-attributes-wrapper table-wrapper block">
        <table class="data table additional-attributes" id="product-attribute-specs-table">
            <tbody> 
            
    <?php
    foreach ($attributes as $attribute): ?>
        
       
        
             <tr>
                    <td class="col label" scope="row"><?php echo $attribute->getFrontendLabel() ?></td>
                    <td class="col data feature" data-th="<?php echo $attribute->getFrontendLabel() ?>"><?php /* @escapeNotVerified */ echo $attribute->getFrontend()->getValue($product) ?></td>
                </tr>
            
            
        
        <?php    
    endforeach; ?>
           </tbody>
        </table>
        </div>
 <?php }
endforeach;

?>
 

 

URL: https://blog.qaisarsatti.com/magento_2/magento-2-get-product-attributes-attribute-groups/


Find helpful ? Consider Giving Kudos to this post.
Problem solved? Click Accept as Solution!"

 

 

Re: Sort attributes by attribute groups on product page

Hi, thank you very much.

I'm am new to this, so I have just one more question, which phtml file exactly are we talking about?

Re: Sort attributes by attribute groups on product page

Hello emil_faurholdt

Magebull/MultiSlider/Block/Test.php

 

<?php
namespace Magebull\MultiSlider\Block;

class Test extends \Magento\Framework\View\Element\Template
{

  protected $_productloader;
  protected $request;
  protected $_groupCollection;
  protected $_registry;

  public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Model\ProductFactory $_productloader,
        \Magento\Framework\Registry $_registry,
        \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $_groupCollection,
        \Magento\Framework\App\Request\Http $request

    ) {


        $this->_productloader = $_productloader;
        $this->request = $request;
         $this->_registry = $_registry;
        $this->_groupCollection = $_groupCollection;
        parent::__construct($context);
    }
//get the product
    public function getLoadProduct()
    {            
        $product = $this->_registry->registry('current_product'); 
        return $this->_productloader->create()->load($product->getId());
    }
//Get attribute group id
    public function getAttributeGroupId($attributeSetId)
    {
         $groupCollection = $this->_groupCollection->create();
         $groupCollection->addFieldToFilter('attribute_set_id',$attributeSetId);
         $groupCollection->addFieldToFilter('attribute_group_name','Grid Attributes');
         
         
         return $groupCollection->getFirstItem();

    }
    //Get all attribute groups
    public function getAttributeGroups($attributeSetId)
    {
         $groupCollection = $this->_groupCollection->create();
         $groupCollection->addFieldToFilter('attribute_set_id',$attributeSetId);
         
         $groupCollection->setOrder('sort_order','ASC');
         return $groupCollection;

    }
//get attribute by groups
 public function getGroupAttributes($pro,$groupId, $productAttributes){
        $data=[];
        $no =__('No');
        foreach ($productAttributes as $attribute){
   
          if ($attribute->isInGroup($pro->getAttributeSetId(), $groupId) && $attribute->getIsVisibleOnFront() ){
              if($attribute->getFrontend()->getValue($pro) && $attribute->getFrontend()->getValue($pro)!='' && $attribute->getFrontend()->getValue($pro)!=$no){
                $data[]=$attribute;
              }
          }

        }
 
  return $data;
 }
 
 }

Magebull/MultiSlider/etc/module.xml

 

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Magebull_MultiSlider" setup_version="0.0.1"/>
</config>

/Magebull/MultiSlider/view/frontend/layout/catalog_product_view.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page layout="1column" 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.details">
    		   <block class="Magebull\MultiSlider\Block\Test" name="example.tabs" template="Magebull_MultiSlider::test1.phtml" group="detailed_info">
              <arguments>
                  <argument translate="true" name="title" xsi:type="string">Your Title</argument>
              </arguments>
          </block>
    	</referenceBlock>
    </body>    
</page>

 

/Magebull/MultiSlider/view/frontend/templates/test1.phtml

 

<?php 

$_product = $block->getLoadProduct();
$groupid = $block->getAttributeGroupId($_product->getAttributeSetId());
$attributesgroups = $block->getAttributeGroups($_product->getAttributeSetId());

$productAttributes = $_product->getAttributes();




?>
 
<?php
foreach ($attributesgroups as $attributesgroup):


 $attributes = $block->getGroupAttributes($_product,$attributesgroup->getAttributeGroupId(),$productAttributes);
 

 
 
 if($attributes){ ?>

 

   
            <h3 class="col label" scope="row"><?php echo $attributesgroup->getAttributeGroupName() ?></h3>
           
           
            <div class="additional-attributes-wrapper table-wrapper block">
        <table class="data table additional-attributes" id="product-attribute-specs-table">
            <tbody>
           
    <?php
    foreach ($attributes as $attribute): ?>
       
       
       
             <tr>
                    <td class="col label" scope="row"><?php echo $attribute->getFrontendLabel() ?></td>
                    <td class="col data feature" data-th="<?php echo $attribute->getFrontendLabel() ?>"><?php /* @escapeNotVerified */ echo $attribute->getFrontend()->getValue($_product) ?></td>
                </tr>
           
            
       
        <?php    
    endforeach; ?>
           </tbody>
        </table>
        </div>
 <?php }
endforeach;

?>

Magebull/MultiSlider/registration.php

 

<?php
    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'Magebull_MultiSlider',
        __DIR__
    );


Kindlt find below link to download extension.


Url: https://we.tl/bxUdgFcTyz

If you've found my answer useful, please give "Kudos" or "Accept as Solution"

 

 
 
 

Re: Sort attributes by attribute groups on product page

Saved my day right there! Thank you very much.

Re: Sort attributes by attribute groups on product page

*solved