Filter a product collection by two categories in Magento -


I'm trying to find the products include two categories I've got an example to obtain such products Which are in category 1 or category 2. I need products that are in CATEGORY1 and CATEGORY2

In the blog example is:.

  class ModuleName_Catalog_Model_Resource_Eav_Mysql4_Product_Collection extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection {public function addCategoriesFilter ($ categories) {$ alias = 'cat_index'; $ CategoryCondition = $ this- & gt; GetConnection () - & gt; QuoteInto (....? Store_id = & '$ alias'. Product_id = e.entity_id and' $ alias, $ this- & gt; getStoreId ()); $ Category Condition. = $ Alias Category_id IN ('. $ Categories.') '; $ This- & gt; GetSelect () - & gt; JoinInner (array ($ alias = & gt; $ this- & gt; receivable (list / category_product_index ')), $ categoryCondition, array (' status' = & gt; 'status')); $ This- & gt; _categoryIndexJoined = True; $ This- & gt; _joinFields ['position'] = array ('table' = & gt; $ alias, 'field' = & gt; 'position'); $ This return; }}  

When I am using this filter alone, it displays or queries on several categories. When I combine this filter with the ReadyProductCollection of Mage_Catalog_Model_Layer remove it anyhow the filter effect.

Can I change the filter to more and combine with readyProductCollection?

can

thank you

Will allow filtering by several categories, but if you have a complete display to load multiple collections:

  $ iNumberFeaturedItems = 4; $ OCurrentCategory = Dana :: Registry ('current_category'); $ OFeaturedCategory = Mage :: getModel ('Catalog / Category') - & gt; GetCollection () - & gt; AddAttributeToFilter ('name', 'featured') - & gt; GetFirstItem (); $ AfeaturedCollection = Dana :: getResourceModel ('list / product_collection') - & gt; AddAttributeToSelect (array ('name', 'price', 'small_image', 'url_key'), 'internal') - & gt; AddStoreFilter () - & gt; AddCategoryFilter ($ oFeaturedCategory) - & gt; AdCarchIDIDS ();  

The first step is to get the collection of products for a category (in this case, a special category). To get to the next step, note that not is a load display (ref Mage_Core_Model_Mysql4_Collection_Abstract :: getAllIds () )

  $ aFeaturedProdIds = $ aFeaturedCollection-> GetAllIds (); ($ AFeaturedProdIds) shuffle;  

Then get the ID for a second category:

$ aCurrentCatProdIds = $ oCurrentCategory-> GetProductCollection () - & gt; GetAllIds ();

and pierced the array to find the product IDs in two categories:

  $ aMergedProdIds = array_intersect ($ aFeaturedProdIds, $ aCurrentCatProdIds);  

In the case of this special use, as long as we do not have enough proxy products, we cross the tree up to the loop, unless we have a big enough match find (but prohibit root category!):

  while (count ($ aMergedProdIds) & lt; $ iNumberFeaturedItems & amp; amp; $ oCurrentCategory- & gt; getId ()! = Dana :: app () - & gt; GetStore () - & gt; getRootCategoryId ()): $ OCurrentCategory = $ oCurrentCategory-> getParentCategory (); $ AParentCatProdIds = $ oCurrentCategory-> getProductCollection () - & gt; GetAllIds (); $ AMergedProdIds = array_intersect ($ aFeaturedProdIds, $ aParentCatProdIds); Endwhile;  

Finally, filter our initial collections by the intersection of ID products, and return.

  $ aFeaturedItems = $ aFeaturedCollection-> gt; AddIdFilter (array_slice $ aMergedProdIds, 0, $ iNumberFeaturedItems)) - & gt; GetItems (); Return $ aFeaturedItems;  

Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -