Question

How would I list all brands and images on a CMS page? I am using an attribute named 'brands' and would like to call each one and display the image. I am using the following code to display on product page and it works great. All my brand images are located in /media/wysiwyg/brands/.

<div class="brandImage"
<?php $_product = $this->getProduct(); $manufacturerName = $_product->getAttributeText('brands'); $manufacturerId = $_product->getManufacturer(); echo '<a href="/catalogsearch/result/?manufacturer='.$manufacturerId.'&q='.$manufacturerName.'">' ?> <img src="../media/wysiwyg/brands/<?php echo str_replace(' ', '-',strtolower($manufacturerName)); ?>.png" alt="<?php echo $manufacturerName; ?>" title="<?php echo $this->__('to see more from') ?>&nbsp;<?php echo $manufacturerName; ?>&nbsp;<?php echo $this->__('click here') ?>" /></a>
</div>

I have also read that I should create a custom module, and used this page as an example, but can't figure out how to put the php code in.

Was it helpful?

Solution 2

The code that worked for me in my custom template was:

<?php
/**
 * Brand Listing
 *
 *
 */
?>

<?php
$product = Mage::getModel('catalog/product');

$attributes = Mage::getResourceModel('eav/entity_attribute_collection')
    ->setEntityTypeFilter($product->getResource()->getTypeId())
    ->addFieldToFilter('attribute_code', 'brands');

$attribute = $attributes->getFirstItem()->setEntity($product->getResource());

$manufacturers = $attribute->getSource()->getAllOptions(false);

?>
<ul id="manufacturer_list">
    <?php foreach ($manufacturers as $manufacturer): ?>
        <li><a href="/catalogsearch/advanced/result/?brands[]=<?php echo $manufacturer['value'] ?>">
        <img src="/media/wysiwyg/brands/<?php echo str_replace(' ', '-',strtolower($manufacturer['label']))?>.png" alt="<?php echo $manufacturer['label'] ?>" title="<?php echo 'View products from &nbsp;' . $manufacturer['label'] ?>" /></a></li>
    <?php endforeach; ?>
</ul>

And for the search to work, make sure that in Magento Admin, the attribute is set to be used in Advanced Search.

OTHER TIPS

What I would do is make a cms template specific to the brands page. You can follow the example here to make a template: https://stackoverflow.com/questions/31765194/add-layout-identifier-in-magento-admin-panel/31766410#31766410

And inside template/page/newlayout.phtml

You copy the template that you normally use but make your edits, meaning inserting your php code:

 <div class="brandImage">

                    <?php 

                        $_product = $this->getProduct(); 
                        $manufacturerName = $_product->getAttributeText('brands');
                        $manufacturerId = $_product->getManufacturer(); 
                    ?>

                    <a href="/catalogsearch/result/?manufacturer=<?php echo $manufacturerId.'&q='.$manufacturerName ?>">

                        <img src="../media/wysiwyg/brands/<?php echo str_replace(' ', '-',strtolower($manufacturerName)); ?>.png" alt="<?php echo $manufacturerName; ?>" title="<?php echo $this->__('to see more from') ?>&nbsp;<?php echo $manufacturerName; ?>&nbsp;<?php echo $this->__('click here') ?>" />        
                    </a>

                </div>

Then go to the cms page and assign it your new layout.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top