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

aah*_*haa 4 php magento

$categories在整个页面中使用它

$categories = Mage::getModel('catalog/category')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('level',2)
    ->addIsActiveFilter()
    ->addAttributeToSort('position'); 

foreach($categories as $cat) {$children=$cat->getChildrenCategories();}
Run Code Online (Sandbox Code Playgroud)

选项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
Run Code Online (Sandbox Code Playgroud)

选项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. 
Run Code Online (Sandbox Code Playgroud)

有人可以解释差异吗?以及我如何选择2

Ami*_*era 6

基本上,当我们使用 getChildrenCategories()函数时,仅从类别字段collection->中检索了很少的字段url_key,name,all_children,is_anchor

您可以在类上看到它Mage_Catalog_Model_Resource_Category。因此,如果要从函数获取图像网址,则只需添加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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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