Last active
May 25, 2016 01:39
-
-
Save MichaelThessel/60b26ff2a847ef3b824027bb0af34461 to your computer and use it in GitHub Desktop.
AW Colorswatches performance optimization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/app/code/local/AW/Colorswatches/Helper/Swatch.php b/app/code/local/AW/Colorswatches/Helper/Swatch.php | |
index 8a2a868..09807fb 100644 | |
--- a/app/code/local/AW/Colorswatches/Helper/Swatch.php | |
+++ b/app/code/local/AW/Colorswatches/Helper/Swatch.php | |
@@ -87,16 +87,22 @@ class AW_Colorswatches_Helper_Swatch | |
$collection = $swatchAttribute->getSwatchCollection(); | |
$childProducts = $this->_getAssociatedProductList($product); | |
+ | |
+ $childCodes = array(); | |
+ foreach ($childProducts as $child) { | |
+ $childCodes[$child->getId()] = $child->getData($swatchAttribute->getAttributeModel()->getAttributeCode()); | |
+ } | |
+ $validCodes = array_unique($childCodes); | |
+ | |
$result = array(); | |
foreach($collection as $swatch) { | |
+ if (!in_array($swatch->getOptionId(), $validCodes)) continue; | |
+ | |
/** @var AW_Colorswatches_Model_Swatch $swatch */ | |
- $image = AW_Colorswatches_Helper_Image::resizeImage($swatch->getImage(), $imgWidth, $imgHeight); | |
- $ttImage = AW_Colorswatches_Helper_Image::resizeImage($swatch->getImage(), $tooltipWidth, $tooltipHeight); | |
$productList = array(); | |
$notSaleableList = array(); | |
- foreach($childProducts as $child) { | |
- $value = $child->getData($swatchAttribute->getAttributeModel()->getAttributeCode()); | |
- if ($value !== $swatch->getOptionId()) { | |
+ foreach ($childProducts as $child) { | |
+ if ($childCodes[$child->getId()] !== $swatch->getOptionId()) { | |
continue; | |
} | |
$productList[] = $child->getId(); | |
@@ -108,6 +114,14 @@ class AW_Colorswatches_Helper_Swatch | |
$ttImage = $this->_getProductImage($child, $tooltipWidth, $tooltipHeight); | |
} | |
} | |
+ | |
+ if (!isset($image)) { | |
+ $image = AW_Colorswatches_Helper_Image::resizeImage($swatch->getImage(), $imgWidth, $imgHeight); | |
+ } | |
+ if (!isset($ttImage)) { | |
+ $ttImage = AW_Colorswatches_Helper_Image::resizeImage($swatch->getImage(), $tooltipWidth, $tooltipHeight); | |
+ } | |
+ | |
$result[$swatch->getOptionId()] = array( | |
'title' => $swatch->getOptionLabel(), | |
'img' => $image, | |
@@ -116,6 +130,8 @@ class AW_Colorswatches_Helper_Swatch | |
'not_saleable' => $notSaleableList, | |
'sort_order' => $swatch->getSortOrder(), | |
); | |
+ | |
+ unset($image, $ttImage); | |
} | |
return $result; | |
} | |
@@ -186,4 +202,4 @@ class AW_Colorswatches_Helper_Swatch | |
{ | |
return Mage::helper('catalog/image')->init($product, 'image')->resize($width, $height)->__toString(); | |
} | |
-} | |
\ No newline at end of file | |
+} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment