cancel
Showing results for 
Search instead for 
Did you mean: 

How can i make compare product Attributes have the same sort order like my Attributes set?

SOLVED

How can i make compare product Attributes have the same sort order like my Attributes set?

hi to all

in magento when you press compare product the Attributes are in diffrent postition than the Attributes set in admin. How can i have the same sort order in comparison window? 

thanks

1 ACCEPTED SOLUTION

Accepted Solutions

Re: How can i make compare product Attributes have the same sort order like my Attributes set?

Hi @freris ,

 

check the modifications in function below:

Function is in file vendor/magento/module-catalog/Model/ResourceModel/Product/Compare/Item/Collection.php

public function getComparableAttributes()
{
    if ($this->_comparableAttributes === null) {        $this->_comparableAttributes = [];        $setIds = $this->_getAttributeSetIds();
        if ($setIds) {            $attributeIds = $this->_getAttributeIdsBySetIds($setIds);            $select = $this->getConnection()->select()->from(
                ['main_table' => $this->getTable('eav_attribute')]
            )->join(
                ['additional_table' => $this->getTable('catalog_eav_attribute')],
                'additional_table.attribute_id=main_table.attribute_id'
            )->joinLeft(
                ['al' => $this->getTable('eav_attribute_label')],
                'al.attribute_id = main_table.attribute_id AND al.store_id = ' . (int)$this->getStoreId(),
                [
                    'store_label' => $this->getConnection()->getCheckSql(
                        'al.value IS NULL',
                        'main_table.frontend_label',
                        'al.value'
                    )
                ]
            )
                    ->joinLeft(
                ['as' => $this->getTable('eav_entity_attribute')],
                'as.attribute_id = main_table.attribute_id'
            )
                    ->where(
                'additional_table.is_comparable=?',
                1
            )->where(
                'main_table.attribute_id IN(?)',                $attributeIds
            )
                    ->order('as.sort_order') //sort by sort_order
                    ;            $attributesData = $this->getConnection()->fetchAll($select);
            if ($attributesData) {                $entityType = \Magento\Catalog\Model\Product::ENTITY;                $this->_eavConfig->importAttributesData($entityType, $attributesData);
                foreach ($attributesData as $data) {                    $attribute = $this->_eavConfig->getAttribute($entityType, $data['attribute_code']);                    $this->_comparableAttributes[$attribute->getAttributeCode()] = $attribute;
                }                unset($attributesData);
            }
        }
    }
    return $this->_comparableAttributes;
}

 Try to override the core file and do the changes. Do not modify core files.

 

Hope this helps you!

Problem Solved! Click Kudos & Accept as Solution!

View solution in original post

2 REPLIES 2

Re: How can i make compare product Attributes have the same sort order like my Attributes set?

Hi @freris ,

 

check the modifications in function below:

Function is in file vendor/magento/module-catalog/Model/ResourceModel/Product/Compare/Item/Collection.php

public function getComparableAttributes()
{
    if ($this->_comparableAttributes === null) {        $this->_comparableAttributes = [];        $setIds = $this->_getAttributeSetIds();
        if ($setIds) {            $attributeIds = $this->_getAttributeIdsBySetIds($setIds);            $select = $this->getConnection()->select()->from(
                ['main_table' => $this->getTable('eav_attribute')]
            )->join(
                ['additional_table' => $this->getTable('catalog_eav_attribute')],
                'additional_table.attribute_id=main_table.attribute_id'
            )->joinLeft(
                ['al' => $this->getTable('eav_attribute_label')],
                'al.attribute_id = main_table.attribute_id AND al.store_id = ' . (int)$this->getStoreId(),
                [
                    'store_label' => $this->getConnection()->getCheckSql(
                        'al.value IS NULL',
                        'main_table.frontend_label',
                        'al.value'
                    )
                ]
            )
                    ->joinLeft(
                ['as' => $this->getTable('eav_entity_attribute')],
                'as.attribute_id = main_table.attribute_id'
            )
                    ->where(
                'additional_table.is_comparable=?',
                1
            )->where(
                'main_table.attribute_id IN(?)',                $attributeIds
            )
                    ->order('as.sort_order') //sort by sort_order
                    ;            $attributesData = $this->getConnection()->fetchAll($select);
            if ($attributesData) {                $entityType = \Magento\Catalog\Model\Product::ENTITY;                $this->_eavConfig->importAttributesData($entityType, $attributesData);
                foreach ($attributesData as $data) {                    $attribute = $this->_eavConfig->getAttribute($entityType, $data['attribute_code']);                    $this->_comparableAttributes[$attribute->getAttributeCode()] = $attribute;
                }                unset($attributesData);
            }
        }
    }
    return $this->_comparableAttributes;
}

 Try to override the core file and do the changes. Do not modify core files.

 

Hope this helps you!

Problem Solved! Click Kudos & Accept as Solution!

Re: How can i make compare product Attributes have the same sort order like my Attributes set?

thanks for your quick reply!

 

YES it's WORKING !!

Thank you for your help