繁体   English   中英

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>

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM