且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2021-12-17 08:00:00

Hi product count 来自方法名 loadProductCount 位于 code/core/Mage/Catalog/Model/Resource/Category/Collection.php

Hi product count comes from method name loadProductCount which is located at location code/core/Mage/Catalog/Model/Resource/Category/Collection.php

如果你深入挖掘,这个计数来自两个表之间的连接查询:catalog_category_productcatalog_category_entity

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;           
}   

config.xml 中使用的事件

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>