want level 2 category name in magento - php

$product = Mage::getModel('catalog/product')->load($item->getProductId());
$pro = $product->getCategoryName(); //category id is fetched here
$category = Mage::getModel('catalog/category')->load($pro);
Root Catalog
Furniture
Electronics
Computer
Processor
Apparel
So when i get processor as category name, i want to display its level 2 category name that is Electronics

got the solution, The code below will get the current category and then keep getting the parent category until it gets the highest category (but not the root category)
$product = Mage::getModel('catalog/product')->load($item->getProductId());
$pro = $product->getCategoryName(); //category id is fetched here
$category = Mage::getModel('catalog/category')->load($pro);
if ($category)
{
while($category->getLevel() != 2)
{
$category = $category->getParentCategory();
if (!$category)
{
break;
}
}
if ($category)
{
echo $category->getName();
}
else
{
echo 'Cannot find parent category';
}
}

$CategoryCollection=Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('level',2);

You can use this
$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*')
->addIsActiveFilter()
->addAttributeToFilter('level',2)
OR
<?php
$_category = Mage::registry('current_category');
$_category = $this->getCurrentCategory();
// Category Name
echo $_category->getName();
// Category Level
echo $_category->getLevel();
?>

Yo can use something like this
public function get_cat_from_product($prod_id,$level){
$categoryIds = Mage::getModel('catalog/product')->load($prod_id)->getCategoryIds();
if(count($categoryIds) ){
$firstCategoryId = $categoryIds[$level-1]; //level 2 -> pos 1
$_category = Mage::getModel('catalog/category')->load($firstCategoryId);
echo $_category->getName();
}else{
return null;
}
}

Related

How can I get category name of post in news module from category module in CodeIgniter HMVC

I want to get category name for news post. I can easily get category id but I want to get category name using category Id or other method. How should I do that?
This is how I displayed my blog post in controller
function post($slug = FALSE)
{
if (isset($slug)) {
$data['query'] = $this->mdl_blogs->get_where_slug($slug);
$data['view_file'] = "blog_view";
$this->load->module('template');
$this->template->public_one_col($data);
}
}
in model
function get_where_slug($slug){
$table = $this->get_table();
$this->db->where('news_slug', $slug);
$query=$this->db->get($table);
return $query;
}
in view
<?php
foreach ($query->result() as $row) {
$data['news_title'] = $row->news_title;
$news_body = $row->news_body;
$news_slug = $row->news_slug;
$category_id = $row->category_id;
//$data['category_name'] = $row->category_name;
//$news_category = $row->news_category;
?>
<h2><a href="<?php echo (base_url().'blogs/post/'.$news_slug) ;?>">
<?php echo $data['news_title'];?></a></h2>
<p><?php echo $news_body;?></p>
<p><?php echo $category_id;?></p>
<?php
}
?>
<?php
//echo Modules::run('comments');
?>
Here I also want to display category name. I want to get it from categories table
Here are my tables
in News table
news_id
category_id
news_slug
......
in categories
category_id
category_name
category_slug
You need to join your category table in your model
function get_where_slug($slug){
$table = $this->get_table();
$query=$this->db
->select('n.*,c.category_name,c.category_slug')
->from($table.' n')
->join('category c','n.category_id=c.category_id','LEFT')
->where('n.news_slug', $slug)
->get();
return $query;
}
And in your view you can have you category name in foreach loop as
$category_name = $row->category_name;
same for category_slugif you want this too

Woocommerce - single product - show different content for different products

I'd like to show different content on single product page for different categories.
Sample code
<?php if(in_category('62')){ ?>Text for category 62<?php }?>
<?php if(in_category('21')){ ?>Text for category 21<?php }?>
Make the new template and name it: taxonomy-product_cat-Your_category_product_slug.php
Path: /wp-content/your-theme/woocommerce/taxonomy-product_cat-Your_category_product_slug.php
Another way to do this:
$cat1 = 62;
$cat2 = 21;
$terms = wp_get_post_terms($post->ID, 'product_cat');
if (!empty($terms)) {
foreach ($terms as $t) {
if ($t->term_id == $cat1) {
woocommerce_get_template_part('single-product-product1');
} else if ($t->term_id == $cat2) {
woocommerce_get_template_part('single-product-product2');
}
}
}

Mageto - Filter Category Navigation in tree like state

