Skip to content

Instantly share code, notes, and snippets.

@proxium
Last active October 27, 2020 17:49
Show Gist options
  • Save proxium/a8076222bd771d247d52 to your computer and use it in GitHub Desktop.
Save proxium/a8076222bd771d247d52 to your computer and use it in GitHub Desktop.
Product Count Per Category in Magento
<?php
/**
* @param $category
* @return int
*/
public function getProductCount($category)
{
$productTable = Mage::getSingleton('core/resource')->getTableName('catalog/category_product');
$select = $this->getReadConnection()->select()
->from(
array('main_table' => $productTable),
array(new Zend_Db_Expr('COUNT(main_table.product_id)'))
)
->where('main_table.category_id = :category_id');
$bind = array('category_id' => (int)$category->getId());
$counts = $this->getReadConnection()->fetchOne($select, $bind);
return intval($counts);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment