Magento - Check if subcategory exists in current category - magento

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

Related

How to get current categorys root parent category and all its sub category

I need logic to show the category tree like below:
Cat1 -> cat11 -> cat111
Cat2 -> cat21 -> cat211
Cat3 -> cat31 -> cat311
So if I am in cat211, in the left column all the categories of its root parent category (Cat2) should be displaying. Eg. in magento
Cat2 -> Cat21 -> Cat211
The attribute path contains all parent ids in the form 1/root_id/parent_id/.../category_id. You can use $category->getPathIds() to retrieve it as an array (or $category->getParentIds() if you want to exclude the id of the category itself). Because the first id in the path is always "1", you should remove the first array item to start from the store root category:
$categoryIds = $category->getPathIds();
array_shift($categoryIds);
Then you can load these at once, using a collection:
$categoryCollection = $category->getCollection()->addIdFilter($categoryIds);
foreach ($categoryCollection as $categoryInPath) {
// do what you want with $categoryInPath
}
I guess you mean the category tree, And this should give you category tree. you can see the various filters being applied. (You can always do according to your need.)
<?php
$rootCatId = Mage::app()->getStore()->getRootCategoryId();
$catlistHtml = getTreeCategories($rootCatId, false);
echo $catlistHtml;
function getTreeCategories($parentId, $isChild){
$allCats = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('is_active','1')
->addAttributeToFilter('include_in_menu','1')
->addAttributeToFilter('parent_id',array('eq' => $parentId))
->addAttributeToSort('position', 'asc');
$class = ($isChild) ? "sub-cat-list" : "cat-list";
$html .= '<ul class="'.$class.'">';
foreach($allCats as $category)
{
$html .= '<li><span>'.$category->getName()."</span>";
$subcats = $category->getChildren();
if($subcats != ''){
$html .= getTreeCategories($category->getId(), true);
}
$html .= '</li>';
}
$html .= '</ul>';
return $html;
}
?>
EDIT: getting all parent category using an id/current category id..
$id = Mage::registry('current_category'); //or if you are retrieving other way around
$category = Mage::getModel('catalog/category')->load($id);
$categorynames = array();
foreach ($category->getParentCategories() as $parent) {
$categorynames[] = $parent->getName();
}
print_r($categorynames); //dumping category names for eg.

unable to get product price in custom file magento

Hi i am new to magento and i need to get the products from a particular category
for this i have use
<?php
$id1=4;
$category1 = Mage::getModel('catalog/category')->load($id1);
$collection1 = $category1->getProductCollection();
$collection1->addAttributeToSelect('name');
$collection1->addAttributeToSelect('description');
$collection1->addAttributeToSelect('image');
$collection1->addAttributeToSelect('producturl');
$collection1->addAttributeToSelect('prlce');
$products1 = $collection1->getItems();
$_helper1 = $this->helper('catalog/output'); ?>
<?php foreach ($products1 as $product1){ ?>
<?php echo $this->htmlEscape($product1->getPrice()) ?>
<?php } ?>
in this it is showing the name , image and url but when i am trying to echo price it doenot show anyhing.Please suggest me where i am doing mistake
This is how you do it:
<?php
// Test Params
$cat_id = 4;
$store_id = 1;
// Load Category
$category = Mage::getModel('catalog/category')->load($cat_id);
// Load Category Products
$categoryProducts = $category->getProductCollection();
// Iterate Through Product Collection
foreach ($categoryProducts as $categoryProduct)
{
// Load Product At Specified Store Id
$product = Mage::getModel('catalog/product')
->setStoreId($store_id)
->load($categoryProduct->getId());
// Debug
print_r($product->getData());
// Get Price (e.g.)
echo 'Product Price: '. $product->getPrice()
}
?>

How to get url of a category in magento

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.

Magento: display all level2 categories

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)

Magento - Product Collection with the current user's Wishlist

Within a Magento php Controller, how can I get a Product Collection containing the products listed in the logged in user's (ie current user's) Wishlist.
I am getting the Wishlist using:
$wishList = Mage::getModel('wishlist/wishlist')->loadByCustomer(Mage::getSingleton('customer/session')->getCustomer());
and this contains the correct number of items.
But I would like to get a Product Collection. I have tried:
$productCollection = $wishList->getProductCollection();
and
$productCollection = $wishList->getProductCollection()->addAttributeToSelect('id')->load();
but the Product Collection I get has a length of 0.
How do I get the Product Collection?
You can use the getWishlistItemCollection (see link for more details) off the wishlist helper to return a collection of items, you then need to get the product from the item.
I have been using the following code to create an associative array of the products, which I then use to determine if a product I am displaying in the list page is in the wishlist...hopefully this will help:
public function getWishList() {
$_itemCollection = Mage::helper('wishlist')->getWishlistItemCollection();
$_itemsInWishList = array();
foreach ($_itemCollection as $_item) {
$_product = $_item->getProduct();
$_itemsInWishList[$_product->getId()] = $_item;
}
return $_itemsInWishList;
}
Try this with product all details like name, images etc...
<?php
$customer = Mage::getSingleton('customer/session')->getCustomer();
if($customer->getId())
{
$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);
$wishListItemCollection = $wishlist->getItemCollection();
foreach ($wishListItemCollection as $item)
{
echo $item->getName()."</br>";
echo $item->getId()."</br>";
echo $item->getPrice()."</br>";
echo $item->getQty()."</br>";
$item = Mage::getModel('catalog/product')->setStoreId($item->getStoreId())->load($item->getProductId());
if ($item->getId()) :
?>
<img src="<?php echo Mage::helper('catalog/image')->init($item, 'small_image')->resize(113, 113); ?>" width="113" height="113" />
<?php endif; } } ?>
$customer = Mage::getSingleton('customer/session')->getCustomer();
$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);
$wishListItemCollection = $wishlist->getItemCollection();
foreach ($wishListItemCollection as $item)
{
// do things
}

Resources