I created a code to filter the product collection by categories. Making its own left side block using the following code.
I have it to work perfect for filtering, I am just having a hard time rendering the display.
The Level 2 categories work fine and only show
Top Level Category 1
Top Level Category 2
Once selected it then shows sub categories and their sub categories..
Sub Category of 1
Sub sub category of ^^^
Sub sub category of ^^^
Sub sub category of ^^^
Sub sub sub category of ^^^
Sub Category of 1
Sub sub category of ^^^
Sub sub category of ^^^
Sub sub category of ^^^
Sub sub sub category of ^^^
I need it to break down like the first level did. What would be the best method?
<?php
$root_category_id = Mage::app()->getStore()->getRootCategoryId();
$filterCategoryId = Mage::app()->getRequest()->getParam('cat');
$products = Mage::getSingleton('catalogsearch/advanced')->getProductCollection();
$catIds = array();
if (isset($products)) foreach ($products as $product) {
if ($product->isSaleable()) {
$ids = $product->getCategoryIds();
foreach ($ids as $id) $catIds[$id] = 1;
}
}
if (!isset($catIds)) return false;
$categories = array();
// Filters rest of categories
if ($filterCategoryId) {
$filterCategory = Mage::getModel('catalog/category')->load($filterCategoryId);
$filterChildren = $filterCategory->getAllChildren(true);
foreach ($catIds as $id => $x) {
$category = Mage::getModel('catalog/category')->load($id);
if (in_array($root_category_id, $category->getParentIds()) && in_array($id, $filterChildren) && $id != $filterCategoryId) {
$categories[$id] = $category->getName();
} else {
unset($catIds[$id]);
}
}
} else {
// Filters the first level categoires
foreach ($catIds as $id => $x) {
$category = Mage::getModel('catalog/category')->load($id);
if (in_array($root_category_id, $category->getParentIds()) && $category->getLevel() == 2) {
$categories[$id] = $category->getName();
} else {
unset($catIds[$id]);
}
}
}
if(isset($categories) && sizeof($categories) > 0):
$url = Mage::app()->getRequest()->getRequesturi();
$url = Mage::helper("core/url")->removeRequestParam($url, 'cat');
?>
<div class="page-subtitle" style="margin:6px auto 14px 6px;"><h2>Shop By Category</h2></div>
<ul class="brandsnav">
<?php foreach($categories as $id=>$name): ?>
<li class="cf">
<span class="subnav_trigger"></span>
<a href="<?php
echo Mage::helper("core/url")->addRequestParam($url, array("cat"=>$id)); ?>"><?php echo $name;?></a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
I just had to add conditions to the foreach statements. Got it working.

Add active class to current subcategory

With the code blow I retrieve the current parent category and his children. Is it possible to add an active class for the current child?
<?php
$currentCat = Mage::registry('current_category');
if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() )
{
$loadCategory = $currentCat;
}
else
{
$loadCategory = Mage::getModel('catalog/category')->load($currentCat->getParentId());
}
$subCategories = explode(',', $loadCategory->getChildren());
foreach ( $subCategories as $subCategoryId )
{
$cat = Mage::getModel('catalog/category')->load($subCategoryId);
if($cat->getIsActive())
{
echo '<li>'.$cat->getName().'</li>';
}
}
?>
You already have the current category, so you can check it's ID against the IDs of the categories being looped through.
Where you have
echo '<li>'.$cat->getName().'</li>';
Change it to do the check for the ID and then add the "active" class when found
$class = '';
if ($currentCat->getId() == $cat->getId())
{
$class = ' class="active"';
}
echo '<li'.$class.'>'.$cat->getName().'</li>';
To set the category as active we have to set the current category in catalog layer.
$_categoryName=YourCategoryName;
$_category = Mage::getModel('catalog/category')->loadByAttribute('name', $_categoryName);
Mage::getSingleton('catalog/layer')->setCurrentCategory($_category);
This looks better than ugly hack of changing in phtml filter..

Showing only the first level of sub categories for the current category

I am using the following code to display the sub categories of the current category you are own.
However I have 2 levels of sub cats so I need to be able to modify the code somehow to only show the 1st level of sub categories for the current category you are own instead of both levels?
<?php
/* Load category by id */
$cat = Mage::registry('current_category');
/*Returns comma separated ids*/
$subcats = $cat->getChildren();
foreach(explode(',',$subcats) as $subCatid)
{
$_category = Mage::getModel('catalog/category')->load($subCatid);
if($_category->getIsActive()) {
echo '<li>'.$_category->getName().'</li>';
/* Load category by id */
$sub_cat = Mage::getModel('catalog/category')->load($_category->getId());
/*Returns comma separated ids*/
$sub_subcats = $sub_cat->getChildren();
foreach(explode(',',$sub_subcats) as $sub_subCatid)
{
$_sub_category = Mage::getModel('catalog/category')->load($sub_subCatid);
if($_sub_category->getIsActive()) {
echo '<li class="sub_cat">'.$_sub_category->getName().'</li>';
}
}
}
}
?> `
If you just want to show the first level subcategories, then just get rid of
/*Returns comma separated ids*/
$sub_subcats = $sub_cat->getChildren();
foreach(explode(',',$sub_subcats) as $sub_subCatid)
{
$_sub_category = Mage::getModel('catalog/category')->load($sub_subCatid);
if($_sub_category->getIsActive()) {
echo '<li class="sub_cat">'.$_sub_category->getName().'</li>';
}
}
Since you're now loading the current category through $cat = Mage::registry('current_category'); and fetches is subcategories that you loop through (first level).
You may use the $_sub_category->getLevel() to check the current level.
Try re-writing the below code
if($_sub_category->getIsActive()) {
to
if($_sub_category->getIsActive() && $_sub_category->getLevel() == 2) {
Please make sure getLevel() returns the appropriate level (can't remember it is 1,2 or 3).

Categories