1

Magento 2.1 in the "More Information" of products page position. It's okay but when I compare position it incorrect such as the product page attribute positions 1 2 3 4 but in compare 2 3 4 1.

1 is my new attribute and 2 3 4 are my old attributes.

0

On product, page attributes are sorted like in attribute set. But on the comparison product page attributes are not sorted at all. You have to override function getComparableAttributes in

vendor/magento/module-catalog/Model/ResourceModel/Product/Compare/Item/Collection.php

Here is Overridden function:

 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( //add sort order to sort the attributes as per backend sort.
                        ['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
                    )
                     ->where("as.attribute_set_id in (?)", $setIds)
                            ->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;
        }

Working fine in Magento 2.4.0

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.