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

时间:2015-02-24 19:52:42

标签: php 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有效,但由于某种原因无法获取图片网址。

//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. 

有人可以解释这个区别吗?我将如何选择2

4 个答案:

答案 0 :(得分:5)

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

你可以在课程Mage_Catalog_Model_Resource_Category上看到。所以如果想从函数中获取tget图片网址,那么只需添加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()

答案 1 :(得分:1)

而不是致电getModel()尝试拨打getSingleton()

答案 2 :(得分:1)

如果您想在第二个选项中获取图片网址,则必须加载类别:

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

答案 3 :(得分:0)

如果使用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;
    }