Reputation: 13734
I have a strange issue and seems many are having the same on internet. Below picture will define my issue and also my magento version is 1.7
As I have highlighted, LEFT says the category has 16 products, but in actual the Category Products Tab shows 15 products. All my categories are messed up. Please let me know what's going wrong. I've tried disabling the cache, but it didn't worked.
[Edit]
I tried removing one-product from the category, then the number on the left went to 15 and total records 14. So I thought may be a product whose is disabled in there in this category. But when I searched for disabled products none were there.
Upvotes: 10
Views: 7013
Reputation: 696
A simple solution to this is go to app/code/core/Mage/Catalog/Model/Category.php
or it's better to create a local file so that it doesn't effects while magento upgrade. So create app/code/local/Mage/Catalog/Model/Category.php
In this model create a new function say getFrontentProductCount()
public function getFrontentProductCount()
{
$collection = Mage::getResourceModel('catalog/product_collection')
->addCategoryFilter($this);
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
return $collection->count();
}
Now go to your template phtml file where you execute your category product count. In general case it's: theme/template/catalog/navigation/left.phtml
now call the above function as required, like:
<ol>
<?php foreach ($_categories as $_category): ?>
<?php if($_category->getIsActive()): ?>
<li>
<a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="current"<?php endif; ?>><?php echo $this->htmlEscape($_category->getName()) ?></a> (<?php echo $_category->getFrontentProductCount() ?>)
</li>
<?php endif; ?>
<?php endforeach ?>
</ol>
Upvotes: 2
Reputation: 325
Hi product count comes from method name loadProductCount which is located at location code/core/Mage/Catalog/Model/Resource/Category/Collection.php
If you will dig in deep this count is coming from a join query between two tables: catalog_category_product
and catalog_category_entity
I have fixed this issue by using event observer. you can do the same for time being. And let me know if you find any better solution.
public function deleteCountCategory (Varien_Event_Observer $observer) {
try {
$product = $observer->getEvent()->getProduct();
$productId = $product->getId();
$resource = Mage::getSingleton('core/resource');
$writeConnection = $resource->getConnection('core_write');
$tableName = $resource->getTableName('catalog_category_product');
$query = "DELETE FROM {$tableName} WHERE product_id = ".(int)$productId;
$writeConnection->query($query);
} catch (Exception $e) {
throw $e;
}
return $this;
}
Event used in config.xml
<events>
<catalog_product_delete_after> <!-- identifier of the event we want to catch -->
<observers>
<catalog_product_delete_after_handler> <!-- identifier of the event handler -->
<type>model</type> <!-- class method call type; valid are model, object and singleton -->
<class>countfix/observer</class> <!-- observers class alias -->
<method>deleteCountCategory</method> <!-- observer's method to be called -->
<args></args> <!-- additional arguments passed to observer -->
</catalog_product_delete_after_handler>
</observers>
</catalog_product_delete_after>
</events>
Upvotes: 4
Reputation: 2016
This will fix them all.
DELETE FROM
catalog_category_product
where product_id NOT IN (SELECT entity_id FROM (catalog_product_entity))
Then, implement the solution in the observer to keep it from happening again.
Upvotes: 12