2012-12-07 43 views
0

我試圖拉類屬於當前商店只,但它似乎並沒有工作。任何人都可以在代碼中看到任何問題?Magento - 通過store_id篩選類別資源集合

$categoryCollection = Mage::getResourceModel('catalog/category_collection') 
    ->setStoreId(Mage::app()->getStore()->getId()) 
    ->addFieldToFilter('include_in_menu', array('eq' => 1)) 
    ->addFieldToFilter('is_active', array('eq' => 1)) 
    ->addFieldToFilter('level', array('eq' => 2)) 
    ->addAttributeToSelect(array('name','url_path','image','description')) 
    ->setOrder('position', 'asc'); 

$categoryCollection->printLogQuery(true); 

這是從STORE_ID 0獲取數據爲好,但我想只從商店編號2

SELECT `e`.*, IF(at_include_in_menu.value_id > 0, at_include_in_menu.value, at_include_in_menu_default.value) AS `include_in_menu`, IF(at_is_active.value_id > 0, at_is_active.value, at_is_active_default.value) AS `is_active` FROM `catalog_category_entity` AS `e` 
INNER JOIN `catalog_category_entity_int` AS `at_include_in_menu_default` ON (`at_include_in_menu_default`.`entity_id` = `e`.`entity_id`) AND (`at_include_in_menu_default`.`attribute_id` = '67') AND `at_include_in_menu_default`.`store_id` = 0 
LEFT JOIN `catalog_category_entity_int` AS `at_include_in_menu` ON (`at_include_in_menu`.`entity_id` = `e`.`entity_id`) AND (`at_include_in_menu`.`attribute_id` = '67') AND (`at_include_in_menu`.`store_id` = 2) 
INNER JOIN `catalog_category_entity_int` AS `at_is_active_default` ON (`at_is_active_default`.`entity_id` = `e`.`entity_id`) AND (`at_is_active_default`.`attribute_id` = '42') AND `at_is_active_default`.`store_id` = 0 
LEFT JOIN `catalog_category_entity_int` AS `at_is_active` ON (`at_is_active`.`entity_id` = `e`.`entity_id`) AND (`at_is_active`.`attribute_id` = '42') AND (`at_is_active`.`store_id` = 2) WHERE (`e`.`entity_type_id` = '3') AND (IF(at_include_in_menu.value_id > 0, at_include_in_menu.value, at_include_in_menu_default.value) = 1) AND (IF(at_is_active.value_id > 0, at_is_active.value, at_is_active_default.value) = 1) AND (`e`.`level` = 2) 

UPDATE

而是存儲濾波器的,我這解決了剛添加的路徑過濾器問題,但仍然很想知道商店過濾器是否有效。

$storeId = Mage::app()->getStore()->getId(); 
$categoryRootId = Mage::app()->getStore($storeId)->getRootCategoryId();; 

$categoryCollection = Mage::getResourceModel('catalog/category_collection') 
    ->addFieldToFilter('path', array('like' => "%/{$categoryRootId}/%")) 
    ->addFieldToFilter('include_in_menu', array('eq' => 1)) 
    ->addFieldToFilter('is_active', array('eq' => 1)) 
    ->addFieldToFilter('level', array('eq' => 2)) 
    ->addAttributeToSelect(array('name','url_path','image','description','store_id')) 
    ->setOrder('position', 'asc') 
    ->load(); 

回答

0

您好檢查下面的代碼,可以幫助你

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

OR

$storeId =Mage::app()->getStore()->getStoreId(); 

$collection->setStoreId($storeId); 

$collection->addStoreFilter($storeId); 
+0

我試過那太多,但沒有奏效。 –

0

試試這個

$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

這也行不通。測試和相同的結果。看起來只有路徑過濾器完美工作。 –

2

我知道這是一個老問題,但如果有人正在尋找答案,因爲我只是:

->addStoreFilter({storeID}) 

爲我做...

2

我花了很多時間.... 這對我來說的exaple工作...

$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."/"."%")); 

修復了MultiStore的 :)

setStoreId - 不行,可以去除

0

setStoreId()addAttributeToFielter('store_id', $storeId)都不起作用,因爲類別表中沒有store_id。下面的代碼工作完美:

$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

在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(); 

        }