diff --git a/app/code/core/Mage/Catalog/Model/Resource/Product/Compare/Item/Collection.php b/app/code/core/Mage/Catalog/Model/Resource/Product/Compare/Item/Collection.php index fea32436767..f2522a00c09 100644 --- a/app/code/core/Mage/Catalog/Model/Resource/Product/Compare/Item/Collection.php +++ b/app/code/core/Mage/Catalog/Model/Resource/Product/Compare/Item/Collection.php @@ -215,15 +215,23 @@ public function getComparableAttributes() $eavConfig = Mage::getSingleton('eav/config'); $attributeIds = $eavConfig->getAttributeSetAttributeIds($setIds); $this->_comparableAttributes = []; + $attributeSortInfo = []; foreach ($attributeIds as $attributeId) { $attribute = $eavConfig->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeId); if ($attribute->getData('is_comparable')) { $this->_comparableAttributes[$attribute->getAttributeCode()] = $attribute; + $attributeSortInfo[$attribute->getAttributeCode()] = $eavConfig->getAttributeSetGroupInfo($attributeId, $setIds); } } - usort($this->_comparableAttributes, function ($a, $b) { - return $a->getPosition() - $b->getPosition(); + uasort($this->_comparableAttributes, function ($a, $b) use ($attributeSortInfo) { + /** @var Mage_Eav_Model_Entity_Attribute_Abstract $a */ + /** @var Mage_Eav_Model_Entity_Attribute_Abstract $b */ + + $aSort = $attributeSortInfo[$a->getAttributeCode()]; // contains group_id, group_sort, sort + $bSort = $attributeSortInfo[$b->getAttributeCode()]; // contains group_id, group_sort, sort + + return $aSort['group_sort'] <=> $bSort['group_sort'] ?: $aSort['sort'] <=> $bSort['sort']; }); } } diff --git a/app/code/core/Mage/Eav/Model/Config.php b/app/code/core/Mage/Eav/Model/Config.php index 3ab8b8b74f4..87ddb33443c 100644 --- a/app/code/core/Mage/Eav/Model/Config.php +++ b/app/code/core/Mage/Eav/Model/Config.php @@ -598,6 +598,27 @@ public function getAttributeSetAttributeIds($attributeSetId) return array_keys($attributes); } + /** + * Return first attribute sorting information found for a given list of attribute sets + * @param int $attributeId + * @param int|int[] $attributeSetIds + * @return false|array + */ + public function getAttributeSetGroupInfo($attributeId, $attributeSetIds) + { + if (!is_array($attributeSetIds)) { + $attributeSetIds = [$attributeSetIds]; + } + + foreach ($attributeSetIds as $attributeSetId) { + if (isset($this->_attributeSetInfo[$attributeId][$attributeSetId])) { + return $this->_attributeSetInfo[$attributeId][$attributeSetId]; + } + } + + return false; + } + /** * @param mixed $entityType * @param string $attribute