I created one attribute with type Visual Swatch
now i need to get image path of specific option value or product then how to get it ?
I had the same problem. Finaly got it working with below code:
use Magento\Swatches\Helper\Data as SwatchData;
use Magento\Swatches\Helper\Media;
...
public function __construct(
SwatchData $swatchHelper,
Media $swatchMediaHelper,
Config $eavConfig
) {
$this->eavConfig = $eavConfig;
$this->swatchHelper = $swatchHelper;
$this->swatchMediaHelper = $swatchMediaHelper;
}
...
$attribute = $this->eavConfig->getAttribute('catalog_product',
$attribute_code); //First get the current attribute
$options = $attribute->getSource()->getAllOptions();
//Get all value_id's from attribute and add them to a array
$valueIds = [];
foreach ( $options as $option ) {
$valueIds[] = $option['value'];
}
//Get all images for value_id's
$images = $this->swatchHelper->getSwatchesByOptionsId($valueIds);
Now $images contains an array with the id, label and image for each value.
Hope this helps someone.
$swatchHelper=$this->_objectManager->get("Magento\Swatches\Helper\Media");
$swatchCollection = $this->_objectManager->create('Magento\Swatches\Model\ResourceModel\Swatch\Collection');
// brand logo is my Visual Swatch attribute
$optionIdvalue=$this->getProduct()->getBrandLogo();
$swatchCollection->addFieldtoFilter('option_id',$optionIdvalue);
$item=$swatchCollection->getFirstItem();
echo $swatchHelper->getSwatchAttributeImage('swatch_thumb', $item->getValue());
echo $swatchHelper->getSwatchAttributeImage('swatch_image', $item->getValue());
<?php/***********Display Brand Attribute In Category page************/?>
<?php
if($_product->getManufacturer()){
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
$brand_value = $_product->getResource()->getAttribute('manufacturer')->getFrontend()->getValue($_product);
$brand_id = $_product->getManufacturer();
$curnt_cat_url = $category->getUrl();
$brandId = '?manufacturer='.$brand_id;
$om = \Magento\Framework\App\ObjectManager::getInstance();
$swatchHelper=$om->get("Magento\Swatches\Helper\Media");
$swatchCollection = $om->create('Magento\Swatches\Model\ResourceModel\Swatch\Collection');
$swatchCollection->addFieldtoFilter('option_id',$brand_id);
$item=$swatchCollection->getFirstItem();
$ThumbImage = $swatchHelper->getSwatchAttributeImage('swatch_thumb', $item->getValue());
$SwatchImage = $swatchHelper->getSwatchAttributeImage('swatch_image', $item->getValue());
?>
<div class="brand_bg">
<a href="<?php echo $curnt_cat_url.$brandId ?>"><img src="<?php echo $SwatchImage; ?>"></a>
</div>
<?php } ?>
<?php/***********End Brand Attribute************/?>
<?php/***********Display Brand Attribute************/?>
<?php
$om = \Magento\Framework\App\ObjectManager::getInstance();
$categoryFactory = $om->get('\Magento\Catalog\Model\CategoryFactory');
$categoryHelper = $om->get('\Magento\Catalog\Helper\Category');
$categoryRepository = $om->get('\Magento\Catalog\Model\CategoryRepository');
$category = $categoryFactory->create()->load($catId);
$categoryProducts = $category->getProductCollection()
->addAttributeToSelect('*');
?>
<div class="main_brand_bg">
<ul>
<?php
$brandIds = [];
foreach ($categoryProducts as $_product) {
if($_product->getManufacturer()){
$brandValue = $_product->getResource()->getAttribute('manufacturer')->getFrontend()->getValue($_product);
$brandId = $_product->getManufacturer();
$curnt_cat_url = $category->getUrl();
$brandLink = '?manufacturer='.$brandId;
$om = \Magento\Framework\App\ObjectManager::getInstance();
$swatchHelper=$om->get("Magento\Swatches\Helper\Media");
$swatchCollection = $om->create('Magento\Swatches\Model\ResourceModel\Swatch\Collection');
$swatchCollection->addFieldtoFilter('option_id',$brandId);
$item = $swatchCollection->getFirstItem();
$thumbImage = $swatchHelper->getSwatchAttributeImage('swatch_thumb', $item->getValue());
$SwatchImage = $swatchHelper->getSwatchAttributeImage('swatch_image', $item->getValue());
if (!in_array($brandId,$brandIds )){
$brandIds[] = $brandId;
?>
<div class="brand_bg">
<li class="brand_img_bg">
<a href="<?php echo $curnt_cat_url.$brandLink; ?>" title="<?php echo $brandValue; ?>" ><img src="<?php echo $thumbImage; ?>"></a>
</li>
</div>
<?php } ?>
<?php } ?>
<?php } ?>
</ul>
</div>