简体   繁体   中英

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. 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. That generally means a descendant of Mage_Catalog_Block_Product_Abstract although there are others. 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.

$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. I assume any product from a category will do. This does not require the block to be a catalog/product_view type.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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