2013-02-08 76 views
10

我有一個奇怪的問題,似乎很多人在互聯網上都一樣。下面的圖片將定義我的問題也是我的Magento的版本是1.7magento在類別中顯示錯誤的產品計數

enter image description here

正如我已經強調,左說,分類中有16個產品,但在實際的分類產品標籤顯示15種產品。我所有的分類都搞砸了。請讓我知道發生了什麼問題。我試過禁用緩存,但它沒有奏效。

[編輯]

我試圖從類別刪除一個產品,然後在左邊的數字去15個,總記錄14.因此,我認爲可能是一個產品,其在那裏被禁用,這個類別中。但是當我搜索殘疾產品時,沒有人在那裏。

回答

4

嗨產品的計數來自方法名稱loadProductCount其位於位置code/core/Mage/Catalog/Model/Resource/Category/Collection.php

如果您將深入挖掘該計數是根據在聯接查詢未來兩個表之間:catalog_category_productcatalog_category_entity

我有固定這個問題通過使用事件觀察者。你可以暫時做同樣的事情。如果您找到更好的解決方案,請告訴我。 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

做你說你有同樣的問題? – 2013-04-04 04:17:34

+0

是的,我在Magento 1.7 CE中面臨同樣的問題。不知怎的,刪除產品後,條目沒有從catalog_category_product表中刪除。 – 2013-04-04 11:42:28

+0

你在觀察哪個事件? – CarComp 2013-05-22 01:57:53

12

這曾經

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

事件將解決他們所有。

DELETE FROM 
catalog_category_product 
where product_id NOT IN (SELECT entity_id FROM (catalog_product_entity)) 

然後,在觀察者處實施解決方案以防止其再次發生。

2

一個簡單的解決方案是去應用程序/代碼/核心/法師/目錄/型號/ Category.php

,或者是更好地創建本地文件,使得它不會影響而Magento的升級。因此,創建 應用程序/代碼/本地/法師/目錄/型號/ Category.php

在這個模型中創建一個新的功能說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(); 

}

現在轉到您的模板phtml文件,您可以在其中執行類別產品計數。在一般情況下,它是:主題/模板/目錄/導航/ left.phtml

現在請根據需要,上面的函數:

<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> 
相關問題