Magento - Get total number of items of a category in view.phtml - magento

How can I get the total number of Items, I want to show it in the category view.phtml file. Usually this value is in the Toolbar.phtml.
I have tried something like this, but I think I am pretty far away:
$this->helper('catalog/output')->$_productCollection->count()
Magento version 1.7.0.2
The expected result should be something like this:
Items in this category: 17
The 17 should be the total number. If possible should include subcategory items.

Assuming that you want to display it in view.phtml you already have the current category object there so you can use $_category->getId()
$products_count = Mage::getModel('catalog/category')->load($_category->getId())
->getProductCount();
echo($products_count);
If you want to use it in list.phtml you can use
echo($_productCollection->count());

Try this,
<?php $_helper = $this->helper('catalog/output');?>
<?php $_category_detail = Mage::registry('current_category');?>
<?php $_category_detail->getName();?>
<?php $_category_detail->getId(); ?>
<?php $products_count = Mage::getModel('catalog/category')->load($_category_detail->getId())
->getProductCount();
echo($products_count);
?>

Basically, you can't show the total amount of filtered items in your view.phtml.
the reason is, that the logic, which gets the total amount, is not present in the $this context of the view.phtml.
But the logic is available in the Mage_Catalog_Block_Product_List_Toolbar block which is a child block of Mage_Catalog_Block_Product_List, though.
that basically means that you can actually get the total amount of filtered items by instantiating a toolbar and list block.
After doing that, the collection of the toolbar block has to be set with the value of the collection of the list block.
the following code is used in the view.phtml file to get the filtered total amount of items from the toolbar block:
$toolbar = new Mage_Catalog_Block_Product_List_Toolbar();
$list = new Mage_Catalog_Block_Product_List();
$toolbar-> setCollection($list -> getLoadedProductCollection());
$products_count = $toolbar -> getTotalNum();

There can be two ways we can find product count of a category.
$collection = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addAttributeToSelect('level')
->addAttributeToSelect('entity_id');
foreach($collection as $cat)
$cat->getProductCount();
This will give you the product count for deepest category only. So for example, you have following categories. considering tree like structure.
Clothes(6)
->Cotton(3)
->Women(2)
The result returned from the piece of code given above.
Clothes(3)
Cotton(1)
Women(2)
There are three products directly associated with Clothes only, 1 with Cotton only and 2 with Women only. So it simply ignores the subcategories.
Another way is getting product count from products perspective.
$current_category = Mage::getModel('catalog/category')->load($cat->getEntityId());
$productCount = Mage::getModel('catalog/product')->getCollection()
->addFieldToFilter('manufacturer',$this->manufacturer["id"])
->addFieldToFilter('visibility',4)
->addFieldToFilter('status',1)
->addCategoryFilter($current_category)->getSize();
This gives us the added benefit of filtering product attributes. However in the above scenario, the count returned will be slightly different.
It will return Clothes (6), as it has 3 products associated to itself and 3 more products to its sub categories. Similarly
Cotton(3)
Women(2).
So for efficient results it would be nice to use mix of both.

It's not right to load additional model inside view because you already have model instance from which you can retrieve Product collection.
$this->getCurrentCategory()->getProductCollection()->count();

it is very simple and it worked well for me its just one line of code
<?php echo Mage::registry('current_category')->getProductCount();?>
It will display the count of the products of current category

Related

Can you Clone a collection row in Magento?

I'm trying to build a custom grid in Magento, where I have a row for every product ordered including qty. For example, if a customer ordered 3 of a specific product - I need to have 3 rows that all have matching details. Is this possible?
This is for a custom product where each row represents a manufacturing run. I'm not necessarily looking for code examples, but I'm not sure if I can do this with the sql style collection process? Or if I need modify the query and loop through once I've colelcted all the data from the database?
Thanks
Instead of showing the product order item grid, I just added an export to xml button. Then, in the grid just modified the "_exportIterateCollection" method as the following:
foreach ($collection as $item) {
for($i = 0; $i < $item->getQtyOrdered(); $i++ ){
call_user_func_array(array($this, $callback), array_merge(array($item), $args));
}
}
Hope this helps future questions!

How to show sort by most popular(most sold) products on product listing page in Magento?

