1

I want to use a swatch based attribute basically as an image uploader for brand logos. I already have a brand attribute which i want to remain as is so the layered navigation is text however want to just call the image from this new brand_swatch attribute based on the valueor label of the brand attribute as these will match however cannot use ID's as these will not match.

Something like below:

//get brand, this works fine
$brand = $_product->getResource()->getAttribute('brand')->getFrontend()->getValue($_product);
$swatchCollection = $objectManager->create('Magento\Swatches\Model\ResourceModel\Swatch\Collection');
//unsure from here on in but want to select the brand_swatch attribute
$optionIdvalue = 12; 
$swatchCollection->addFieldtoFilter('swatch_id',$swatchIdvalue)->getSelect()
->join('eav_attribute_option_value as option', 'main_table.option_id = option.option_id')
->join('eav_attribute_option as option2', 'main_table.option_id = option2.option_id')
->group('main_table.option_id')
->where('option2.frontend_label = ' + $brand);
//here however am unsure how to filter by the item that matches the brand attribute so rather than firstItem get item where name = brand
$item=$swatchCollection->getFirstItem();#
//then build the url for image to be ready to be shown on frontend
return $swatchHelper->getSwatchAttributeImage('swatch_image', $item->getValue());

This is just an example and am unsure if the optionIdvalue is to filter swatches to the brand_swatch attribute and how from there to filter by the name of the option. Can anyone suggest how this is achieved?

Update

I have changed the addFieldtoFilter to swatch_id and used the attribute ID here hoping this should return my swatch collection.

I cannot find where the label for swatches is stored however after looking at below diagram and am therefore unable to filter by the label. Also the query seemed to be only returning one result even without any filters for brand but I was hoping would return my list of options from the attribute.

Also using getOptionId() seems to return random numbers that do not appear to be related to my swatch so am wondering if attribute id and swatch id's are the same thing and if not where to find swatch ids.

http://anna.voelkl.at/wp-content/uploads/2016/12/ce2.1.3.png

Your Answer

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

Browse other questions tagged or ask your own question.