1

I have attribute color_swatch which is visual swatch and it has some items with images. In my code I can take this data:

[swatch_id] => 57
[color_swatch] => Red

[swatch_id] => 58
[color_swatch] => Blue

[swatch_id] => 59
[color_swatch] => Black

How can I get image of the swatch using this data?

0

1 Answer 1

7

You can get swatch Image from this,

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$swatch = $objectManager->create('Magento\Swatches\Helper\Data')->getSwatchesByOptionsId([$swatchId]);

$imageName= $swatch[$optionid]['value'];

if(!empty($imageName)){
$imagePath=$objectManager->create('Magento\Swatches\Helper\Media')->getSwatchMediaUrl().$imageName;
echo $imagePath;
}

You can find Image value from eav_attribute_option_swatch table. Where value is Image path and type 1 is uploaded image filer and 2 is color selected from color swatch.


Note: I am against of direct loading object with $objectManager, for better impact you can inject it in your constructor. I have just given example how you can achieve it. `

3
  • 2
    If you're against using the object manager, why does your only suggested solution involve the object manager? :-)
    – Joe
    Mar 5, 2018 at 15:38
  • @Keyur Shah do you have REST API link which i can get swatch value like above ? Thanks May 27, 2019 at 5:27
  • how can I get swatch id? Nov 29, 2019 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.