2012-02-27 16 views
6

複数のカテゴリで商品コレクションを簡単にフィルタリングする方法はありますか?リストされたカテゴリのすべてのアイテムを取得するには? addCategoryFilterは配列を許可していないようです。個別にそれらをマージ対象の各カテゴリのコレクションを取得する唯一の方法は、複数のカテゴリによるMagentoフィルタ製品のコレクション

ですか?

私はそれが

addAttributeToFilter('category_ids',array('finset'=>array('1','2'))) 

または類似のようなものを可能にするために使用が、これは1.4以降もはや可能であることを理解しています。

注:ストアを取得するには、

$product = Mage::getModel('catalog/product'); 
$_productCollection = $product->getCollection() 
    ->addAttributeToSelect('*') 
    ->addAttributeToFilter('status',1) 
    ->addStoreFilter(); 
+0

Unfortunetlyそれを行うための簡単な方法がありません。 – xyz

答えて

4

Magentoのは、現在の動作方法であり、店舗の:私は1.6を使用していますし、場合には、それはあらゆる使用のだ、私はこのようなものを使用しています$ oStoreCollection-> addCategoryFilter(array( '1'、 '2'))のようにstorecollectionからカテゴリを取得できます。

私はでここに見つけ、あなたを助けるかもしれない解決策に出くわした:次のようになり、

http://www.magentocommerce.com/boards/&/viewthread/201114/#t329230

彼らが使用するコードを: オーバーライドメイジ/カタログ/モデル/リソース/ EAV/Mysql4 /製品/コレクション、および以下のメソッドを追加:

$collection = Mage::getModel('catalog/product')->getCollection() 
         ->addAttributeToSelect('*') 
         ->distinct(true) // THIS IS WHAT YOU NEED TO ADD 
         ->addCategoriesFilter($category->getAllChildren(true)); // Make sure you don't forget to retrieve your category here. 
:それは、このように呼び出されます

public function addCategoriesFilter($categories) 
    { 
     $this->_productLimitationFilters['category_ids'] = $categories; 

     if ($this->getStoreId() == Mage_Core_Model_App::ADMIN_STORE_ID) { 
      $this->_applyZeroStoreProductLimitations(); 
     } else { 
      $this->_applyProductLimitations(); 
     } 

     return $this; 
    } 

    protected function _applyProductLimitations() 
    { 
     $this->_prepareProductLimitationFilters(); 
     $this->_productLimitationJoinWebsite(); 
     $this->_productLimitationJoinPrice(); 
     $filters = $this->_productLimitationFilters; 

     // Addition: support for filtering multiple categories. 
     if (!isset($filters['category_id']) && !isset($filters['category_ids']) && !isset($filters['visibility'])) { 
      return $this; 
     } 

     $conditions = array(
      'cat_index.product_id=e.entity_id', 
      $this->getConnection()->quoteInto('cat_index.store_id=?', $filters['store_id']) 
     ); 
     if (isset($filters['visibility']) && !isset($filters['store_table'])) { 
      $conditions[] = $this->getConnection() 
       ->quoteInto('cat_index.visibility IN(?)', $filters['visibility']); 
     } 

     // Addition: support for filtering multiple categories. 
     if (!isset($filters['category_ids'])) { 
      $conditions[] = $this->getConnection() 
       ->quoteInto('cat_index.category_id=?', $filters['category_id']); 
      if (isset($filters['category_is_anchor'])) { 
       $conditions[] = $this->getConnection() 
        ->quoteInto('cat_index.is_parent=?', $filters['category_is_anchor']); 
      } 
     } else { 
      $conditions[] = $this->getConnection()->quoteInto('cat_index.category_id IN(' . implode(',', $filters['category_ids']) . ')', ""); 
     } 

     $joinCond = join(' AND ', $conditions); 
     $fromPart = $this->getSelect()->getPart(Zend_Db_Select::FROM); 
     if (isset($fromPart['cat_index'])) { 
      $fromPart['cat_index']['joinCondition'] = $joinCond; 
      $this->getSelect()->setPart(Zend_Db_Select::FROM, $fromPart); 
     } 
     else { 
      $this->getSelect()->join(
       array('cat_index' => $this->getTable('catalog/category_product_index')), 
       $joinCond, 
       array('cat_index_position' => 'position') 
      ); 
     } 

     $this->_productLimitationJoinStore(); 

     Mage::dispatchEvent('catalog_product_collection_apply_limitations_after', array(
      'collection' => $this 
     )); 

     return $this; 
    } 

    protected function _applyZeroStoreProductLimitations() 
    { 
     $filters = $this->_productLimitationFilters; 

     // Addition: supprot for filtering multiple categories. 
     $categoryCondition = null; 
     if (!isset($filters['category_ids'])) { 
      $categoryCondition = $this->getConnection()->quoteInto('cat_pro.category_id=?', $filters['category_id']); 
     } else { 
      $categoryCondition = $this->getConnection()->quoteInto('cat_pro.category_id IN(' . implode(',', $filters['category_ids']) . ')', ""); 
     } 

     $conditions = array(
      'cat_pro.product_id=e.entity_id', 
      $categoryCondition 
     ); 
     $joinCond = join(' AND ', $conditions); 

     $fromPart = $this->getSelect()->getPart(Zend_Db_Select::FROM); 
     if (isset($fromPart['cat_pro'])) { 
      $fromPart['cat_pro']['joinCondition'] = $joinCond; 
      $this->getSelect()->setPart(Zend_Db_Select::FROM, $fromPart); 
     } 
     else { 
      $this->getSelect()->join(
       array('cat_pro' => $this->getTable('catalog/category_product')), 
       $joinCond, 
       array('cat_index_position' => 'position') 
      ); 
     } 

     return $this; 
    } 

