Magento Catalog Product SubCategory Name - magento

I am implementing the Magento catalog product view page, and I need the name of subcategory the product is added to.
The code to display the category is like: $_product->category->name
but I am unable to get the sub category name.

to get the subcategory of a category, use the below code
$your_category_id = '2334'; // category_id whose subcategory you want to fetch
$subcats = Mage::getModel('catalog/category')->getCategories($your_category_id);
foreach ($subcats as $sub) {
echo $sub->getName();
}

Well I got the sub categories the following way:
<?php
if (Mage::registry('current_product')) {
if ($_product) {
$categoryIds = $_product->getCategoryIds();
$cat_id = $categoryIds[0]; ----> pass the level of sub catgeory to the array index
$category = Mage::getModel('catalog/category')->load($cat_id);
$cat_name = $category->getName();
}
}
?>

Related

How to get the category name in product/view.phtml from product_viewed.phtml?

How can I get the category name from product_viewed.phtml list?
I get an error when I land on the product page frontend\base\default\template\catalog/product/view.phtml
if I click a product from frontend\base\default\template\reports/product_viewed.phtml
This is the code on my view.phtml
<?php
$_helper = $this->helper('catalog/output');
$_category_detail = Mage::registry('current_category');
var_dump($_category_detail->getName()); //gives current category name
?>
This is the error I get,
Fatal error: Call to a member function getName() on a non-object in
C:\xxx\frontend\base\default\template\catalog\product\view.phtml on
line 86
But I don't have this error, if I click the product from frontend\base\default\template\catalog/product/list.phtml
Any idea how can I get the category name from these two different situations?
I think you can get the category id from product name or id by loading model of product.
such as following code.
$catCollection = $_product->getCategoryCollection();
foreach($catCollection as $cat){
print_r($cat->getData());
//echo $cat->getName();
//echo $cat->getUrl();
}
Another Way,
$_product= Mage::getModel('catalog/product');
$catIds = $_product->getCategoryIds();
After that you can get the categories by accessing $catIds array such as $catIds[0], $catIds[1] etc.
You can't get Mage::registry('current_category') on catalog/product/view.phtml if you access a product with direct link. But if you follow the proper channel like, click on any category then select a product, this particular category will get registered.
You should use the following code where ever you are displaying the categories name:
if(Mage::registry('current_category')){
echo Mage::registry('current_category')->getName();
}else{
$categories = $_product->getCategoryCollection()
->addAttributeToSelect('name');
foreach($categories as $category) {
echo $category->getName();
}
}
tealou,
Mage::registry('current_category') depends on Product url if product url have category id in this routing url ,then you get Mage::registry('current_category'),else you did not get Mage::registry('current_category') value.
It is logic of url_rewrite feature.
Suppose you have product testing(id 235 ) and it categories catone(id 5),cattwo(id 7),catthree (id 11)
for product url
testing.html and target path is catalog/product/view/id/235/
catone/testing.html and target path is catalog/product/view/id/235/category/5
cattwo/testing.html and target path is catalog/product/view/id/235/category/7
catthree /testing.html and target path is catalog/product/view/id/235/category/11
First url not have categoryid so if you click on this url from any category page is not give any value Mage::registry('current_category')
Other have category it will give Mage::registry('current_category') value in view.phtml of product page.
Goto Admin>Catalog>Url rewrite manager her you are find this url
If want to Mage::registry('current_category') then you need to create a new Mage::registry() varible from /catalog/category/view.phtml i.e category page.
$var=Mage::registry('current_category');
Mage::register('some_name', $var);
and fetch in catalog/product/view.phtml using
$my_var = Mage::registry('some_name');
This some_name variable will get when if you will come to product page from category.
put code like
if(Mage::registry('some_name')){
var_dump(Mage::registry('some_name'))->getName());
//gives current category name
}
You can not get current category on product page b\c one product can have many categories. What if customer comes to your product page from google.com, for example? What category will this be?
There is no such Mage::registry('current_category') variable on product page. Forget it. This variable exists only on category pages. On product page you can only check if product is in some category.
almost as MTM has been said, I use this:
if(Mage::registry('current_category')){
$catid = Mage::registry('current_category')->getId();
$cat = Mage::getModel('catalog/category')->load($catid);
} else {
//because sometimes we can not use registry('current_category'), so we use:
$cat_ids = $_product->getCategoryIds(); // get all categories where the product is located
$cat = Mage::getModel('catalog/category')->load( $cat_ids[0] ); // load first category
}
Here is the code i use in Magento 1.8
<?php $cats = $_product->getCategoryIds();
foreach ($cats as $category_id) { $_cat = Mage::getModel('catalog/category')->load($category_id) ;
echo $_cat->getName();
}?>

How to get subcategories of a category in magento

how can I get the all subcategories of a category in magento if there are many nested subcategories?
Thanks
$subcats = Mage::getModel('catalog/category')->getCategories($your_category_id);
foreach ($subcats as $sub) {
echo $sub->getName();
}

get the parent category ids in magento

i put the following code into magento base template file view.phtml.
$categories = $_product->getCategoryIds();
print_r($categories)
with the sample data,when on the Ottoman product page,which breadcrumb is
Home / Furniture / Living Room / Ottoman
the output is Array ( [0] => 22 ) the 22 is Living Room id. how to get Furniture and Living Room id. thank you
$categories=$_product->getCategoryIds();
foreach ($categories as $category)
{
$info = Mage::getModel('catalog/category')
->load($category)
->getParentCategory();
echo $info->getId(); //display category ID
//var_dump($info->debug()); # display model contents
}
Here is how you can get all categories id's:
//load root category
$rootCategoryId = Mage::app()->getStore()->getRootCategoryId();
$rootCategory = Mage::getModel('catalog/category')
->load($rootCategoryId);
//load all children categories of the root category
$childrenCategories = $rootCategory->getChildrenCategories();
Now you can have access to their ids like so:
foreach ($childrenCategories as $category) {
var_dump($category->getId());
}

Get 'active from' attribute from category in magento 1.7

There are pre-defined attributes in Mangento 1.7 categories named
active from
active to
I can fetch Name of the current product's category using this:
$productId=$_helper->productAttribute($_product, $_product->getId(), 'id');
$product = Mage::getModel('catalog/product')->load($productId);
$cats = $product->getCategoryIds();
foreach ($cats as $category_id) {
$_cat = Mage::getModel('catalog/category')->load($category_id) ;
echo $_cat->getName();
} ?>
but I need to find the active from date of current category also.
How do I fetch this?
It is probably worth noting that these values are for the active state of a custom design for the category, and not the actual active state of the category itself...
As an array together:
$_cat = Mage::getModel('catalog/category')->load($category_id);
$_customDesignDates = $_cat->getCustomDesignDate();
Or individually:
$_cat = Mage::getModel('catalog/category')->load($category_id);
$fromDate = $_cat->getData('custom_design_from');
$toDate = $_cat->getData('custom_design_to');

Get name of particular/specific category name of root category in magento

In magento site I have multiple sub categories under the root category. I want name or id of only one particular/specific sub category. How can i get that?
To load particular category you can define the id like i have used 4
<?php $_helper = $this->helper('catalog/output'); ?>
$category_id = $this->getCategoryId();
$childrens = Mage::getModel('catalog/category')->getCollection()
->addFieldToFilter('parent_id', 4)
->addAttributeToSort('name', 'ASC');
?>

Resources