magento showing wrong product count in category

前端 未结 3 799
甜味超标
甜味超标 2021-01-02 04:32

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

相关标签:
3条回答
  • 2021-01-02 05:13

    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>
    
    0 讨论(0)
  • 2021-01-02 05:31

    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>
    
    0 讨论(0)
  • 2021-01-02 05:36

    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.

    0 讨论(0)
提交回复
热议问题