Sometimes we need to add new attributes to a Magento store. Sometimes that store is new and hasn’t been populated, sometimes it’s got 20,000 products in it. I recently wrote an article about Attribute Mapping with Google Content in Magento and this is a prime example. We needed to add a new attribute “Google Title” which included the brand name (and then map it to Google Content).
So, how can we do this?
The easiest way would be to create the new attribute and then assign it to the attribute set for your products. We can then get a list of all the products, cycle through them and add this new value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
error_reporting(E_ALL); ini_set('memory_limit', '1024M'); require_once 'Mage.php'; umask(0); Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); $collection = Mage::getModel('catalog/product') ->getCollection(); $gBrand = 'My Brand Name'; foreach($collection as $product) { $_product = Mage::getModel('catalog/product')->load($product->getId()); $_product->setGoogleTitle($gBrand . ' ' . $_product->getName()); $_product->save(); } |
With this code, we’d have an attribute called google_title
. We could use the same base code to quickly update existing Magento information (such as name, description etc)
Image Credit: wili_hybrid
Comment or tweet @douglasradburn