2012-02-27 43 views
6

Có cách nào dễ dàng để lọc một bộ sưu tập sản phẩm theo nhiều loại? Để nhận tất cả các mục trong bất kỳ danh mục được liệt kê nào? addCategoryFilter dường như không cho phép một mảng.Bộ sưu tập sản phẩm lọc Magento theo nhiều loại

Cách duy nhất để có được bộ sưu tập cho từng danh mục sở thích riêng biệt sau đó hợp nhất chúng?

Tôi hiểu nó được sử dụng để có thể có một cái gì đó giống như

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

hoặc tương tự, nhưng điều này không còn có thể kể từ 1.4.

Lưu ý: Tôi đang sử dụng 1.6, và trong trường hợp đó là của bất kỳ sử dụng, tôi đang sử dụng một cái gì đó như thế này:

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

Thật không may không có cách nào dễ dàng để làm điều đó. – xyz

Trả lời

4

Cách Magento làm việc bây giờ, là để có được những Store, và trên các cửa hàng , bạn có thể lấy các danh mục từ kho lưu trữ như $ oStoreCollection-> addCategoryFilter (mảng ('1', '2'));

Tôi đã xem qua một giải pháp có thể giúp bạn, tìm thấy ở đây tại địa chỉ:

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

Mã họ sử dụng, trông như thế này: Override Mage/Catalogue/mẫu/Resource/Eav/MySQL4/Sản phẩm/Bộ sưu tập, và thêm các phương pháp sau:

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

sau đó nó được gọi như thế này:

$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. 

HTH

+0

Cảm ơn vì điều đó. Tôi đã thêm cả hai, và bây giờ có được một lỗi phương pháp chưa biết. Vì một lý do nào đó nó không nhận ra những gì đã được thêm vào, bất cứ suy nghĩ nào? –

+0

'Lỗi nghiêm trọng: Gọi đến phương thức chưa xác định Mage_Catalog_Model_Resource_Product_Collection :: addCategoriesFilter() ....' –

+0

'Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Collection.php' dường như không được tải tại tất cả –

9

Đây là phương pháp không yêu cầu sửa đổi cho lõi. Nó được lấy từ this post với việc thêm mệnh đề 'nhóm' để xử lý các bản ghi sản phẩm trùng lặp.

$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

Kết nối đang hoạt động nhưng không lọc. Không có gì được thêm vào mệnh đề 'WHERE'. –

+1

Tôi đã thử giải pháp này và chạy vào cùng một vấn đề với @ButtleButkus ... Tôi thấy kết nối được thêm vào truy vấn được tạo ra, nhưng không có điều kiện WHERE. Chúng tôi đang sử dụng Magento EE 1.12 (aka CE 1.7). –

+5

Tôi đã từng bước qua mã và giải pháp này sẽ hoạt động khi danh mục phẳng của Magento bị tắt. Khi danh mục phẳng được bật, điều sau xảy ra. Phương thức addAttributeToFilter nhận ra rằng trường category_id không có trong danh sách SELECT và gọi addAttributeToSelect để thêm nó. Phương thức addAttributeToSelect sau đó không thành công vì category_id không phải là thuộc tính sản phẩm và không có bộ lọc nào được thêm vào bộ sưu tập. –

3

Nếu bạn muốn lọc vào nhiều danh mục, sử dụng AND (do đó, một sản phẩm phải nằm trong mục A, B và C xuất hiện, bạn cần phải có nhiều tham gia:

$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;
  • catalô Flat kích hoạt trong quản trị;
  • Hãy chắc chắn rằng bạn đã lưu trữ khối, trong đó bạn sẽ đặt này;
  • Đỗ không thêm này trong chủ đề trả ..
  • Các bên tham gia mã hóa cứng ở đây tái tạo này:

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

    không có 'cat_index.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

Lọc Bộ sưu tập sản phẩm sử dụng nhiều loại 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/>"; 
} 

Bạn có thể loại bỏ các điều kiện cho loại hình sản phẩm tức là type_id hoặc sửa đổi nó theo yêu cầu.

1

tôi quản lý để giải quyết này (sau nhiều lần thử và sai) với đoạn mã sau:

$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); 

Hy vọng điều này sẽ giúp người;)

Các vấn đề liên quan