HTH

+0

ありがとう。私は両方を追加しましたが、今は未知のメソッドエラーが発生します。何らかの理由で、何が追加されたのか、何らかの考えを認識していないのですか? –

+0

'Mage_Catalog_Model_Resource_Product_Collection :: addCategoriesFilter()....' –

+0

'Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Collection.php'はロードされていないようです。すべて –

9

ここでは、コアを変更する必要はありません。 this postから、重複した製品レコードを処理する 'group'句が追加されています。

$categories = array(7,45,233); 

     $collection = Mage::getModel('catalog/product')->getCollection() 
      ->addAttributeToSelect('*') 
      ->joinField('category_id', 
       'catalog/category_product', 
       'category_id', 
       'product_id=entity_id', 
       null, 
       'left') 
      ->addAttributeToFilter('category_id', array('in' => $categories)); 
     $collection->getSelect()->group('e.entity_id'); 
+0

結合は機能していますが、フィルタリングは機能していません。 WHERE句には何も追加されません。 –

+1

私はこの解決策も試してみましたが、@ButtleButkusと同じ問題に遭遇しました...生成されたクエリに結合が追加されていますが、WHERE条件はありません。 Magento EE 1.12(別名CE 1.7)を使用しています。 –

+5

私はコードを進めてきましたが、このソリューションはMagentoのフラットカタログがオフのときに機能します。フラットカタログを有効にすると、次のようになります。 addAttributeToFilterメソッドは、category_idフィールドがSELECTリストにないことを認識し、addAttributeToSelectを呼び出して追加します。 category_idは製品属性ではないため、コレクションにフィルタが追加されないため、addAttributeToSelectメソッドは失敗します。 –

3

あなたが使用して、複数のカテゴリにフィルタリングするAND(その製品が表示されるまでcategorieのA、BおよびCにする必要がある場合、あなたは複数を持っている必要がありますが加わり:

$products = Mage::getModel('catalog/product')->getCollection() 
    ->joinField('category_id_1', 'catalog/category_product', 'category_id', 'product_id=entity_id', null, 'left') 
    ->joinField('category_id_2', 'catalog/category_product', 'category_id', 'product_id=entity_id', null, 'left') 
    ->addAttributeToFilter('category_id_1', array('eq' => 358)) 
    ->addAttributeToFilter('category_id_2', array('eq' => 252)) 
// etc... 
; 
0
  • Magentoの1.8.0.0、管理に有効
  • フラットカタログ;
  • あなたはこれを配置しますれたブロックをキャッシュされていることを確認してください。
  • ドゥcat_index」なし

    $collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());

  • インナーがここにハードコードされ、これを再現する参加..支払わテーマでこれを追加しません。CATEGORY_ID = 2'

$category = Mage::getModel('catalog/category')->load(100); 
$allChildsIds = $category->getAllChildren($category); 

$visibility = Mage::getModel('catalog/product_visibility'); 

$collection = Mage::getResourceModel('catalog/product_collection'); 
$collection = $this->_addProductAttributesAndPrices($collection) 
    ->addStoreFilter() 
    ->setFlag('do_not_use_category_id', true) 
    ->setFlag('disable_root_category_filter', true) 
    ->addAttributeToSort('created_at', 'desc'); 

$whereCategoryCondition = $collection->getConnection() 
    ->quoteInto('cat_index.category_id IN(?) ', $allChildsIds); 
$collection->getSelect()->where($whereCategoryCondition); 

$conditions = array(); 
$conditions[] = "cat_index.product_id = e.entity_id"; 
$conditions[] = $collection->getConnection() 
    ->quoteInto('cat_index.store_id = ? ', Mage::app()->getStore()->getStoreId()); 
$conditions[] = $collection->getConnection() 
    ->quoteInto('cat_index.visibility IN(?) ', $visibility->getVisibleInCatalogIds()); 

$collection->getSelect()->join(
    array('cat_index' => $collection->getTable('catalog/category_product_index')), 
    join(' AND ', $conditions), 
    array() 
); 

$collection 
    ->setPageSize(3) 
    ->setCurPage(1); 

$collection->load(); 
0

フィルタープロダクトコレクション使用して、複数のカテゴリID

$all_categories = array('3','13','113'); 
$productCollection = Mage::getModel('catalog/product')->getCollection(); 
$productCollection->joinField('category_id', 'catalog/category_product', 'category_id', 
        'product_id = entity_id', null, 'left') 
        ->addAttributeToSelect('*') 
        ->addAttributeToFilter('type_id', array('eq' => 'simple')) 
        ->addAttributeToFilter('category_id', array($all_categories)); 
foreach($productCollection as $product) 
{ 
    echo $product->getId() .$product->getName() . "<br/>"; 
} 

あなたはをTYPE_IDすなわち、製品の種類の条件を削除するか、要件ごとにそれを変更することができます。

1

私は次のコードで(多くの試行錯誤の後に)これ​​を解決するために管理:

$collection = Mage::getModel('catalog/product')->getCollection(); 
$collection->addAttributeToFilter('status', 1); 
$collection->addAttributeToSelect(array('name','sku','price','small_image')); 

// Filter by multiple categories 
$collection->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left'); 
$data_cats = $this->getRequest()->getParam('categories'); 
// Or $data_cats = array(85,86,87,88); 

     $filter_cats = array(); 
     foreach ($data_cats as $value_cats) { 
     $filter_cats[] = array(
     'attribute' => 'category_id', 
     'finset' => $value_cats 
    ); 
} 

$collection->addAttributeToFilter($filter_cats); 

希望を、これは誰かに役立ちます;)

関連する問題