【问题标题】:Load Subcategories by id in magento1在 magento1 中按 id 加载子类别
【发布时间】:2020-09-06 17:16:44
【问题描述】:

我在 magento 中有一个 phtml 文件。我正在使用此代码。但它会加载所有子类别。有什么办法可以排除某些子类别

$layer = Mage::getSingleton('catalog/layer');
$category = $layer->getCurrentCategory();
/* @var $category Mage_Catalog_Model_Category */
$collection = Mage::getModel('catalog/category')->getCollection();
/* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
$collection->addAttributeToSelect('url_key')
->addAttributeToSelect('name')
->addAttributeToSelect('is_anchor')
->addAttributeToSelect('image')
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren())
->setOrder('position', 'ASC')
->joinUrlRewrite()
->load();

 $_categories=$collection; 

$_subcategories = $_category->getChildrenCategories() 
 foreach ($_subcategories as $_subcategory)
{
//need to exclude some subcategory based on its id.
}

【问题讨论】:

    标签: php magento categories


    【解决方案1】:

    您可以通过以下方式过滤更多类别:

    • 他们的等级:addAttributeToFilter('level','1') - 这将只获得第一级类别;
    • 无论是否包含在菜单中:addAttributeToFilter('include_in_menu','1');

    您还可以通过检查他们的产品来进一步过滤类别,以查看他们是否有单独的活动(状态为 1)和可见以及列表(可见性 4)产品:

        Mage::getModel('catalog/category')->load($categoryId)
    ->getProductCollection()
        ->addAttributeToFilter('status', 1)
        ->addAttributeToFilter('visibility', 4);
    

    PS:您可能希望将此业务逻辑移动到 model 类中或至少在 block / helper 类中,以尊重 MVC(Model- Magento 1 平台的 View-Controller) 设计模式。

    【讨论】:

      猜你喜欢
      • 2014-04-22
      • 2013-10-27
      • 2021-07-12
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 2018-01-07
      • 1970-01-01
      • 2015-02-16
      相关资源
      最近更新 更多