简体   繁体   English

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

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

I am getting the error above in a new template I created in order to show the manufacturer's image. 我在为了显示制造商图像而创建的新模板中遇到了以上错误。 I am using this in the view.phtml file and it works just fine. 我在view.phtml文件中使用它,并且工作正常。 But since this is custom, I figured there would be problems. 但是由于这是习惯,所以我认为会有问题。 Hopefully I can figure this one out. 希望我能弄清楚这一点。

Here is my current code: 这是我当前的代码:

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

You may only call getProduct() for a block which supports it. 您只能为支持它的块调用getProduct() That generally means a descendant of Mage_Catalog_Block_Product_Abstract although there are others. 这通常意味着Mage_Catalog_Block_Product_Abstract的后代,尽管还有其他人。 When creating a block you can specify any type that meets your needs. 创建块时,可以指定满足您需要的任何类型。

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

These blocks get their product object from the registry, which is only set for product pages by the catalog controller. 这些块从注册表获取其产品对象,注册表仅由目录控制器为产品页面设置。 If you have to use another block type which does not have a getProduct() function then you can access the registry directly. 如果必须使用另一种没有getProduct()函数的块类型,则可以直接访问注册表。

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

It seems to me that you need the manufacturer from products in a given category, and there are several categories to inspect. 在我看来,您需要特定类别产品的manufacturer ,并且有几个类别需要检查。 I assume any product from a category will do. 我认为任何类别的产品都可以。 This does not require the block to be a catalog/product_view type. 这不需要块为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>

I figured out what I can do. 我想出了我能做的。 I just used the product name as they should be matching anyway and used the same string I was trying to use for the Manufacturer. 我只是使用了产品名称,因为它们无论如何都应该匹配,并使用了与制造商相同的字符串。 I am posting my answer for anyone who may want an answer down the road: 我将我的答案发布给任何可能在未来想要答案的人:

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

I also just used a normal block type of core/template instead of what was suggested in a previous answer. 我也只使用了普通的核心/模板块类型,而不是先前答案中建议的类型。 This worked like a charm for me. 这对我来说就像是一种魅力。

暂无
暂无

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

相关问题 Magento:致命错误:在非对象上调用成员函数 getMethodInstance() - Magento : Fatal error: Call to a member function getMethodInstance() on a non-object Magento致命错误-在非对象上调用成员函数getId() - Magento Fatal Error - Call to a member function getId() on a non-object Magento致命错误:在非对象上调用成员函数save() - Magento Fatal error: Call to a member function save() on a non-object 致命错误:在Magento中的非对象上调用成员函数getIdFieldName() - Fatal error: Call to a member function getIdFieldName() on a non-object in Magento 致命错误:在 magento 中的非对象上调用成员函数 getLevel() - Fatal error: Call to a member function getLevel() on a non-object in magento Magento:致命错误:在非对象上调用成员函数 load() - Magento: Fatal error: Call to a member function load() on a non-object 致命错误:在magento中的非对象上调用成员函数getEmail() - Fatal error: Call to a member function getEmail() on a non-object in magento Magento致命错误:在非对象上调用成员函数addFieldToFilter() - Magento Fatal error: Call to a member function addFieldToFilter() on a non-object Magento致命错误:在非对象上调用成员函数getSku() - Magento Fatal Error: Call to member function getSku() on a non-object Magento:致命错误:在非对象上调用成员函数getItems() - Magento: Fatal error: Call to a member function getItems() on a non-object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM