【问题标题】:How to filter category collection using storefilter?如何使用 storefilter 过滤类别集合?
【发布时间】:2012-07-02 23:26:05
【问题描述】:

我有一个管理模块,我想在该模块中按存储方式获取类别集合,那么我们如何使用 storefilter 过滤集合? addstorefilter 不适用于类别收集。

【问题讨论】:

标签: php magento collections filter categories


【解决方案1】:

使用 setStore 或 setStoreId 方法:

$collection = Mage:getResourceModel('catalog/category_collection');

// or $collection = Mage::getModel('catalog/category')->getCollection();

$collection->setStoreId($myStoreId)
   ->load();

更新: 有一个简单的脚本可以检查:

<?php
require 'app/Mage.php';

Mage::app('admin');

$collection = Mage::getModel('catalog/category')->getCollection()
    ->addAttributeToSelect('name')
    ->setStoreId(2)
    ->load();

echo $collection->count(), "\n";
foreach ($collection as $item) {
   echo $item->getName(), "\n";
}

如果您有 3 个商店视图,则将有 3 个 storeId。有 0 - 用于管理存储,1 - 用于默认存储和 2(或其他值)用于另一个存储。我刚刚检查了这个,它可以工作。

【讨论】:

  • @mufaddal 哪个版本的 Magento?
【解决方案2】:

我添加了下面的代码,它对我有用

$storecategoryid = Mage::app()->getStore($storeid)->getRootCategoryId(); 

从这段代码中我得到了商店根目录。

$category = Mage::getModel('catalog/category')->getCollection()->addFieldToFilter('is_active',array('eq' => 1))->load();

从这里我得到整个类别集合

foreach($category as $cat)
{
  if($cat->getData('level')==2 && $cat->getData('parent_id')==$storecategoryid)
  { 
     echo 'my code';
  }
}

这样我得到了商店类别。

【讨论】:

  • 使用-&gt;addIsActiveFilter()addPathsFilter('/' . $storecategoryid . '/')
  • 是的,但您的问题并不清楚。您想按根类别而不是商店过滤
  • 是的,我希望它按商店过滤,我还没有找到任何按商店过滤的解决方案,所以我放了这种逻辑,因为我的根类别对于不同的商店是不同的,这个逻辑可以解决我的问题问题
【解决方案3】:
$rootCategoryId = Mage::app()->getStore()->getRootCategoryId();
$categories = Mage::getModel('catalog/category')->getCollection();
$categories->addAttributeToFilter('path', array('like' => "1/{$rootCategoryId}/%"));

【讨论】:

    【解决方案4】:

    道具前往@xpoback 以获得正确答案。我想重申一下答案,因为有些答案有些正确,有些则不正确。

    简短的回答,使用这个:

    $rootId = Mage::app()->getStore()->getRootCategoryId();
        $collection = Mage::getModel('catalog/category')
            ->getResourceCollection()
            ->addAttributeToSelect('*')
            ->addFieldToFilter('path', array('like' => "1/{$rootId}/%"))
            ->addIsActiveFilter();
    

    对于所有想了解其他答案的问题的人,请继续阅读。

    首先,@Muffadal 的答案是有效的,但在性能方面非常慢。您希望保持对 mySQL 的数据查询,而不是使用 php 在大集合上循环(前提是您的商店中有多个类别)。

    该答案下使用addPathsFilter('/' . $storecategoryid . '/') 的注释也是正确的,但在这种情况下会比使用LIKE 运算符慢(请参阅article

    @Serjio 的方法在区分不同类别方面不起作用。它将做的是提取存储特定的翻译。示例商店 1 已将类别命名为 Rings,商店 2 已将相同类别命名为 My Rings。 如果我们使用他的代码从 store 2 进行调用,它将拉取所有类别,无论 Root 是什么,但会拉取 Store 2 的命名约定 - My Rings

    【讨论】:

      【解决方案5】:

      在 Magento 1.9 中

      $storeId=2;
      $rootCategoryId = Mage::app()->getStore($storeId)->getRootCategoryId();
      
      
                  $categories = Mage::getModel('catalog/category')
                      ->getCollection()
                      ->setStoreId($storeId)
                      ->addFieldToFilter('is_active', 1)
                      ->addAttributeToFilter('path', array('like' => "1/{$rootCategoryId}/%"))
                      ->addAttributeToSelect('*');
      
                      foreach($categories as $categorie)
                      {
                          $catid=$cat->getId();                   
                          $catname=$categorie->getName();
                          $catp=catp$categorie->getParent_id();
                      }
      

      【讨论】:

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