Magento获取此错误:致命错误:在非对象上调用成员函数getAttributeText()


Magento getting this error: Fatal error: Call to a member function getAttributeText() on a non-object

我在为显示制造商的图像而创建的新模板中收到了上述错误。我在view.phtml文件中使用了这个,它运行得很好。但既然这是惯例,我想会有问题的。希望我能弄清楚这个。

这是我当前的代码:

<?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_Catalog_Block_Product_Abstract的后代,尽管还有其他的。创建块时,可以指定满足您需要的任何类型。

<block type="catalog/product_view" template="path/to/your/template.phtml" />

这些块从注册表中获取其产品对象,该注册表仅由目录控制器为产品页设置。如果必须使用另一个没有getProduct()功能的块类型,则可以直接访问注册表。

$product = Mage::registry('current_product');

在我看来,你需要来自给定类别中的产品的manufacturer,并且有几个类别需要检查。我认为任何一个类别的产品都可以。这不需要块是catalog/product_view类型。

<?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>

我也只是使用了一个普通的块类型的核心/模板,而不是之前的答案中建议的。这对我来说很有魅力。