@@ -429,12 +429,13 @@ public function getSwatchAttributesAsArray(Product $product)
429
429
*/
430
430
public function getSwatchesByOptionsId (array $ optionIds )
431
431
{
432
- sort ($ optionIds );
433
- $ cacheKey = implode ('- ' , $ optionIds );
434
- if (!isset ($ this ->swatchesCache [$ cacheKey ])) {
432
+ $ swatches = $ this ->getCachedSwatches ($ optionIds );
433
+
434
+ if (count ($ swatches ) !== count ($ optionIds )) {
435
+ $ swatchOptionIds = array_diff ($ optionIds , array_keys ($ swatches ));
435
436
/** @var \Magento\Swatches\Model\ResourceModel\Swatch\Collection $swatchCollection */
436
437
$ swatchCollection = $ this ->swatchCollectionFactory ->create ();
437
- $ swatchCollection ->addFilterByOptionsIds ($ optionIds );
438
+ $ swatchCollection ->addFilterByOptionsIds ($ swatchOptionIds );
438
439
439
440
$ swatches = [];
440
441
$ currentStoreId = $ this ->storeManager ->getStore ()->getId ();
@@ -451,10 +452,34 @@ public function getSwatchesByOptionsId(array $optionIds)
451
452
if (!empty ($ fallbackValues )) {
452
453
$ swatches = $ this ->addFallbackOptions ($ fallbackValues , $ swatches );
453
454
}
454
- $ this ->swatchesCache [ $ cacheKey ] = $ swatches ;
455
+ $ this ->setCachedSwatches ( $ swatchOptionIds , $ swatches) ;
455
456
}
456
457
457
- return $ this ->swatchesCache [$ cacheKey ];
458
+ return array_filter ($ this ->getCachedSwatches ($ optionIds ));
459
+ }
460
+
461
+ /**
462
+ * Get cached swatches
463
+ *
464
+ * @param array $optionIds
465
+ * @return array
466
+ */
467
+ private function getCachedSwatches (array $ optionIds )
468
+ {
469
+ return array_intersect_key ($ this ->swatchesCache , array_combine ($ optionIds , $ optionIds ));
470
+ }
471
+
472
+ /**
473
+ * Cache swatch. If no swathes found for specific option id - set null for prevent double call
474
+ *
475
+ * @param array $optionIds
476
+ * @param array $swatches
477
+ */
478
+ private function setCachedSwatches (array $ optionIds , array $ swatches )
479
+ {
480
+ foreach ($ optionIds as $ optionId ) {
481
+ $ this ->swatchesCache [$ optionId ] = isset ($ swatches [$ optionId ]) ? $ swatches [$ optionId ] : null ;
482
+ }
458
483
}
459
484
460
485
/**
0 commit comments