Home » Archive

Articles tagged with: category

Magento, MySQL »

[31 Dec 2009 | 3 Comments | 579 views]

Suppose, you have a product collection and you want to filter it by category. Suppose, you want to filter it by more than one category. You can use addCategoryFilter if you have only one category. But, what if you want to filter by more than one category?
Category ids are stored for product in a comma separated way. So, to filter product collection by more than one category, you have to use:
addAttributeToFilter(‘category_ids’,array(‘finset’=>$categoryIds));

Magento »

[22 Dec 2009 | No Comment | 321 views]

You can get current category from the following code:

$currentCategory = Mage::registry(‘current_category’);

You can get the parent category from the following code:

Magento »

[21 Dec 2009 | No Comment | 553 views]

Here, I will be showing you the code to get sub categories of a particular category and the number of products (product count) present in the sub categories.
Like, I have a category named Furniture. The sub categories under Furniture are Living Room and Bedroom. Now, I want to show the sub categories under Funiture and the products associated with the sub categories (Living Room and Bedroom).

Magento »

[7 Dec 2009 | No Comment | 319 views]

$product->getCategoryIds() gives array of category ids which the product is associated to. We can loop through this array and load each category to get the category name and url.

<?php
/**
* get categories from a product
*/
$categoryIds = $product->getCategoryIds();
/**
* looping through the array of category ids
*/
foreach($categoryIds as $categoryId) {
$category = Mage::getModel(‘catalog/category’)->load($categoryId);
?>
Category: <a href="<?php echo $category->getCategoryUrl() ?>"><?php echo $category->getName() ?></a><br/>
<?php
}
?>