Created
July 26, 2014 04:14
-
-
Save deivisonarthur/b995fc6495b567f2dd4a to your computer and use it in GitHub Desktop.
From http://stackoverflow.com/questions/9468059/magento-filter-product-collection-by-multiple-categories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment