magento showing wrong product count in category

独自空忆成欢 提交于 2019-11-30 12:47:26

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>

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.

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!