Filter your product collection into two categories in Magento

I am trying to find products that are in two categories. I found an example for getting category 1 or category2 products. http://www.alphadigital.cl/blog/lang/en-us/magento-filter-by-multiple-categories.html I need products that belong to category1 and category2.

Example blog:

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->getConnection()->quoteInto(
    $alias.'.product_id=e.entity_id AND '.$alias.'.store_id=? AND ',
    $this->getStoreId()
  );

  $categoryCondition.= $alias.'.category_id IN ('.$categories.')';

  $this->getSelect()->joinInner(
    array($alias => $this->getTable('catalog/category_product_index')),
    $categoryCondition,
    array('position'=>'position')
  );

  $this->_categoryIndexJoined = true;
  $this->_joinFields['position'] = array('table'=>$alias, 'field'=>'position' );

  return $this;

  }
}

When I use only this filter, it executes an OR query across several categories. When I combine this filter with prepareProductCollection of Mage_Catalog_Model_Layer it somehow removes the filter effect.

How can I change the filter to AND and combine it with prepareProductCollection?

thank

thank

+3
source share
3

, , :

$iNumberFeaturedItems = 4;
$oCurrentCategory = Mage::registry('current_category');
$oFeaturedCategory = Mage::getModel('catalog/category')->getCollection()
        ->addAttributeToFilter('name','Featured')
        ->getFirstItem();
$aFeaturedCollection = Mage::getResourceModel('catalog/product_collection')
        ->addAttributeToSelect(array('name', 'price', 'small_image', 'url_key'), 'inner')
        ->addStoreFilter()
        ->addCategoryFilter($oFeaturedCategory)
        ->addCategoryIds();

( , "" ). , , NOT (ref Mage_Core_Model_Mysql4_Collection_Abstract::getAllIds())

    $aFeaturedProdIds = $aFeaturedCollection->getAllIds();
    shuffle($aFeaturedProdIds);  //randomize the order of the featured products

:

    $aCurrentCatProdIds = $oCurrentCategory->getProductCollection()->getAllIds();

, , :

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

, , , ( !):

    while(count($aMergedProdIds) < $iNumberFeaturedItems && $oCurrentCategory->getId() != Mage::app()->getStore()->getRootCategoryId()): 
        $oCurrentCategory = $oCurrentCategory->getParentCategory();
        $aParentCatProdIds = $oCurrentCategory->getProductCollection()->getAllIds();
        $aMergedProdIds = array_intersect($aFeaturedProdIds,$aParentCatProdIds);
    endwhile;

, .

    $aFeaturedItems = $aFeaturedCollection->addIdFilter(array_slice($aMergedProdIds,0,$iNumberFeaturedItems))->getItems();
    return $aFeaturedItems;
+3

, magento 1.3, finset category_ids, .

, ,

.

+1

, addCategoryFilter() - . , .

-1
source

Source: https://habr.com/ru/post/1757863/


All Articles