We have accounting software populating the name field on a magento webstore, the problem is there’s a 30 character limit and the name always tends to be aimed more at our inhouse operations than SEO or UI.
I’m wondering the best way to create an ‘alternate name’ for some magento products, but only if it exits, otherwise fall back to the regular name field.
Is the best way to do this to just create a new Product Attribute (alternate_name) and the call an if else statement in the frontend?
<?php if($_product->getAttributeText(’alternate_name’)) {
echo $_product->getAttributeText(’alternate_name’)
} else {
echo $_product->getName();
} ?>
Will I then need to change it in several places in the layout folder? or is there an easier place to override this method once and have it work everytime $product->getName() gets called?
If you need this for reading/viewing in the frontend only, you could override the
getName()method of the core modelMage_Catalog_Model_Productto do something like this:}
This way you’d have just a single point of change, serving all product
getName()calls, no matter where they’re coming from.Be aware that doing so can cause issues when it comes to editing/saving the
nameattribute.This is because the Magento backend for example will also use your
getName()to fill thenamefield of the product edit<form>, so the value landing in this<input>field could be either the one of thealternate_nameor thenameattribute.Saving changes could then lead to saving the
alternate_namevalue in thenameattribute, which you most probably don’t want.To avoid this, the method above always returns the value of the
nameattribute in case the call is coming from within the admin area. This should be enough to catch admin edit forms, but can of course not catch other possible cases, wheregetName()is used to feedsetName()->save()calls.