diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md index b31348bdf0a..2ebf4bfb486 100644 --- a/.github/copilot-instructions.md +++ b/.github/copilot-instructions.md @@ -56,6 +56,7 @@ This project aims to provide a stable and secure version of Magento 1.x, with on - Update comments to reflect changes in code. - Update tests to cover new functionality and changes in code. - Update copyright notices in new files. +- Do not add return types in docblocks if type hints are used. ## UI guidelines diff --git a/app/code/core/Mage/ConfigurableSwatches/Helper/Data.php b/app/code/core/Mage/ConfigurableSwatches/Helper/Data.php index 4c812e5a917..d3003d25c76 100644 --- a/app/code/core/Mage/ConfigurableSwatches/Helper/Data.php +++ b/app/code/core/Mage/ConfigurableSwatches/Helper/Data.php @@ -84,6 +84,14 @@ public static function normalizeKey($key) return trim(strtolower($key)); } + /** + * Wraps a key in a Normalized object that normalizes to lowercase but preserves original value + */ + public static function normalizeKeyToObject(?string $key): Mage_ConfigurableSwatches_Model_String_Normalized + { + return new Mage_ConfigurableSwatches_Model_String_Normalized($key); + } + /** * Get list of attributes that should use swatches * diff --git a/app/code/core/Mage/ConfigurableSwatches/Helper/List/Price.php b/app/code/core/Mage/ConfigurableSwatches/Helper/List/Price.php index 9ae47158933..a24ea1669be 100644 --- a/app/code/core/Mage/ConfigurableSwatches/Helper/List/Price.php +++ b/app/code/core/Mage/ConfigurableSwatches/Helper/List/Price.php @@ -65,8 +65,7 @@ public function attachConfigurableProductChildrenPricesMapping(array $products, ['product' => $product], ); $configurablePrice = $product->getConfigurablePrice(); - $cofigurableSwatchesHelper = Mage::helper('configurableswatches'); - $result[$cofigurableSwatchesHelper::normalizeKey($attributePrice['store_label'])] = [ + $result[Mage_ConfigurableSwatches_Helper_Data::normalizeKey($attributePrice['store_label'])] = [ 'price' => $configurablePrice, 'oldPrice' => $this->_getHelper()->prepareOldPrice( $product, diff --git a/app/code/core/Mage/ConfigurableSwatches/Helper/Mediafallback.php b/app/code/core/Mage/ConfigurableSwatches/Helper/Mediafallback.php index acda04049dd..5fef616b8bd 100644 --- a/app/code/core/Mage/ConfigurableSwatches/Helper/Mediafallback.php +++ b/app/code/core/Mage/ConfigurableSwatches/Helper/Mediafallback.php @@ -78,7 +78,7 @@ public function attachProductChildrenAttributeMapping(array $parentProducts, $st // normalize to all lower case before we start using them $optionLabels = array_map(function ($value) { - return array_map(Mage_ConfigurableSwatches_Helper_Data::normalizeKey(...), $value); + return array_map(Mage_ConfigurableSwatches_Helper_Data::normalizeKeyToObject(...), $value); }, $optionLabels); foreach ($parentProducts as $parentProduct) { @@ -109,7 +109,8 @@ public function attachProductChildrenAttributeMapping(array $parentProducts, $st } // using default value as key unless store-specific label is present - $optionLabel = $optionLabels[$optionId][$storeId] ?? $optionLabels[$optionId][0]; + $optionLabelObject = $optionLabels[$optionId][$storeId] ?? $optionLabels[$optionId][0]; + $optionLabel = (string) $optionLabelObject; // initialize arrays if not present if (!isset($mapping[$optionLabel])) { @@ -119,7 +120,7 @@ public function attachProductChildrenAttributeMapping(array $parentProducts, $st } $mapping[$optionLabel]['product_ids'][] = $childProduct->getId(); - $mapping[$optionLabel]['label'] = $optionLabel; + $mapping[$optionLabel]['label'] = $optionLabelObject; $mapping[$optionLabel]['default_label'] = $optionLabels[$optionId][0]; $mapping[$optionLabel]['labels'] = $optionLabels[$optionId]; @@ -184,10 +185,11 @@ public function getConfigurableImagesFallbackArray( // load images from the configurable product for swapping if (is_array($mapping)) { foreach ($mapping as $map) { + $mapLabel = (string) $map['label']; $imagePath = null; //search by store-specific label and then default label if nothing is found - $imageKey = array_search($map['label'], $imageHaystack); + $imageKey = array_search($mapLabel, $imageHaystack); if ($imageKey === false) { $imageKey = array_search($map['default_label'], $imageHaystack); } @@ -197,7 +199,7 @@ public function getConfigurableImagesFallbackArray( $imagePath = $mediaGallery['images'][$imageKey]['file']; } - $imagesByLabel[$map['label']] = [ + $imagesByLabel[$mapLabel] = [ 'configurable_product' => [ Mage_ConfigurableSwatches_Helper_Productimg::MEDIA_IMAGE_TYPE_SMALL => null, Mage_ConfigurableSwatches_Helper_Productimg::MEDIA_IMAGE_TYPE_BASE => null, @@ -206,11 +208,11 @@ public function getConfigurableImagesFallbackArray( ]; if ($imagePath) { - $imagesByLabel[$map['label']]['configurable_product'] + $imagesByLabel[$mapLabel]['configurable_product'] [Mage_ConfigurableSwatches_Helper_Productimg::MEDIA_IMAGE_TYPE_SMALL] = $this->_resizeProductImage($product, 'small_image', $keepFrame, $imagePath); - $imagesByLabel[$map['label']]['configurable_product'] + $imagesByLabel[$mapLabel]['configurable_product'] [Mage_ConfigurableSwatches_Helper_Productimg::MEDIA_IMAGE_TYPE_BASE] = $this->_resizeProductImage($product, 'image', $keepFrame, $imagePath); } diff --git a/app/code/core/Mage/ConfigurableSwatches/Model/Resource/Catalog/Product/Attribute/Super/Collection.php b/app/code/core/Mage/ConfigurableSwatches/Model/Resource/Catalog/Product/Attribute/Super/Collection.php index f485132ad84..2fd3df18b3a 100644 --- a/app/code/core/Mage/ConfigurableSwatches/Model/Resource/Catalog/Product/Attribute/Super/Collection.php +++ b/app/code/core/Mage/ConfigurableSwatches/Model/Resource/Catalog/Product/Attribute/Super/Collection.php @@ -119,7 +119,9 @@ protected function _getOptionLabels() ->where( 'labels.store_id IN (?)', [Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID, $this->getStoreId()], - ); + ) + ->order('options.sort_order ASC') + ->order('labels.value ASC'); $resultSet = $this->getConnection()->query($select); $labels = []; diff --git a/app/code/core/Mage/ConfigurableSwatches/Model/String/Normalized.php b/app/code/core/Mage/ConfigurableSwatches/Model/String/Normalized.php new file mode 100644 index 00000000000..f0e91a45ac8 --- /dev/null +++ b/app/code/core/Mage/ConfigurableSwatches/Model/String/Normalized.php @@ -0,0 +1,44 @@ +originalValue = $originalValue; + } + + /** + * Get normalized string value + */ + public function __toString(): string + { + return Mage_ConfigurableSwatches_Helper_Data::normalizeKey($this->originalValue); + } + + /** + * Get non-normalized original value + */ + public function getOriginalValue(): ?string + { + return $this->originalValue; + } +} diff --git a/app/design/frontend/rwd/default/template/configurableswatches/catalog/product/list/swatches.phtml b/app/design/frontend/rwd/default/template/configurableswatches/catalog/product/list/swatches.phtml index 8ba3bcd5f2a..2241f18716a 100644 --- a/app/design/frontend/rwd/default/template/configurableswatches/catalog/product/list/swatches.phtml +++ b/app/design/frontend/rwd/default/template/configurableswatches/catalog/product/list/swatches.phtml @@ -8,8 +8,10 @@ /** @var Mage_Core_Block_Template $this */ +/** @var Mage_Catalog_Model_Product $_product */ $_product = $this->getProduct(); +/** @var Mage_ConfigurableSwatches_Model_String_Normalized[] $_attrValues */ if (Mage::helper('configurableswatches')->isEnabled() && $_product && $_product->getId() && ($_attrValues = $_product->getListSwatchAttrValues()) && count($_attrValues) > 0): $_attrStockValues = $_product->getListSwatchAttrStockValues(); @@ -25,31 +27,47 @@ if (Mage::helper('configurableswatches')->isEnabled() && $_product && $_product- $_optionLabel): ?> getHyphenatedString($_optionLabel); - $_swatchUrl = Mage::helper('configurableswatches/productimg')->getSwatchUrl($_product, $_optionLabel, $_swatchInnerWidth, $_swatchInnerHeight, $_swatchType); + $_swatchUrl = Mage::helper('configurableswatches/productimg')->getSwatchUrl( + $_product, + $_optionLabel, + $_swatchInnerWidth, + $_swatchInnerHeight, + $_swatchType + ); + + $_label = $this->escapeHtml($_optionLabel->getOriginalValue()); $_hasImage = !empty($_swatchUrl); - $_liClasses = []; + $_liClass = ''; $_aClass = 'swatch-link swatch-link-' . $_swatchAttribute->getId(); if ($_hasImage) { - if ($_swatchType == 'media') { - $_liClasses[] = 'is-media'; - } + $_liClass .= $_swatchType == 'media' ? ' is-media' : ''; $_aClass .= ' has-image'; - } elseif (strlen($_optionLabel) > 3) { - $_liClasses[] = 'wide-swatch'; + } elseif (strlen($_label) > 3) { + $_liClass .= ' wide-swatch'; } if (Mage::helper('configurableswatches/productlist')->swatchMatchesFilter($_optionValue)) { - $_liClasses[] = 'filter-match'; + $_liClass .= ' filter-match'; } - $_liClass = (!empty($_liClasses)) ? ' ' . implode(' ', $_liClasses) : ''; ?> -