如何获得两个父类别的子类别

时间:2013-09-03 09:04:06

标签: magento

我有一个主要类别(父类别),其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)

的子类别的集合

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分: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);

希望这对您有所帮助。