Add active class to current subcategory - php

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..

Related

want level 2 category name in magento

$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;
}
}

WordPress sidebar navigation based off of top-level parent page

I'm trying to create a complex sidebar navigation system that remains the same depending on what top-level page you're viewing. For example, say I have this navigation:
Home
About
Our Company
Location
Hours
Our PeopleJim
Dave
Sarah
Kelly
PortfolioLogos
Websites
Contact
Now, if the user was anywhere within the About section, I'd want the sidebar to display:
About
Our Company
Location
Hours
Our PeopleJim
Dave
Sarah
Kelly
That goes for if they're in the main About page, in the Our Company page, or in the Location page. I need the whole navigation visible regardless of depth.
If the user was in a page with no sub-items, such as Contact, the sidebar needs to show:
Home
About
Portfolio
Contact
What's more, the ordering needs to be based on a WordPress Menu (one main menu, each sidebar can't be it's own; that's too complicated for the user). I don't know if that complicates things or not.
In the past, I've managed to get something to display children and sibling pages, but it doesn't display parent pages, and doesn't display in the order that the user defines.
<ul>
<?
global $wp_query;
if( empty($wp_query->post->post_parent) ) {
$parent = $wp_query->post->ID;
}
else {
$parent = $wp_query->post->post_parent;
}
wp_list_pages ("&title_li=&child_of=$parent");
?>
</ul>
If this can be modified to work the way I want it, that'd be great. I'm going to try and figure this out on my own, and will post updates as I make progress.
UPDATE 1: I've made a bit of progress determining what the absolute parent is. I think I'm on the right track.
<?
if ($post->post_parent) {
$ancestors = get_post_ancestors($post->ID);
$root = count($ancestors)-1;
$parent = $ancestors[$root];
} else {
$parent = $post->ID;
}
echo "Current Page: " . $post->ID . "<br />";
echo "Top Level Parent: " . $parent;
?>
UPDATE 2: I can now determine the correct page ID to query for a menu, so I think I'm getting close.
<?
$children = get_pages("child_of=".$post->ID);
if ($post->post_parent) {
$ancestors = get_post_ancestors($post->ID);
$root = count($ancestors)-1;
$parent = $ancestors[$root];
} elseif (count($children) != 0) {
$parent = $post->ID;
} else {
$parent = 0;
}
echo "Current Page: " . $post->ID . "<br />";
echo "Top Level Parent: " . $parent;
?>
UPDATE 3: I'm nearly there! The only problem is this uses the page's order from the editor, not from the menu. Is it possible to edit this to work with a menu instead?
<aside>
<?
$children = get_pages("child_of=".$post->ID);
if ($post->post_parent) {
$ancestors = get_post_ancestors($post->ID);
$root = count($ancestors)-1;
$parent = $ancestors[$root];
$sidebarDepth = 0;
$postParentID = get_post($parent);
$title = $postParentID->post_title;
} elseif (count($children) != 0) {
$parent = $post->ID;
$sidebarDepth = 0;
$postParentID = get_post($parent);
$title = $postParentID->post_title;
} else {
$parent = 0;
$sidebarDepth = 1;
$frontPageID = get_option("page_on_front");
$exclude = $frontPageID;
$postParentID = get_post($frontPageID);
$title = $postParentID->post_title;
}
?>
<header>
<h6><? echo $title ?> »</h6>
</header>
<section>
<nav>
<?
echo "<ul>";
wp_list_pages("child_of=" . $parent . "&depth=" . $sidebarDepth . "&exclude=" . $exclude . "&title_li=&");
echo "</ul>";
?>
</nav>
</section>
</aside>
Figured it out! I modified the code from this answer to add a start_in option to wp_nav_menu, and modified my code from there. Now this works exactly as I wanted it.
In functions.php:
// add start_in argument to navigation
add_filter("wp_nav_menu_objects",'my_wp_nav_menu_objects_start_in',10,2);
function my_wp_nav_menu_objects_start_in( $sorted_menu_items, $args ) {
if (isset($args->start_in) && $args->start_in != 0) {
$menu_item_parents = array();
foreach ($sorted_menu_items as $key => $item) {
if ($item->object_id == (int)$args->start_in) $menu_item_parents[] = $item->ID;
if (in_array($item->menu_item_parent, $menu_item_parents)) {
$menu_item_parents[] = $item->ID;
} else {
unset($sorted_menu_items[$key]);
}
}
return $sorted_menu_items;
} else {
return $sorted_menu_items;
}
}
In page.php (or whatever template you want):
<aside>
<?
if ($post->post_parent) {
$ancestors = get_post_ancestors($post->ID);
$root = count($ancestors)-1;
$parent = $ancestors[$root];
} elseif (count(get_pages("child_of=".$post->ID)) != 0) {
$parent = $post->ID;
} else {
$parent = get_option("page_on_front");
$sidebarDepth = 1;
$exclude = $parent;
}
if ($post->post_parent || count(get_pages("child_of=".$post->ID)) != 0) {
$sidebarDepth = 0;
$start_in = $parent;
$depth = 0;
} else {
$depth = 1;
$start_in = 0;
}
$parentID = get_post($parent);
$parentTitle = $parentID->post_title;
$parentURL = get_permalink($parentID);
?>
<header>
<h6><? echo $parentTitle ?> »</h6>
</header>
<section>
<nav>
<?
wp_nav_menu(
array(
"container" => false,
"depth" => $depth,
"items_wrap" => '<ul>%3$s</ul>',
"start_in" => $start_in,
"theme_location" => "first"
)
);
?>
</nav>
</section>
</aside>

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.

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