Magento: display all level2 categories - magento

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)

Related

Magento search function not displaying results correctly

I have a issue where the search results are being counted but not showing up in the content section.
I've tried re-indexing and caching. Also checked to make sure products are set as 'catalog, search'.
Not sure if its a bug in theme (Goetze, using template 4).
This will return no result which is good.
Whereas this shows that there are 23 products in the count but will not display them in grid/list.
Any help would greatly be appreciated.
<?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());
}
$subCategories = explode(',', $loadCategory->getChildren());
foreach ( $subCategories as $subCategoryId )
{
$cat = Mage::getModel('catalog/category')->load($subCategoryId);
if($cat->getIsActive())
{
echo '<a class="parent-categories" href="'.$cat->getURL().'">'.$cat->getName().'</a>';
}
}
?>
Fixed this by commenting out all that code except:
$currentCat = Mage::registry('current_category');
And search seems to be working properly!

sorting and pager not working on custom collection Magento

Hi i have added a new product relation in magento named as accesories.
Like i have a main product A and i have attach B c and d with it as
accesories
Now on the frontend i need to show this collection as the same design
on list .phtml
so i have decide to render the custom collection on the list .phtml
for this i have render a custom collection in _getProductCollection()
by below code
if($_GET['product_id'])
{
$productid=$_GET['product_id'];
$product = Mage::getModel('catalog/product_link')
->getCollection()
->addFieldToFilter('link_type_id', 6)
->addFieldToFilter('product_id',$productid)
->load();
$LinkedProduct=$product->getData();
foreach($LinkedProduct as $LinkedProducts)
{
$LinkedProductId[]= $LinkedProducts['linked_product_id'];
}
$productIds = array_values($LinkedProductId);
$_productCollection=Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id', array('in' => $productIds))
->load();
$this->_productCollection=$_productCollection;
}else{
if (is_null($this->_productCollection)) {
$layer = $this->getLayer();
/* #var $layer Mage_Catalog_Model_Layer */
if ($this->getShowRootCategory()) {
$this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
}
// if this is a product view page
if (Mage::registry('product')) {
// get collection of categories this product is associated with
$categories = Mage::registry('product')->getCategoryCollection()
->setPage(1, 1)
->load();
// if the product is associated with any category
if ($categories->count()) {
// show products from this category
$this->setCategoryId(current($categories->getIterator()));
}
}
$origCategory = null;
if ($this->getCategoryId()) {
$category = Mage::getModel('catalog/category')->load($this->getCategoryId());
if ($category->getId()) {
$origCategory = $layer->getCurrentCategory();
$layer->setCurrentCategory($category);
$this->addModelTags($category);
}
}
$this->_productCollection = $layer->getProductCollection();
$this->prepareSortableFieldsByCategory($layer->getCurrentCategory());
if ($origCategory) {
$layer->setCurrentCategory($origCategory);
}
}
}
return $this->_productCollection;
}
The collection is working fine
but the problem is the sorting and toolbar is not working
. Can you please suggest me how can i make a sorting and pager works with my custom colection
OK i have solve this by adding a sort function in collection like below code
$productid=$_GET['product_id'];
$product = Mage::getModel('catalog/product_link')
->getCollection()
->addFieldToFilter('link_type_id', 6)
->addFieldToFilter('product_id',$productid)
->load();
$LinkedProduct=$product->getData();
foreach($LinkedProduct as $LinkedProducts)
{
$LinkedProductId[]= $LinkedProducts['linked_product_id'];
}
$productIds = array_values($LinkedProductId);
$_productCollection=Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id', array('in' => $productIds))
->addAttributeToSort($_GET['order'],$_GET['dir'] )
->setPageSize($_GET['limit'])
->load();
echo $_productCollection->getSelect() ;
$this->_productCollection=$_productCollection;
the sorting was not working because there wqas no sort function added in the colection
->addAttributeToSort($_GET['order'],$_GET['dir'] )
->setPageSize($_GET['limit'])
now it is working fine.
thanks

Magento - Check if subcategory exists in current category

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())
{
}
?>

Magento navigation-block issues

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>

Change sort order of Magento subcategories

I am working on a site where I am displaying a list of all the subcategories associated with the current category. The code below works fine for that, but I'd like to change the way the list of subcategories is sorted. Currently, it sorts by category ID. I'd like it to show up in whatever order the Magento user put the categories in in the admin (where they can drag-and-drop to change the category order). Appreciate any help!
<?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());
}
$subCategories = explode(',', $loadCategory->getChildren());
foreach ( $subCategories as $subCategoryId )
{
$cat = Mage::getModel('catalog/category')->load($subCategoryId);
if($cat->getIsActive())
{
echo ''.$cat->getName().'';
}
}
?>
Try calling getChildrenCategories this will take into account the position of each category:
$loadCategory->getChildrenCategories()
EDITED
Unlike getChildren which returns a string of all category ids returns an array of Mage_Catalog_Model_Category so your code will need to be changed to take this into account.
From your code snippet above the following change should work. Note the call to getChildrenCategories() and the change in the foreach loop as each item should be a category object.
<?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());
}
$subCategories = $loadCategory->getChildrenCategories();
foreach ( $subCategories as $subCategory )
{
if($subCategory->getIsActive())
{
echo ''.$subCategory->getName().'';
}
}
?>

Resources