I am using following tutorial to display the most sold product sort by option(Filtering) to display on product listing page in magento?
Tutorial
/app/code/local/Mage/Catalog/Model/Resource/Product/collection.php
<?php
public function sortByReview($dir){
$table = $this->getTable('review/review');
$entity_code_id = Mage::getModel('review/review')->getEntityIdByCode(Mage_Rating_Model_Rating::ENTITY_PRODUCT_CODE);
$cond = $this->getConnection()->quoteInto('t2.entity_pk_value = e.entity_id and ','').$this->getConnection()->quoteInto('t2.entity_id = ? ',$entity_code_id);
$this->getSelect()->joinLeft(array('t2'=>$table), $cond,array('review' => new Zend_Db_Expr('count(review_id)')))
->group('e.entity_id')->order("review $dir");
}
?>
But I want to sort products which are most sold from every category.
How can I do this?
Is there any free extension available for this?
I did the following thing as the client did not want automatically filter most sold product.
I created an attribute "popular" as drop down and give values from 1 to 5.Then marked "Used for Sorting in Product Listing" to Yes.
After this the attribute was visible under sorting options.

Count articles in category

I would like to display article count in a single category.
$categoryName = "CategoryName";
$n = count($this->category->$categoryName);
echo "This category has ".$n." articles in it!";
This is not working (example).
There is a parameter allowing you to do this already. Assign a Category List or Category Blog Menu item and then choose under the Category Options parameters tab. Change No Articles Message to show.
The reason that your code isn't working - is because a single category is just represented by $this->category.
However for either Category Blog or Category List layouts you can simply use:
$categoryName = "CategoryName";
if($this->category->title==$categoryName) {
count($this->category->getNumItems(true))
}
http://docs.joomla.org/Help25:Menus_Menu_Item_Article_Category_Blog#Category_Options
Try this.
The default category layout contain the count of the articles in it.
take a look at this components\com_content\views\categories\tmpl
Also you can found the count count($item->getChildren())
Hope this may you..

How to get a list of categories by product number?

I need to sort categoies by product number that means which category has large number of products is shown at the top of category list. So catgories list should be descending order by product number.
any help would be appreciated!
I have categories and its product as following as:
category no of products
cell phones 5
cameras 8
computers 3
I would like to sort categories cameras,cell phones,computers by product number .For this sorting result i would like to use join in collection class of category.
The following code will fetch the product per category:-
$productCollection = Mage::getResourceModel('catalog/product_collection')
->addCategoryFilter($category);
$productCount = $productCollection->count();
You can do in this way:-
Get an array of all categories
Loop through them
Fetch product collection with product count for each category (from the above code).
Sort categories with the product count.
Hope this helps. Thanks.
If you have a collection of categories called $collection then you can do this:
$collection->setOrder('children_count', 'desc');
Edit:
To make it the default in an overridden collection use this method:
protected function _initSelect()
{
parent::_initSelect();
$this->setOrder('children_count', 'desc');
return $this;
}

Magento - Loading root collection loads all products

Regarding using multiple stores, with different root categories:
I have 2 stores set up, with different roots. One has 14 products, the other 6.
If I use the following on my homepage (simply to show how many products are in the root category for that store - in this case with an ID of 8) I get 20 products - so all products in the store, from all roots:
$_testproductCollection = Mage::getModel('catalog/category')->load(8)
->getProductCollection()
->addAttributeToSelect('*')->load();
echo "NO. OF PRODUCTS IS ".$_testproductCollection->count();
However, if I change the ID to a sub-category, I get the correct amount of products. There are only 6 products in this root:
But the count shows 20 (as there's 20 in the whole store - or both roots).
Anyone know what's up? Is it a bug?
I also noticed that if you go to Manage Products and use the store view filter, it doesn't do anything, still showing 20 products in the store view whose root has only 6 products:
OK, I think this works, haven't tested too much but seems to have done the trick. You need to first get your stores root category id, then join some fields so you have access to the products "category_id", then filter using that:
$_rootcatID = Mage::app()->getStore()->getRootCategoryId();
$_testproductCollection = Mage::getResourceModel('catalog/product_collection')
->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left')
->addAttributeToFilter('category_id', array('in' => $_rootcatID))
->addAttributeToSelect('*');
$_testproductCollection->load();
foreach($_testproductCollection as $_testproduct){
echo $this->htmlEscape($_testproduct->getName())."<br/>";
};

Resources