How can I set the subcategories to be visible at all times in main navigation block (left column)? Once I start browsing the subcategories of any category, the navigation block is showing only the top categories. Example at www.valikoi.com.
Regards, Tommi
I think this will help you,
<ul class="catlist">
<?php
$obj = new Mage_Catalog_Block_Navigation();
$store_cats = $obj->getStoreCategories();
$current_cat = $obj->getCurrentCategory();
$current_cat = (is_object($current_cat) ? $current_cat->getName() : '');
$currentCategory = Mage::registry('current_category');
foreach ($store_cats as $cat) {
if ($cat->getName() == $current_cat) {
//enters and lists all main categories
echo '<li class="current">
'.$cat->getName()."\n<ul>\n";
//get the child category of a current category
foreach ($obj->getCurrentChildCategories() as $subcat) {
echo '<li>
'.$subcat->getName()."</li>\n";
}
echo "</ul>\n</li>\n";
} else {
//enters when a sub category is clicked and lists all main categories
echo '<li>
'.$cat->getName()." </li>\n";
//this is to load the categories like in admin order
$loadCategory= Mage::getModel('catalog/category')->getCategories($currentCategory->getParentId(),0, true, true);
//below code did not work to load categories like in admin order
//$explod= $loadCategory->getChildrenCategories('position','asc');
//$subCategories = explode(',', $explod);
if (($currentCategory->getParentId() == $cat->getId()) )
{
//enters when a sub category is clicked and loads its parent and show its subcategories
echo '<ul class="catlist_inner">';
foreach ($loadCategory as $subCategoryId)
{
if($subCategoryId->getIsActive())
{
echo '<li>
'.$subCategoryId->getName().' </li>';
}
}
echo '</ul>';
}
}
}
?>
</ul>
Related
Hello i need to get the quantity of the item added in cart for the item i am viewing in product detail
atm i am using this
<?php $cart = Mage::getModel('checkout/cart')->getQuote();
$result = array();
foreach ($cart->getAllItems() as $item) {
$result['qty'] = $item->getQty();
}
$qty=$result['qty'];
//echo $qty ;
if(!$qty == 0){
echo '<span>'.$qty.'</span>';
}else{
echo "<span>0</span>";
}?>
but it returns only the last qty added/updated in the cart, and i need the specific single product qty
Try this:
//$product is current product that you are viewing
$qty = null;
foreach ($cart->getAllItems() as $item) {
if ($product->getId() == $item->getProductId()) {
$qty = $item->getQty();
}
}
if (!is_null($qty)) {
//$qty is what you are looking for
}
else {
//the current product is not in the cart
}
I have the following category setup in my magento store:
store root
|
bedroom kitchen bathroom
| | |
furniture furniture furniture
lighting lighting misc
misc
I need to be able to check if the category misc exists as a subcategory in my current category or not, and if so show a particular block.
So if i was in the kitchen category it would not show but in bedroom and bathroom it would.
How can i do this this check?
To get your sub-category misc
$currentCategoryId = 10;
$collection = Mage::getModel('catalog/category')->getCollection()
->addAttributeToFilter('is_active', 1) //only active categories
->addAttributeToFilter('parent_id', $currentCategoryId)
->addAttributeToFilter('name', 'misc');
To check if your collection contains at least 1 record in it:
if ($collection->getSize() > = 1) {
//'misc' sub-category exists
}
else {
//'misc' sub-category does not exists
}
Load you current category using and get its Id
$currentCat = Mage::registry('current_category');
$currentCatId = $currentCat->getId();
Load the 'misc' sub-category using
$_category = Mage::getModel('catalog/category')->loadByAttribute('name', 'misc');
$_categoryParentId = $_category->getParentCategory()->getId();
if( $currentCatId == $_categoryParentId){
//Do your stuffs
}
Though you have two categories with the same name misc, so you would want to laod category by url-key or parse through the multiple category array to test.
Try flollowing,
<?php
$categoryId = 5;
$categories = Mage::getModel('catalog/category')->load($categoryId)->getChildren();
$catAsArray = explode(',', $categories);
//you can have $catAsArray to check if it has subcategories
foreach($catAsArray as $child)
{
$_child = Mage::getModel( 'catalog/category' )->load( $child );
echo $_child->getName() . '<br />';
echo $_child->getUrl() . '<br />';
echo $_child->getDescription() . '<br />';
}
?>
Or the other way,
<?php
$categoryId = 5;
$_category = Mage::getModel('catalog/category')->load($categoryId);
if($_category->hasChildren())
{
}
?>
am trying to display all the second level categories and my code is
<?php $storeId = Mage::app()->getStore()->getId();
//Get category model
$_category = Mage::getModel('catalog/category')->setStoreId($storeId);
$_categoryCollection = $_category->getCollection();
$_categoryCollectionIds = $_categoryCollection->getAllIds();
//Remove root category from array
unset($_categoryCollectionIds[0], $_categoryCollectionIds[1]);
?>
<div id="accordian_hover">
<?php
$o = null;
$o .= '<ul>';
foreach ($_categoryCollectionIds as $catId) {
$_category = $_category->load($catId);
if($_category->getLevel() == 2) {
$catChildren = $_category->getChildren();
if(!empty($catChildren)) {
$o .= '<li> '.$_category->getName().'';
$o .= '<ul>';
$categoryChildren = explode(",", $catChildren);
foreach ($categoryChildren as $categoryChildId) {
/* #var $_childCategory Mage_Catalog_Model_Category */
$_childCategory = $_category = Mage::getModel('catalog/category')->setStoreId($storeId)->load($categoryChildId);
$o .= '<li>'.$_childCategory->getName().'</li>';
// If you wish to display the total number of products for each category, uncomment this line
// $o .= '<span class="totalNumberOfProducts">'.$_childCategory->getProductCollection()->count().'</span>';
}
$o .= '</ul></li>';
}
}
} $o .='</ul>';
echo $o;
?>
</div>
But the top menu url is wrong all other are displaying correct but the main category url is wrong(2nd category) Please help me , and i have to display the third level menu also when the user hover on category...
Live Link http://toolskaart.com/
Hope the below code will solve your problem.
Mage::getModel('catalog/category')->load($_category->getId())->getUrl()
It works for me.
I have categories with 4 levels.
Example :
level0category --> level1category --> level2category -->lastlevel_level3cateogory
is it possible on lastlevel to display the categories that are in level2 and only? (on a block that i load a custom left.phtml)
I was thinking something with automatic category detection and not have to set cat_id=
Edit: this is what i was looking for, after mix of code from different sources.
<?php
$currentCat = Mage::registry('current_category');
if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() )
{
// current category is a toplevel category
$loadCategory = $currentCat;
}
else
{
// current category is a sub-(or subsub-, etc...)category of a toplevel category
// load the parent category of the current category
$loadCategory = Mage::getModel('catalog/category')->load($currentCat->getParentId());
// #TODO enhance for more nested category levels to display sub-categories
}
$subCategories = explode(',', $loadCategory->getChildren());
foreach ( $subCategories as $subCategoryId )
{
$cat = Mage::getModel('catalog/category')->load($subCategoryId);
if ($cat->getIsActive())
{
if ($currentCat->getEntityId() == $subCategoryId)
{
echo '<li style="display:none;">'.$cat->getName().'</li>';
}
else
{
echo ''.$cat->getName().' <br>'; }
}
}
?>
$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*')
->addIsActiveFilter()
->addAttributeToFilter('level',2)
->addOrderField('name');
make sure this line is there
apply ->addAttributeToFilter('level',2)
thanks .............. but ...I have below code which is working up to 1st level not for more level in category tree could someone help me for the 3rd level and more........... level of the category tree...........that means if i I will click the parent category only that particular parent open with his children all other will on closed manner
like
Category1
-subcategory1
----subsubcategory1
-subcategory2
Category2
-subcategory1
-subcategory2
<?php
$obj = new Mage_Catalog_Block_Navigation();
$store_cats = $obj->getStoreCategories();
$current_cat = $obj->getCurrentCategory();
$current_cat = (is_object($current_cat) ? $current_cat->getName() : '');
foreach ($store_cats as $cat) {
if ($cat->getName() == $current_cat) {
echo '<li class="current">'.$cat->getName()."\n<ul>\n";
foreach ($obj->getCurrentChildCategories() as $subcat) {
echo '<li>'.$subcat->getName()."</li>\n";
}
echo "</ul>\n</li>\n";
} else {
echo '<li>'.$cat->getName()."</li>\n";
}
}
?>
The easiest way to solve this is to create a recursive function (a function that calls itself).
Here is how you might want to set up your code:
//go through all the parent catgeroies
foreach ($store_cats as $cat) {
// if it's the category we are looking for let's spit it out as an <li>
if ($cat->getName() == $current_cat) {
echo '<li class="current">'.$cat->getName()."\n<ul>\n";
// let's get all the subcategories no matter how deep (look at function below).
getChildCategories();
}
}
//our new sub-category getter
public function getChildCategories() {
// lets loop through all the children of the current category and spit out <li> for them
foreach ($obj->getCurrentChildCategories() as $subcat) {
echo '<li>'.$subcat->getName()."</li>\n";
//lets call ourself again to see whether there are deeper layer to be found
getChildCategories();
}
}
What you would want to add to your code is an if statement checking whether there are children:
if ($obj->getCurrentChildCategories()) {//then loop through etc.}
This way you avoid hitting errors when you hit rock bottom.