0

In Magento 2.1.0, I have a custom attribute named brand, which has swatch images. So far I've managed to do this:

$attribute = $this->eavConfig->getAttribute('catalog_product', 'brand');
$options = $attribute->getSource()->getAllOptions();

now i can go through them and get the label and value like this:

 foreach ($options as $option) {
    echo $brand->getValue();  // Value        
    echo $brand->getLabel(); // Label
 }

but I'm not able to get the image corresponding to the attribute.

Can someone please help? I'm banging my head to the walls for two days now.

0

I've ended up creating my solution since in more than a week no other answer was found. So I'll leave it here in case someone else needs this

The attribute swatches are located in the eav_attribute_option_swatch table so i created a model that uses that table:

class Swatchcollect extends \Magento\Framework\Model\AbstractModel implements SwatchcollectInterface, \Magento\Framework\DataObject\IdentityInterface

in the __contruct of the block I've used:

\Dalimedia\SwatchImagesCollect\Model\SwatchcollectFactory $swatchCollect
$this->swatchCollect = $swatchCollect

and then created a function that gathers the info from the table like this:

$allSwatches = $this->swatchCollect->create();
$arraySwatches = $allSwatches->load($atribut['value'])->getData();

I hope this helps someone.

0

You need to add manufaturer to brand

            <?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************/?>

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.