簡體   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