使用 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

而不是调用 getModel() 尝试调用 getSingleton().

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

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

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

你可以在 class 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()

如果使用 magento 2

我尝试了以下,这样它不会影响性能,你需要单独调用类别模型

在 magento 2.2 的根目录中打开

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

在线750左右

添加

->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;
    }