From f0e8993e06158bf632faf4b5f90ebf660ecd4f95 Mon Sep 17 00:00:00 2001 From: David Hiendl Date: Mon, 1 Jul 2024 13:00:34 +0200 Subject: [PATCH] fix regression introduced in #2993 where attributes are no longer sorted correctly by attribute group order in attribute comparison --- .../Product/Compare/Item/Collection.php | 12 +++++++++-- app/code/core/Mage/Eav/Model/Config.php | 21 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) 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