1

So I know how I just don't know if my method is the best practice/"easy" way to do it.

The places I plan to show brand/manufacturer name is: (always after product name)

  • mini-cart
  • checkout/cart
  • product page
  • wishlist
  • product grid
  • order email (items.phtml)
  • related products
  • upsell
  • new.phtml

What I'm using now:

product/view.phtml:

<? echo $_product->getAttributeText('manufacturer') ?>

template/checkout/cart/item/default.phtml (example: showing product name aswell here)

<a href="<?php echo $this->getProductUrl() ?>"><?php echo $this->htmlEscape($this->getProductName()) ?>
<br>
<?php $_product = Mage::getModel('catalog/product')->load($_item->getProductId());
$manufacturerName = $_product->getAttributeText('manufacturer') ; ?>
<?php echo $manufacturerName; ?>
</a>

Any ideas?

1
  • The code looks functional. But loading a whole product model for a single attribute is a terrific waste of resources - this is the least efficient approach possible. Commented Feb 11, 2013 at 9:29

3 Answers 3

2

Honest truth? Your code works fine, but isn't good and isn't best practice. Loading a whole product model to a fetch a single attribute is wasteful. Instead consider the following.

If you want the name to be displayed everywhere in that format, then you've got a number of options.

  1. You could just extend the product model and replace the getName() method with one that appends the manufacturer name. You just need to add a conditional statement to it so that store code 0 doesn't get this variant. That way editing it in the admin won't keep adding the manufacturer each time you open it. This means nothing is hard-coded, but will execute at run time, each time a product is loaded. No template files have to be modified.

  2. You could add an observer to attach to the product save, which just alters the name to suit your requirements. Then saves the original name in another field. This would mean it is hard coded, but would perform far better as it isn't running at run time. No template files have to be modified.

  3. Add the attribute to be selected to every collection as necessary, then edit every template where the product name is displayed. This will execute each time a product is loaded and would require template files to be modified.

There's always going to be a balance between flexibility and performance. So choose appropriately.

Also, rather than ever loading an entire product model to grab one attribute, instead edit the collection being loaded itself to fetch that attribute. Eg

$_productCollection = $category->getProductCollection() ->addAttributeToSelect('manufacturer');

This is much more lightweight and is best practice.

Option 2 is a much quicker approach in terms of execution time and deployment. And on a busy store would probably be the approach we would take.

1

You could load the attribute in your basic collection trough a config.xml this will avoid loading a new collection. For example if you want to do this in the cart you could do:

<sales>
      <quote>
          <item>
              <product_attributes>
                  <manufacturer/>
              </product_attributes>
          </item>
      </quote>
  </sales>

When you take a look at the catalog config you can also see

<frontend>
    <product>
        <collection>
            <attributes>
               <manufacturer /> 
            </attributes>
        </collection>
    </product>
</frontend>

For this you will need to create a custom extension...

This will allow you to just get the attribute out of the product / quote item

$_item->getAttributeText('manufacturer');
0

I would create a helper with a method which works similar like the currency method. So instead of this:

echo Mage::helper('core')->currency($_price);

You would have something like this:

echo Mage::helper('yourmodule/product')->formatName($product);

which outputs: "Router x - Cisco". And maybe add parameters to change formatting.

You would still need to change every template, but at least now you have a helper that takes the responsibility of formatting the name out the templates. Obviously this has it's benefits like if you wanted to add the color to the name as well, you just have to change one method instead of all the templates.

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.