1

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.

2 Answers 2

1

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.

2
  • Thank you! I was just on the track of creating a specific template file, the code gives me an error saying 'call to member function on non-object'
    – k1rah
    Commented Aug 2, 2015 at 0:06
  • 1
    @k1rah it is probably because of $_product = $this->getProduct(); replace it with: $_product = Mage::getModel('catalog/product')->load($_item->getProductId()); Commented Aug 2, 2015 at 0:26
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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