Only active product count on Magento Category - php

I am trying to display the number of products in each category, using this code to I want to display subcategories of category id:3 . It is showing but it included disabled and invisible products.
<?php
$cats = Mage::getModel('catalog/category')->load(3)->getChildrenCategories();
?>
<ul>
<?php foreach($cats as $category): ?>
<li>
<?php echo $category->getName() ?>(<?php echo $category->getProductCount(); ?>)
</li>
<?php endforeach; ?>
</ul>
Is there any good solution so that I can get the exact count of categories that are Enabled and active.

you need check active filter acondition and for this you can use below code
$products = Mage::getModel('catalog/category')->load($category->getId())
->getProductCollection()
->addAttributeToSelect('entity_id')
->addAttributeToFilter('status', 1)
->addAttributeToFilter('visibility', 4);
echo $products->count();

Without any regard to Magento programming conventions, that piece of code should look like this:
<?php
$cats = Mage::getModel('catalog/category')->load(3)->getChildrenCategories();
$collection = Mage::getModel('catalog/product')->getCollection();
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
?>
<ul>
<?php foreach($cats as $category): ?>
<?php $count = $collection->addCategoryFilter($category)->getSize(); ?>
<li>
<?php echo $category->getName() ?>(<?php echo $count ?>)
</li>
<?php endforeach; ?>
</ul>
But do the world a favor and organize the code properly.

Loading collection and using multiple attribute filter is not a proper way and this won't filter associated product and it's stock levels.
This piece of code with do all together,
$categoryId = 32; // Replace with your category
$category = Mage::getModel('catalog/category')
->setStoreId(Mage::app()->getStore()->getId())
->load($categoryId);
Mage::register('current_category', $category);
$products = Mage::getSingleton('catalog/layer')->getProductCollection();
echo $products->getSize();

Related

Filtering non active categories in magento

So I'm using this code to display the categories that any product on my website is included in. The issue is that I'm using some categories as placeholders to show 'featured products' I wanted to try to filter these out.
This is Magento 1.9.1.0 on PHP 5.6.30
<ul class="listfix">
<?php $categories = $_product->getCategoryIds(); ?>
<?php foreach($categories as $k => $_category_id):
?>
<?php $_category= Mage::getModel('catalog/category')->load($_category_id)?>
<?php if($_category->getId()):?>
<li><a href="<?php echo $_category->getUrl() ?>"><?php echo $_category-
>getName() ?></a>
</li>
<?php endif;?>
<?php endforeach; ?>
</ul>
I tried adding this
$_category->addAttributeToFilter('is_active', 1)//get only active
categories
but it threw an error, I'm not a great php guy, I just find pieces of code and try to make them work. I got the original parts from HERE
as per below I've tried to add in the code that follows but I'm still seeing categories listed that are not active...
<ul class="listfix">
<?php $activeCategories =
Mage::getResourceModel('catalog/category_collection')
->addAttributeToFilter('is_active', 1)
->getColumnValues('entity_id');
?>
<?php $activeCategories = $_product->getCategoryIds(); ?>
<?php foreach($activeCategories as $k => $_category_id): ?>
<?php $_category= Mage::getModel('catalog/category')->load($_category_id)?>
<?php if($_category->getId()):?>
<li><a href="<?php echo $_category->getUrl() ?>"><?php echo $_category-
>getName() ?></a>
</li>
<?php endif;?>
This will give you a list of all the active Categor ID's in 1.9:
$activeCategories = Mage::getResourceModel('catalog/category_collection')
->addAttributeToFilter('is_active', 1)
->getColumnValues('entity_id');
Then you loop through that array, and load the category, or whatever, by ID:
foreach($activeCategories as $id) {
$cat = Mage::getModel('catalog/category')->load($id);
}
<ul class="listmem">
<?php $activeCategories =
Mage::getResourceModel('catalog/category_collection')
->addAttributeToFilter('is_active', 1)
->getColumnValues('entity_id');?>
<ul class="listfix">
<?php $activeCategories = $_product->getCategoryIds(); ?>
<?php foreach($activeCategories as $id) {
$cat = Mage::getModel('catalog/category')->load($id);
} ?>
<?php if($cat->getId()):?>
<li><a href="<?php echo $cat->getUrl() ?>"><?php echo $cat->getName() ?>
</a>
</li>
<?php endif;?>
</ul>
I thought I sorted out what I was doing wrong, but I'm still seeing the categories that aren't active in my list.

Magento 1.9: How to get Subcategories of specific Category?

I am using the following block in my Magento CMS for home site:
{{block type="catalog/product_list" name="catalog_list" category_id="1420" template="catalog/product/listStart.phtml"}}
How can I just get an output of all subcategories of the category_id as specified in the block (id 1420 in this case).
So far I have the following code:
<?php
$_category = $this->getCurrentCategory();
$collection = Mage::getModel('catalog/category')->getCategories($_category->entity_id);
$helper = Mage::helper('catalog/category');
?>
<div class="category-products">
<div id="carousel">
<ul class="products-in-row">
<?php $i=0; foreach ($collection as $cat): ?>
<li class="item">
<?php echo $cat->getName();?>
</li>
<?php endforeach ?>
</ul>
</div>
I'm getting all subcategories of the main category only.
This code may help if you want to get child category of every current category
<?php
$layer = Mage::getSingleton('catalog/layer');
$_category = $layer->getCurrentCategory();
$currentCategoryId= $_category->getId();
$children = Mage::getModel('catalog/category')->getCategories($currentCategoryId);
foreach ($children as $category)
{
echo $category->getName(); // will return category name
echo $category->getRequestPath(); // will return category URL
}
?>
Another way:
<?php
$categoryId = 10 ; // get current category id
$category = Mage::getModel('catalog/category')->load($categoryId);
$catList = explode(",", $category->getChildren());
foreach($catList as $cat)
{
$subcategory = Mage::getSingleton('catalog/category')->load($cat);
echo $subcategory->getName();
}
?>

