4

I am working on a collection query of categories:

$categories_check = Mage::getModel('catalog/category')
    ->getCollection()               
    ->setStoreId($storeId)           
    ->addFieldToFilter('is_active', 1)       
    ->addAttributeToFilter('path', array('like' => "1/{$rootCategoryId}/%"))   
    ->setProductStoreId($storeId)   
    ->setLoadProductCount(1)   
    ->addAttributeToSelect('*');

It returns the categories below root category $rootCategoryId. My requirement is to change this query, that it works for an array of root categories.

Like when I pass the array $rootcat = array('15,20,25,35'); it provides me categories below these root categories.

0

Edited:

As basic of on your last question Programatically get category collection by multiple root category id .I have give you the answer.

Have create multiple OR condition for addFieldToFilter() for getting the result on basic of path field

You should this script which may be give u your required result.

require_once "YOurMagentoDirapp/Mage.php";
umask(0);
ini_set('display_errors', 1);
Mage::app("admin");


$buildArray=array();

$rootId = Mage_Catalog_Model_Category::TREE_ROOT_ID;
$rootids= array(12,15,18,20);

foreach($rootid as $id){

  $buildArray[]= array('attribute' =>'path','like'=>$rootId.'/'.$id.'/%');

}

$collection = Mage::getModel('catalog/category')->getCollection()
        ->addFieldToFilter('is_active',1)
->addFieldToFilter($buildArray);

For getting the query of a collection.You can try this.

$CollectionObject->getSelect()->__toString();

For you case. use below

echo $categories_check->getSelect()->__toString();

Donot use

  • ->setStoreId($storeId)
  • ->setProductStoreId($storeId)

  • ->setLoadProductCount(1)

  • I want this query also work for array of root category and give it sub category. – Mathew Oct 7 '15 at 7:00
  • do you have try my yesterday answer magento.stackexchange.com/a/85448/4564 – Amit Bera Oct 7 '15 at 7:01
  • i have check his/her last question on basic of that o have asnwer this question – Amit Bera Oct 7 '15 at 7:13
  • Why not use above condition in collection "->setStoreId($storeId)" etc. – Mathew Oct 7 '15 at 8:15
  • if i use then it may be one store wise collection.. not sure 100%. Was not tried yet' – Amit Bera Oct 7 '15 at 8:17
2

You can pass an array of conditions to addAttributeToFilter, then it will be combined with OR.

The syntax is as follows:

->addAttributeToFilter(array(
    array('attribute' => 'path', 'like' => "1/15/%"),
    array('attribute' => 'path', 'like' => "1/20/%"),
    array('attribute' => 'path', 'like' => "1/25/%"),
    array('attribute' => 'path', 'like' => "1/35/%"),
))

Create this array parameter with a loop over your input array and you are set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.