【问题标题】:magento showing wrong product count in categorymagento 在类别中显示错误的产品数量
【发布时间】:2013-02-08 16:53:46
【问题描述】:

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

正如我强调的那样,LEFT 表示该类别有 16 种产品,但实际上“类别产品”选项卡显示 15 种产品。我所有的类别都搞砸了。请让我知道出了什么问题。我试过禁用缓存,但没有奏效。

[编辑]

我尝试从该类别中删除一个产品,然后左侧的数字变为 15,总记录为 14。所以我认为可能是该类别中被禁用的产品。但是当我搜索禁用产品时,没有。

【问题讨论】:

    标签: magento magento-1.7


    【解决方案1】:

    这将解决所有问题。

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

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

    【讨论】:

      【解决方案2】:

      嗨产品计数来自位于位置code/core/Mage/Catalog/Model/Resource/Category/Collection.php的方法名称loadProductCount

      如果您要深入挖掘,这个计数来自两个表之间的连接查询: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;           
      }   
      

      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>
      

      【讨论】:

      • 你说你有同样的问题吗?
      • 是的,我在 Magento 1.7 CE 中遇到了同样的问题。不知何故,在删除产品后,该条目并未从 catalog_category_product 表中删除。
      • 你在观察哪个事件?
      • @roshanlal 您是否为此制作了自定义模块?
      【解决方案3】:

      一个简单的解决方案是去 app/code/core/Mage/Catalog/Model/Category.php

      或者最好创建一个本地文件,这样它在 magento 升级时不会影响。所以创建 app/code/local/Mage/Catalog/Model/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 文件。一般情况下是:theme/template/catalog/navigation/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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多