Php Magento收到此错误:致命错误:对非对象调用成员函数getAttributeText()
为了显示制造商的图像,我创建了一个新模板,其中出现了上述错误。我在view.phtml文件中使用它,它工作得很好。但由于这是定制的,我想会有问题。希望我能想出这个办法 这是我目前的代码:Php Magento收到此错误:致命错误:对非对象调用成员函数getAttributeText(),php,magento,Php,Magento,为了显示制造商的图像,我创建了一个新模板,其中出现了上述错误。我在view.phtml文件中使用它,它工作得很好。但由于这是定制的,我想会有问题。希望我能想出这个办法 这是我目前的代码: <?php $layer = Mage::getSingleton('catalog/layer'); $category = $layer->getCurrentCategory(); $currentCategoryId = $category->getId();
<?php
$layer = Mage::getSingleton('catalog/layer');
$category = $layer->getCurrentCategory();
$currentCategoryId = $category->getId();
$children = Mage::getModel('catalog/category')->getCategories($currentCategoryId);
$product = $this->getProduct();
$brand = $product->getAttributeText('manufacturer');
?>
<div class="landing-page nested-container">
<?php foreach ($children as $category): ?>
<div class="vertical-section grid12-4 mobile-grid-half">
<a href="<?php echo $category->getRequestPath(); ?>">
<img class="center-block" alt="<?php echo $category->getName(); ?>" src="<?php echo $this->getBaseUrl()."media/catalog/category/".Mage::getModel('catalog/category')->load($category->getId())->getThumbnail(); ?>" />
<div class="caption category-boxes-logo full-width">
<?php echo '<img src="'.$this->getBaseUrl().'media/wysiwyg/infortis/brands/'.str_replace(' ', '-', strtolower($brand)).'.jpg" alt="'.$brand.'"></a>' ?>
</div>
</a>
</div>
<?php endforeach; ?>
</div>
' ?>
对于支持它的块,只能调用getProduct()
。这通常意味着Mage\u Catalog\u Block\u Product\u Abstract
的后代,尽管还有其他后代。创建块时,可以指定满足需要的任何类型
<block type="catalog/product_view" template="path/to/your/template.phtml" />
在我看来,您需要特定类别产品的
制造商
,并且有几个类别需要检查。我假设某个类别的任何产品都可以。这不要求块是目录/产品视图类型
<?php
$layer = Mage::getSingleton('catalog/layer');
$category = $layer->getCurrentCategory();
$children = $category->getChildrenCategories();
?>
<div class="landing-page nested-container">
<?php foreach ($children as $category): ?>
<!-- Load a product brand per category -->
<?php $product = $category->getProductCollection()->getFirstItem() ?>
<?php $brand = $product ? $product->getAttributeText('manufacturer') : 'default' ?>
<div class="vertical-section grid12-4 mobile-grid-half">
<a href="<?php echo $category->getRequestPath(); ?>">
<img class="center-block" alt="<?php echo $category->getName(); ?>" src="<?php echo $this->getBaseUrl()."media/catalog/category/".$category->getId()->getThumbnail(); ?>" />
<div class="caption category-boxes-logo full-width">
<?php echo '<img src="'.$this->getBaseUrl().'media/wysiwyg/infortis/brands/'.str_replace(' ', '-', strtolower($brand)).'.jpg" alt="'.$brand.'"></a>' ?>
</div>
</a>
</div>
<?php endforeach; ?>
</div>
' ?>
我知道我能做什么。我只是使用了产品名称,因为它们无论如何都应该匹配,并且使用了我试图为制造商使用的相同字符串。我将我的答案发布给任何希望得到答案的人:
<?php
$layer = Mage::getSingleton('catalog/layer');
$category = $layer->getCurrentCategory();
$currentCategoryId = $category->getId();
$children = Mage::getModel('catalog/category')->getCategories($currentCategoryId);
?>
<div class="landing-page nested-container">
<?php foreach ($children as $category): ?>
<div class="vertical-section grid12-4 mobile-grid-half">
<a href="<?php echo $category->getRequestPath(); ?>">
<img class="center-block" alt="<?php echo $category->getName(); ?>" src="<?php echo $this->getBaseUrl()."media/catalog/category/".Mage::getModel('catalog/category')->load($category->getId())->getThumbnail(); ?>" />
<div class="caption category-boxes-logo full-width">
<img src="<?php echo $this->getBaseUrl().'media/wysiwyg/infortis/brands/'.str_replace(' ', '_', strtolower($category->getName())).'.png' ?>" alt="<?php echo $category->getName(); ?>">
</div>
</a>
</div>
<?php endforeach; ?>
</div>
我也只是使用了一个普通的块类型的核心/模板,而不是在前面的答案中建议的。这对我来说很有魅力。
var\u dump($product)代码>我应该把它放在哪里?$product不是一个对象,因为$this没有它。试着检查$this是什么。我试着做一个var_转储,但仍然得到一个空的致命错误Trydie(get_class($this))
,这将只给出块的类名,这很重要。或者你可以告诉我们你创建了什么类型的块。我现在在尝试以下操作时遇到了这个错误:致命错误:在第56行的/html/app/code/core/Mage/Catalog/block/Product/View.php中的非对象上调用成员函数getMetaTitle()。我正在使用CMS块生成模板。这就是编码现在的样子:{{block type=“catalog/product\u view”template=“catalog/category/list.phtml”}您的CMS语法看起来是正确的。如果没有注册“当前产品”,则会出现上述错误,这表明您正在查看的页面不是产品详细信息页面。不,不是。我很抱歉没有澄清这一点。这是我在类别的登录页上使用的自定义块。我可以把链接发给你,这样你就可以看到我想让它工作的地方。你希望得到什么产品?是否有其他方式获得品牌/制造商?如有必要,可以为类别而不是产品创建属性。
<?php
$layer = Mage::getSingleton('catalog/layer');
$category = $layer->getCurrentCategory();
$currentCategoryId = $category->getId();
$children = Mage::getModel('catalog/category')->getCategories($currentCategoryId);
?>
<div class="landing-page nested-container">
<?php foreach ($children as $category): ?>
<div class="vertical-section grid12-4 mobile-grid-half">
<a href="<?php echo $category->getRequestPath(); ?>">
<img class="center-block" alt="<?php echo $category->getName(); ?>" src="<?php echo $this->getBaseUrl()."media/catalog/category/".Mage::getModel('catalog/category')->load($category->getId())->getThumbnail(); ?>" />
<div class="caption category-boxes-logo full-width">
<img src="<?php echo $this->getBaseUrl().'media/wysiwyg/infortis/brands/'.str_replace(' ', '_', strtolower($category->getName())).'.png' ?>" alt="<?php echo $category->getName(); ?>">
</div>
</a>
</div>
<?php endforeach; ?>
</div>