magento在类别中显示错误的产品数量

cod*_*iot 10 magento magento-1.7

我有一个奇怪的问题,似乎很多人在互联网上也有同样的问题.下面的图片将定义我的问题,我的magento版本是1.7

在此输入图像描述

正如我所强调的那样,LEFT表示该类别有16种产品,但实际上,"产品类别"选项卡显示了15种产品.我的所有类别都搞砸了.请告诉我出了什么问题.我已经尝试禁用缓存,但它没有用.

[编辑]

我尝试从类别中删除单个产品,然后左边的数字变为15和总记录14.所以我认为可能是在此类别中禁用的产品.但当我搜索残疾产品时,没有人在那里.

Car*_*omp 12

这将解决所有问题.

DELETE FROM 
catalog_category_product 
where product_id NOT IN (SELECT entity_id FROM (catalog_product_entity)) 
Run Code Online (Sandbox Code Playgroud)

然后,在观察者中实施解决方案,以防止它再次发生.


ros*_*lal 4

您好,产品计数来自位于 location 的方法名称 loadProductCountcode/core/Mage/Catalog/Model/Resource/Category/Collection.php

如果您深入挖掘,此计数来自两个表之间的联接查询:catalog_category_productcatalog_category_entity

我已经通过使用事件观察器解决了这个问题。您暂时也可以这样做。如果您找到更好的解决方案,请告诉我。

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;           
}   
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)