Load Category/Product by Attribute

If you ever need to manually load a category or product you can do it either by id or by attribute:

loadById($id)

$_product= Mage::getModel('catalog/product')->load(25);
$_category = Mage::getModel('catalog/category')->load(10);

loadByAttribute($attribute, $value)

//Load by Sku
$_product = Mage::getModel('catalog/product')->loadByAttribute('sku', 'product001');

//Load by Name
$_product = Mage::getModel('catalog/product')->loadByAttribute('name', 'Product Name');
$_category = Mage::getModel('catalog/category')->loadByAttribute('name', 'Category Name');

This method will working with any attribute, but be careful; if the attribute matches more than one record only the first record will be returned

Related Posts:

Comments (2)

  • Nice blog post about Load Category/Product by Attribute

    Reply

Post A Comment