Magento - Filter category resource collection by store_id

前端 未结 6 1421
谎友^
谎友^ 2021-01-03 18:54

I am trying to pull category that belongs to current store only but it doesn\'t seem to work. Can anyone see any issue in my code?

$categoryCollection = Mage         


        
相关标签:
6条回答
  • 2021-01-03 19:27

    i know it is an old question, but if someone is looking for the answer as i just was:

    ->addStoreFilter( {storeID} )
    

    did it for me...

    0 讨论(0)
  • 2021-01-03 19:39

    In Magento 1.9

    $storeId=2;
        $rootCategoryId = Mage::app()->getStore($storeId)->getRootCategoryId();
    
                    $categories = Mage::getModel('catalog/category')
                        ->getCollection()
                        ->setStoreId($storeId)
                        ->addFieldToFilter('is_active', 1)
                        ->addAttributeToFilter('path', array('like' => "1/{$rootCategoryId}/%"))
                        ->addAttributeToSelect('*');
    
                        foreach($categories as $categorie)
                        {
                            $catid=$cat->getId();                   
                            $catname=$categorie->getName();
                            $catp=catp$categorie->getParent_id();
    
                        }
    
    0 讨论(0)
  • 2021-01-03 19:43

    I spent a lot of time.... this exaple work for me...

    $store = Mage::app()->getStore()->getId();
    $rootCategoryId = Mage::app()->getStore()->getRootCategoryId();
    
    $rootpath = Mage::getModel('catalog/category')
                        ->setStoreId($store)
                        ->load($rootCategoryId)
                        ->getPath();
    
    $categories = Mage::getModel('catalog/category')->setStoreId($store)
                        ->getCollection()
                        ->addAttributeToSelect('*')
                        ->addAttributeToFilter('path', array("like"=>$rootpath."/"."%")); 
    

    fix for multistore :)

    setStoreId - not work, can remove

    0 讨论(0)
  • 2021-01-03 19:46

    Neither setStoreId() nor addAttributeToFielter('store_id', $storeId) don't work because there's no store_id in the category tables. The code below works perfectly:

    $storeId = 21; // your store id
    $rootCategoryId = Mage::app()->getStore($storeId)->getRootCategoryId();
    $categories = Mage::getModel('catalog/category')->getCollection();
    $categories->addAttributeToFilter('path', array('like' => "1/{$rootCategoryId}/%"));
    
    0 讨论(0)
  • 2021-01-03 19:47

    Try this

    $categoriesCollection = Mage::getModel('catalog/category')
    ->getCollection()
    ->setStoreId(1) 
    ->addFieldToFilter('include_in_menu', array('eq' => 1))
    ->addFieldToFilter('level', array('eq' => 2))
    ->addFieldToFilter('is_active', array('eq'=>'1'))
    ->setOrder('position', 'asc')
    ->addAttributeToSelect('*');
    
    0 讨论(0)
  • 2021-01-03 19:48

    Hello check following code may be help you

    ->addFieldToFilter('store_id', Mage::app()->getStore()->getId());
    

    OR

    $storeId =Mage::app()->getStore()->getStoreId();
    
    $collection->setStoreId($storeId);
    
    $collection->addStoreFilter($storeId);
    
    0 讨论(0)
提交回复
热议问题