使用getChildrenCategories获取类别图像->;getImageUrl(MAGENTO)


Get Category image in with getChildrenCategories->getImageUrl (MAGENTO)

我在整个页面中使用这个$categories

$categories = Mage::getModel('catalog/category')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('level',2)
    ->addIsActiveFilter()
    ->addAttributeToSort('position'); 
foreach($categories as $cat) {$children=$cat->getChildrenCategories();}

选项1

//option 1 
$children  = $category->getChildren();
foreach(explode(',', $children) as $child):
$sub = Mage::getModel('catalog/category')->load($child);
$sub->getName() // this return ok, name show up
$sub->getImageUrl() // this return ok, image show up

选项2有效,但由于某些原因无法获取图像url。

//option 2 
$children  = $category->getChildrenCategories();
foreach($children as $sub):
$sub->getName() // this return ok, name show up
$sub->getImageUrl() // this return empty, image NOT show up so is other attribute beside name. 

有人能解释一下区别吗?以及如何使用opt 2

基本上,当我们使用getChildrenCategories()函数时,只有少数字段从类别字段集合->url_key,name,all_children,is_anchor中检索到。

你可以在类Mage_Catalog_Model_Resource_Category上看到这一点。所以,如果想要从函数中获取图像url,那么只需添加addAttributeToFilter('image')

  $collection = $category->getCollection();
$collection->addAttributeToSelect('url_key')
    ->addAttributeToSelect('name')
    ->addAttributeToSelect('all_children')
    ->addAttributeToSelect('is_anchor')
    ->setOrder('position', Varien_Db_Select::SQL_ASC)
    ->joinUrlRewrite()
->addAttributeToFilter('is_active', 1)
    ->addIdFilter($category->getChildren())
->addAttributeToSelect('image');

foreach($category as $eachChildCat){
if ($image = $eachChildCat->getImage()) {
    $url = Mage::getBaseUrl('media').'catalog/category/'.$image;
    }
}

$eachChildCat->getImage()不起作用,然后使用$eachChildCat->getResource()->getImage

不要调用getModel(),而是尝试调用getSingleton()

如果你想在第二个选项中获得图像url,你必须加载类别:

$subcategory = Mage::getModel('catalog/category')->load($sub->getId());
$imageUrl = $subcategory->getImageUrl();

如果使用magento 2

我尝试了以下操作,这样它就不会影响性能,您需要将类别模型单独称为

在magento 2.2 的根目录中打开

/vendor/magento/module catalog/Model/ResourceModel/Category.php

在线750ish

添加

->addAttributeToSelect(
    'image'
)

或替换功能

 /**
     * Return child categories
     *
     * @param 'Magento'Catalog'Model'Category $category
     * @return 'Magento'Catalog'Model'ResourceModel'Category'Collection
     */
    public function getChildrenCategories($category)
    {
        $collection = $category->getCollection();
        /* @var $collection 'Magento'Catalog'Model'ResourceModel'Category'Collection */
        $collection->addAttributeToSelect(
            'url_key'
        )->addAttributeToSelect(
            'image'
        )->addAttributeToSelect(
            'name'
        )->addAttributeToSelect(
            'all_children'
        )->addAttributeToSelect(
            'is_anchor'
        )->addAttributeToFilter(
            'is_active',
            1
        )->addIdFilter(
            $category->getChildren()
        )->setOrder(
            'position',
            'Magento'Framework'DB'Select::SQL_ASC
        )->joinUrlRewrite();
        return $collection;
    }