Get Subcategory in Magento

I need help. I use this code to get the category link under products info on a few pages in Magento:
<?php $categories = $_product->getCategoryIds(); ?>
<span>In </span>
<?php $i=1; foreach($categories as $k => $_category_id): ?>
<?php if($i>1) {break;} ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category_id) ?>
<a class="in-category" href="<?php echo $_category->getUrl() ?>"><?php echo $_category->getName() ?></a>
<?php $i++; endforeach; ?>
You can see it here: http://192.241.178.130/new_arrivals
Problem I'm having is that I want the script to display the category closest to the product but it's instead displaying the root category (Default category of the site)
M
Thanks.
Try something like this:
<?php
$categoryIds = $_product->getCategoryIds();
$categories = Mage::getModel('catalog/category')
->addAttributeToSelect('url_key')//add url key to select
->addAttributeToSelect('name')//add name to select
->getCollection() //get categories as collection
->addAttributeToFilter('entity_id', $categoryIds)//filter only by selected ids
->addAttributeToFilter('is_active', 1)//get only active categories
->addAttributeToFilter('level', array('gte'=>2))//ignore root category and 'root of roots'
->setOrder('level', 'desc');//sort by level descending
$mainCategory = $categories->getFirstItem();//get only the category lowest in the tree
if ($mainCategory->getId()) : ?>
<a class="in-category" href="<?php echo $mainCategory->getUrl() ?>"><?php echo $mainCategory->getName() ?></a>
<?php endif;?>
Instead of using a foreach to walk one time through the array you can use array_pop.
Anyhow the function getCategoryIds() will return an array of all categories the product is in. This also include parent categories and are in logical order. The category with the lowest id will show up first.
Perhaps something like this will work for you:
<?php $_category_id = array_pop($_product->getCategoryIds()); ?>
<span>In </span>
<?php $_category = Mage::getModel('catalog/category')->load($_category_id) ?>
<a class="in-category" href="<?php echo $_category->getUrl() ?>">
<?php echo $_category->getName() ?>
</a>

Magento display subcategories name on product page

I am looking at displaying the names of the sub-categories that have been selected within the admin area of Magento on the single product page.
I have the template open, but I just need to call the relevant code, any ideas?
Try this
<?php
$onCatalog = false;
if(Mage::registry('current_product')) {
$onCatalog = true;
}
Try this (assuming you are inside the product view template, view.phtml):
<?php foreach($_product->getCategoryCollection() as $_cat): ?>
<?php echo $_cat->getName() ?><br />
<?php endofreach ?>
That should get you started and get a list of the categories the product has been assigned to.
If you would rather thave the IDs:
<?php $categoryIds = $_product->getCategoryIds() // an array ?>
You could use this.
<h2>This product is in the following categories</h2>
<ul>
<?php
$categories = $_product->getCategoryCollection();
$categories->addAttributeToSelect(array('name', 'url'));
foreach ($categories as $category){
if ($category->getName() == 'Default Category' || $category->getName() == 'Categories') {
continue;
}
?>
<li><?php echo $category->getName() ?></li>
<?php } ?>
</ul>
OK, if your're in catalog>product>view.phtml or catalog>product>list.phtml
<?php foreach($_product->getCategoryCollection() as $_cat): ?>
<?php echo $_cat->getName() ?><br />
<?php endforeach ?>
Otherwise, first line should get your the product:
$_product = Mage::registry('current_product');
Would give your currently selected product.
While:
$_product = Mage::helper('catalog/product')->load(35)
Would get you product 35.

Magento -> How can I list products with same multiple select attribute?

I'm trying to retrieve products that carry same attribute. Specifically multiple select type. It seems the basic methods don't work. Selecting only the "name" attribute, I get all my products listed. When I try to filter "shop_by_color", it filters down, but not entirely. Not sure why it removes some and leaves others, even though they are the wrong ones. Any tips appreciated.
<?php
$model = Mage::getModel('catalog/product');
$collection = $model->getCollection();
$collection->addAttributeToSelect('name');
$collection->addAttributeToFilter('shop_by_color'); // multiple select attribute
$collection->addFieldToFilter(array(array('attribute'=>'shop_by_color','finset'=>array('Yellow, White'),
)));
$collection->load();
?>
<ul>
<?php foreach($collection as $product) : ?>
<li><?php echo $product->getName() ?></li>
<?php endforeach; ?>
</ul>
Hi I am not sure about your syntax I have never seen this sort of thing before.
<ul>
<?php foreach($collection as $product) : ?>
<li><a href="<?php echo $product->getProductUrl() ?>"><?php echo $product->getName()
?></a></li>
<?php endforeach; ?>
</ul>
Shouldn't it be...
<ul>
<?php foreach($collection as $product) { ?>
<li><a href="<?php echo $product->getProductUrl() ?>"><?php echo $product->getName()
?></a></li>
<?php } ?>
</ul>
DC
<?php
$_productCollection=$this->getLoadedProductCollection();
$_productCollection->clear()
->addAttributeToSelect('*')
->addAttributeToFilter('type_id', array('eq' => 'simple'))
->addAttributeToSort('name', 'ASC')
->addAttributeToSort('created_at', 'ASC')
->load();?>
on list page please use this type collection filter it's working fine.

Categories