1

我有一个主类别(父类别),其 id = 5 & 37。我想收集它的子类别。我怎样才能做到这一点?

$catid = array(5,37);


$_category = Mage::getModel('catalog/category')->load(5);
$_subcategories1 = $_category->getChildrenCategories();
$_category = Mage::getModel('catalog/category')->load(37);
$_subcategories2 = $_category->getChildrenCategories();  

我想要包含两个类别 id(5,37) 的子类别的集合

4

2 回答 2

2

你可以从一个选择中得到它:

$subcategories = Mage::getModel('catalog/category')
    ->setStoreId(Mage::app()->getStore()->getId())
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('parent_id', array(5, 37))
    ->setOrder('parent_id', 'ASC');//if you want them somehow grouped by parent_id
foreach ($subcategories as $category){
    //do something with $category
}
于 2013-09-03T09:23:18.100 回答
0

这里我给你一个例子,将两个类别集合合并到一个集合中

  $storeId = Mage::app()->getStore()->getId();
    $categoryOneId = 5;
    $categoryTwoId = 37;

    $categoryOne = Mage::getModel('catalog/category')->load($categoryOneId);
    $categoryTwo = Mage::getModel('catalog/category')->load($categoryTwoId);

    $collectionOne = Mage::getModel('catalog/product')->getCollection()
        ->setStoreId($storeId)
        ->addCategoryFilter($categoryOne);

    $collectionTwo = Mage::getModel('catalog/product')->getCollection()
        ->setStoreId($storeId)
        ->addCategoryFilter($categoryTwo);

    $merged_ids = array_merge($collectionOne->getAllIds(), $collectionTwo->getAllIds());

    $mergedCollection = Mage::getModel('catalog/product')->getCollection()
        ->addFieldToFilter('entity_id', $merged_ids);

希望这对你有帮助。

于 2013-09-03T09:14:31.250 回答