3

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 ?

5

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.

| improve this answer | |
3
$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());
| improve this answer | |
2
<?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************/?>
| improve this answer | |
1
<?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>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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