I've accomplished this without creating a module. Hope this may help somebody
- I created a custom product attribute. which allows the user to choose a manufacturer from the dropdown list.
- I get image from app->design>[my vendor]->[my theme]->web->images->products, and image name will be equal to name selected from above dropdown.
- Added layout in XML where I need this image should be.
- used phtml file to locate images and display.
1.
From admin menu, Stores->Attributes->product
Create a new attribute. Default Label(your name)
, Catalog Input Type for Store Owner(dropdown)
, Values Required(No)
. under Manage Options (Values of Your Attribute)
you can add your options.
2.
You can choose any location as you need.
3.
This could be different for your needs,
<referenceContainer name="product.details.section" >
<container name="brand.logo" htmlTag="div" htmlClass="right-brand" before="-">
<block class="Magento\Catalog\Block\Product\View" name="customBrandLogo" template="product/view/custombrandlogo.phtml"/>
</container>
4.
in my case, phtml file is custombrandlogo.phtml located at Magento_Catalog->templates->product->view
I get manufacturer name from backend menu,
<?php
$_product = $this->getProduct();
$manufacturerName = $_product->getAttributeText('manufacturer');
$manufacturerId = $_product->getManufacturer();
//php close tag will be at the end
I convert the name into lowercase and replace underscores with spaces.
$manufacturerName= strtolower($manufacturerName);
$manufacturerName=str_replace(' ', '_',$manufacturerName);
I get image path
$imgPath=$this->getViewFileUrl("{$imgPath}/{$manufacturerName}" );
//php tag is closed?>
_Finally display an image with img
tag.
<img class="img-responsive" src="<?php echo $imgPath ?>.png" alt="<?php echo $manufacturerName; ?>" /></a>
I hardcoded image extension as .png