- October 2019
- June 2019
- September 2018
- May 2018
- December 2017
- April 2017
- June 2016
- February 2016
- November 2015
- January 2015
- August 2014
- July 2014
- May 2014
- March 2014
- February 2014
- January 2014
- November 2013
- August 2013
- June 2013
- May 2013
- April 2013
- March 2013
- February 2013
- December 2012
- November 2012
- September 2012
- June 2012
- April 2012
- March 2012
- February 2012
- January 2012
- December 2011
- November 2011
- September 2011
- July 2011
- June 2011
- May 2011
- March 2011
- January 2011
- October 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- September 2009
- July 2009
- June 2009
- May 2009
- March 2009
- September 2008
- April 2008
- December 2007
- June 2007
- June 2005
- September 2004
- May 2002
- October 2001
- August 2001
2012-11-26: Magento: Loading a category or product by URL key
Sometimes you want to load a specific category (or product) in Magento. It's easy enough to load an object by its ID number, but magic numbers are bad. Besides, the number could differ between your development environment and the live production site. Fortunately, it is also possible to load an object by its url_key.
Normally, you might load a category by ID through the simple code $cat = Mage::getModel ('catalog/product')->load ($id);. To load the category by URL key, a bit more code is needed. Here's how:
<?php
$category = Mage::getModel ('catalog/category')
->getCollection ()
->addAttributeToFilter ('url_key', 'sales') //load the "sales" category
->getFirstItem (); //only 1 result
?>
The same works with products (but use the catalog/product model instead of catalog/category).
Where's the name?
If you try to call getName() on the returned category object, it won't return the actual category name. You might not need it (for instance, when you're only querying the products in that category), but then again, you might. Even more so for product objects. You can tell Magento to include the name (or any other attribute) before querying it:
<?php
$category = Mage::getModel ('catalog/category')
->getCollection ()
->addAttributeToSelect ('name') //include the "name" property in the returned object
->addAttributeToFilter ('url_key', 'sales')
->getFirstItem ();
?>
What about the price?
Using that category to load products? You probably want to include at least the name of that product and the price you expect customers to pay for it. Here's another snippet:
<?php
$products = $category->getProductCollection ()
->addAttributeToSelect ('name') //yes, I want the "name" property again
->addFinalPrice () //and give me the price too!
->setPageSize (3); //but only give me 3 products
foreach ($products as $product)
{
/* do stuff here */
}
?>
What about everything else?
If you really want to load everything about a product or category, use "*" as the parameter to addAttributeToSelect().
Comments
Hi, just wanted to let you know that your code snippets all got jumbled togther on one line which is making your comments color out the rest of the code.
Post a comment