Pregunta

I need help with the following error:

Fatal error: Call to a member function getno_cart_button() on a non-object in /var/home/hifi-studio_at/www/app/design/frontend/default/theme582/template/catalog/product/compare/list.phtml on line 134

This error occurs only in the compare-list. I made an Attribute called "no_cart_button", if it is checked, the product does not show the "Cart-Button" any more, but instead a "Anfrage (Ask)" linking to a Contact-Page. On all other lists and grids it does work. But why no here? This is the code I used:

<!-- // Warenkorb in Anfrage verändern // Start // -->
                <?php   if($_product->getno_cart_button()){ ?> <div class="add-to-cart"><button type="button" title="<?php echo $this->__('Anfrage') ?>" class="button btn-cart" onClick="parent.location='/kontakt'"><span><span><?php echo $this->__('Anfrage') ?></span></span></button></div> <?php }
                                        else { ?> <p><button type="button" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Add to Cart')) ?>" class="button btn-cart" onclick="setPLocation('<?php echo $this->helper('catalog/product_compare')->getAddToCartUrl($_item) ?>', true)"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></p> <?php } ?>
                <?php else: ?>
                <!-- // Warenkorb in Anfrage verändern // ENDE // -->

This Error also occurs only with Products that have this attribute. Others don't show the cart button nor the error.

Any suggestions? Thanks!

The full code: compare/list.phtml

 ?>
    <div class="page-title title-buttons">
        <h1><?php echo $this->__('Compare Products') ?></h1>
        <a href="#" onclick="window.print(); return false;" class="link-print"><?php echo $this->__('Print This Page') ?></a>
    </div>
    <?php $_total=$this->getItems()->count() ?>
    <?php if($_total): ?>
        <table class="data-table compare-table" id="product_comparison">
            <?php $_i=0 ?>
            <?php foreach($this->getItems() as $_item): ?>
                <?php if($_i++%10==0): ?>
                    <col width="1" />
                <?php endif; ?>
                <col width="<?php echo floor(100/$_total); ?>%" />
            <?php endforeach; ?>
            <?php if ($_total>2): ?>
                <thead>
                <tr>
                    <?php $_i=0 ?>
                    <?php foreach($this->getItems() as $_item): ?>
                        <?php if($_i++%10==0): ?>
                            <th>&nbsp;</th>
                        <?php endif; ?>
                        <td class="a-right"><a href="#" class="btn-remove" onclick="removeItem('<?php echo $this->helper('catalog/product_compare')->getRemoveUrl($_item) ?>');" title="<?php echo $this->quoteEscape($this->__('Remove This Item')) ?>"><?php echo $this->__('Remove This Item') ?></a></td>
                    <?php endforeach; ?>
            </tr>
            </thead>
        <?php endif ?>
        <tbody>
        <tr class="product-shop-row top">
            <?php $_i=0 ?>
            <?php foreach($this->getItems() as $_item): ?>
                <?php if($_i++%10==0): ?>
                    <th>&nbsp;</th>
                <?php endif; ?>
                <td>
                    <a class="product-image" href="#" onclick="setPLocation('<?php echo $this->getProductUrl($_item) ?>', true)" title="<?php echo $this->stripTags($_item->getName(), null, true) ?>"><img src="<?php echo $this->helper('catalog/image')->init($_item, 'small_image')->resize(125, 125); ?>" width="125" height="125" alt="<?php echo $this->stripTags($_item->getName(), null, true) ?>" /></a>
                    <h2 class="product-name"><a href="#" onclick="setPLocation('<?php echo $this->getProductUrl($_item) ?>', true)" title="<?php echo $this->stripTags($_item->getName(), null, true) ?>"><?php echo $this->helper('catalog/output')->productAttribute($_item, $_item->getName(), 'name') ?></a></h2>
                    <?php echo $this->getReviewsSummaryHtml($_item, 'short') ?>
                    <?php echo $this->getPriceHtml($_item, true, '-compare-list-top') ?>
                </td>
            <?php endforeach; ?>
        </tr>
        <tr class="product-shop-row bottom">
            <?php $_i=0 ?>
            <?php foreach($this->getItems() as $_item): ?>
                <?php if($_i++%10==0): ?>
                    <th>&nbsp;</th>
                <?php endif; ?>
                <td>
                    <?php if($_item->isSaleable()): ?>
                        <!-- // Warenkorb in Anfrage verändern // START // -->
                        <?php   if($_product->getno_cart_button()){ ?> <p><button type="button" title="<?php echo $this->__('Anfrage') ?>" class="button btn-cart" onClick="parent.location='/kontakt'"><span><span><?php echo $this->__('Anfrage') ?></span></span></button></p> <?php }
                                else { ?> <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button> <?php } ?>
                        <?php else: ?>
<!-- // Warenkorb in Anfrage verändern // ENDE // -->
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>
                    <?php if ($this->helper('wishlist')->isAllow()) : ?>
                        <ul class="add-to-links">
                            <li><a href="<?php echo $this->getAddToWishlistUrl($_item) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
                        </ul>
                    <?php endif; ?>
                </td>
            <?php endforeach; ?>
        </tr>
        </tbody>
        <tbody>
        <?php foreach ($this->getAttributes() as $_attribute): ?>
            <tr>
                <?php $_i=0 ?>
                <?php foreach($this->getItems() as $_item): ?>
                    <?php if($_i++%10==0): ?>
                        <th><span class="nobr"><?php echo $_attribute->getStoreLabel() ?></span></th>
                    <?php endif; ?>
                    <td>
                        <?php switch ($_attribute->getAttributeCode()) {
                            case "price": ?>
                                <?php echo $this->getPriceHtml($_item, true, '-compare-list-' . $_attribute->getCode()) ?>
                                <?php break;
                            case "small_image": ?>
                                <img src="<?php echo $this->helper('catalog/image')->init($_item, 'small_image')->resize(125, 125); ?>" width="125" height="125" alt="<?php echo $this->escapeHtml($_item->getName()) ?>" title="<?php echo $this->escapeHtml($_item->getName()) ?>" />
                                <?php break;
                            case "date":
                                echo substr($this->getProductAttributeValue($_item, $_attribute),0,10);
                                break;
                            default: ?>
                                <div class="std">
                                    <?php echo $this->helper('catalog/output')->productAttribute($_item, $this->getProductAttributeValue($_item, $_attribute), $_attribute->getAttributeCode()) ?>
                                </div>
                            <?php break;
                        } ?>
                    </td>
                <?php endforeach; ?>
            </tr>
        <?php endforeach; ?>
        </tbody>
        <tbody>
        <tr class="add-to-row">
            <?php $_i=0 ?>
            <?php foreach($this->getItems() as $_item): ?>
                <?php if($_i++%10==0): ?>
                    <th>&nbsp;</th>
                <?php endif; ?>
                <td>
                    <?php echo $this->getPriceHtml($_item, true, '-compare-list-bottom') ?>
                    <!-- // Warenkorb in Anfrage verändern // START // -->
                        <?php   if($_product->getno_cart_button()){ ?> <p><button type="button" title="<?php echo $this->__('Anfrage') ?>" class="button btn-cart" onClick="parent.location='/kontakt'"><span><span><?php echo $this->__('Anfrage') ?></span></span></button></p> <?php }
                                else { ?> <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button> <?php } ?>
                        <?php else: ?>
                    <!-- // Warenkorb in Anfrage verändern // ENDE // -->
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>
                    <?php if ($this->helper('wishlist')->isAllow()) : ?>
                        <ul class="add-to-links">
                            <li><a href="<?php echo $this->getAddToWishlistUrl($_item);?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
                        </ul>
                    <?php endif; ?>
                </td>
            <?php endforeach; ?>
        </tr>
        </tbody>
    </table>
    <div class="buttons-set">
        <button type="button" title="<?php echo $this->quoteEscape($this->__('Close Window')) ?>" class="button" onclick="window.close();"><span><span><?php echo $this->__('Close Window') ?></span></span></button>
        <span class="please-wait" id="compare-list-please-wait" style="display:none;">
            <img src="<?php echo $this->getSkinUrl('images/opc-ajax-loader.gif') ?>" alt="<?php echo $this->quoteEscape($this->__('Please wait...')) ?>" title="<?php echo $this->quoteEscape($this->__('Please wait...')) ?>" class="v-middle" /> <?php echo $this->__('Please wait...') ?>
        </span>
    </div>
    <script type="text/javascript">
        decorateTable('product_comparison');

        /**
         * Send remove item request, after that reload windows
         */
        function removeItem(url)
        {
            new Ajax.Request(url, {
                parameters: {isAjax: 1, method: 'POST'},
                onLoading: function(){$('compare-list-please-wait').show();},
                onSuccess: function(transport) {
                    $('compare-list-please-wait').hide();
                    window.location.reload();
                    window.opener.location.reload();
                }
            });
        }
    </script>
<?php else: ?>
    <script type="text/javascript">window.close();</script>
<?php endif; ?>

and the code of a working list.phtml:

<?php
    $_productCollection=$this->getLoadedProductCollection();
    $_helper = $this->helper('catalog/output');
?>

<?php if(!$_productCollection->count()): ?>
    <div class="note-msg-empty-catalog">
        <h3><?php echo $this->__('There are no products matching the selection.') ?></h3>
        <?php echo $this->__('This is a custom CMS block displayed if category is empty.') ?><br />
        <?php echo $this->__('You can replace it with any custom content.') ?>

    </div>
<?php else: ?>
    <div class="category-products">
    <?php echo $this->getToolbarHtml() ?>
    <?php // List mode ?>
    <?php if($this->getMode()!='grid'): ?>
    <?php $_iterator = 0; ?>
    <ol class="products-list" id="products-list">
    <?php foreach ($_productCollection as $_product): ?>
        <li class="item<?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>">
            <?php // Product Image ?>
            <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(210); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>
            <?php // Product description ?>
            <div class="product-shop">
                <div class="f-fix">
                    <div class="list-left">
                        <?php $_productNameStripped = $this->stripTags($_product->getName(), null, true); ?>
                        <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped; ?>"><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></a></h2>
                                            <div class="desc std">
                            <?php echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?>
                            <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped ?>" class="link-learn"><?php echo $this->__('Learn More') ?></a>
                        </div>
                    </div>
                    <div class="list-right">
                        <?php echo $this->getPriceHtml($_product, true) ?>
                        <?php if($_product->getRatingSummary()): ?>
                            <?php echo $this->getReviewsSummaryHtml($_product) ?>
                        <?php endif; ?>
                        <?php if($_product->isSaleable()): ?>
                    <!-- // Warenkorb in Anfrage verändern // Start // -->
                        <?php   if($_product->getno_cart_button()){ ?> <p><button type="button" title="<?php echo $this->__('Anfrage') ?>" class="button btn-cart" onClick="parent.location='/kontakt'"><span><span><?php echo $this->__('Anfrage') ?></span></span></button></p> <?php }
                                else { ?> <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button> <?php } ?>
                        <?php else: ?>
                    <!-- // Warenkorb in Anfrage verändern // ENDE // -->
                            <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                        <?php endif; ?>
                        <ul class="add-to-links">
                            <?php if ($this->helper('wishlist')->isAllow()) : ?>
                                <li><a title="<?php echo $this->__('Add to Wishlist') ?>" href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist tooltips"><?php echo $this->__('Add to Wishlist') ?></a></li>
                            <?php endif; ?>
                            <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                                <li><span class="separator">|</span> <a title="<?php echo $this->__('Add to Compare') ?>" href="<?php echo $_compareUrl ?>" class="link-compare tooltips"><?php echo $this->__('Add to Compare') ?></a></li>
                            <?php endif; ?>
                        </ul>
                    </div>
                    <div class="clear"></div>
                </div>
            </div>
<?php $_product = Mage::getModel('catalog/product')->load($_product->getId()); ?>
            <div class="label-product">             
                <?php if($_product->getData('neu')){echo '<span class="neu">'.$this->__('neu').'</span>';  }?>
                <?php if($_product->getData('neu_ab')){echo '<span class="neu_ab">'.$this->__('neu_ab').'</span>';  }?>
                <?php if($_product->getData('sale')){echo '<span class="sale">'.$this->__('Sale').'</span>';  }?>
                <?php if($_product->getData('aktion')){echo '<span class="aktion">'.$this->__('Aktion').'</span>';  }?>
                <?php if($_product->getData('vorfuehrbereit')){echo '<span class="vorfuehrbereit">'.$this->__('vorführbereit').'</span>';  }?>
                <?php if($_product->getData('season')){echo '<span class="season">'.$this->__('season').'</span>';  }?>
            </div>

        </li>
    <?php endforeach; ?>
    </ol>
    <script type="text/javascript">decorateList('products-list', 'none-recursive')</script>

    <?php else: ?>

    <?php // Grid Mode ?>

    <?php $_collectionSize = $_productCollection->count() ?>
    <?php $_columnCount = $this->getColumnCount(); ?>
    <?php $i=0; foreach ($_productCollection as $_product): ?>
        <?php if ($i++%$_columnCount==0): ?>
        <ul class="products-grid row">
        <?php endif ?>
            <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?> col-xs-12 col-sm-4">
                <div class="wrapper-hover">
                    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image">
                 <?php $_product = Mage::getModel('catalog/product')->load($_product->getId()); ?>

                  <div class="label-product">

<!-- automatisch als NEU kennzeichnen --> <?php  $now = date("Y-m-d H:m:s");    $newFromDate = Mage::getModel('catalog/product')->load($_product->getID())->getNewsFromDate(); $newToDate = Mage::getModel('catalog/product')->load($_product->getID())->getNewsToDate();    if($newFromDate < $now && $newToDate > $now) { ?> <span class="new_product">Neu</span> <?php } ?> <!-- -->
<!-- automatisch als SALE kennzeichnen --> <?php  $now = date("Y-m-d H:m:s");    $saleFromDate = Mage::getModel('catalog/product')->load($_product->getID())->getSaleFromDate(); $saleToDate = Mage::getModel('catalog/product')->load($_product->getID())->getSaleToDate();    if($saleFromDate < $now && $saleToDate > $now) { ?> <span class="sale_product">Sale</span> <?php } ?> <!-- -->
<!-- automatisch als Aktion kennzeichnen --> <?php  $now = date("Y-m-d H:m:s");    $aktionFromDate = Mage::getModel('catalog/product')->load($_product->getID())->getAktionFromDate(); $aktionToDate = Mage::getModel('catalog/product')->load($_product->getID())->getAktionToDate();    if($aktionFromDate < $now && $aktionToDate > $now) { ?> <span class="aktion_product">Aktion</span> <?php } ?> <!-- -->

                        <?php if($_product->getData('neu')){echo '<span class="neu">'.$this->__('neu').'</span>';  }?> 
                        <?php if($_product->getData('neu_ab')){echo '<span class="neu_ab">'.$this->__('neu_ab').'</span>';  }?>
                        <?php if($_product->getData('sale')){echo '<span class="sale">'.$this->__('Sale').'</span>';  }?>
                        <?php if($_product->getData('aktion')){echo '<span class="aktion">'.$this->__('Aktion').'</span>';  }?>
                        <?php if($_product->getData('vorfuehrbereit')){echo '<span class="vorfuehrbereit">'.$this->__('vorführbereit').'</span>';  }?>
                        <?php if($_product->getData('season')){echo '<span class="season">'.$this->__('season').'</span>';  }?>
                    </div>    
                    <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(228); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>
                    <div class="product-shop">
                        <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h2>

                       <div class="desc_grid"><?php $small_getDescription = strip_tags($_product->getShortDescription());
                             if (strlen($small_getDescription) < 70) echo($small_getDescription);
                             else { echo mb_substr($small_getDescription, 0, 70,'UTF-8').'...';} ?></div>
                        <?php echo $this->getPriceHtml($_product, true) ?>
                        <?php if($_product->getRatingSummary()): ?>
                        <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                        <?php endif; ?>
                        <div class="actions">
                          <?php if($_product->isSaleable()): ?>
                        <!-- // Warenkorb in Anfrage verändern // Start // -->
                        <?php   if($_product->getno_cart_button()){ ?> <p><button type="button" title="<?php echo $this->__('Anfrage') ?>" class="button btn-cart" onClick="parent.location='/kontakt'"><span><span><?php echo $this->__('Anfrage') ?></span></span></button></p> <?php }
                                else { ?> <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button> <?php } ?>
                        <?php else: ?>
                         <!-- // Warenkorb in Anfrage verändern // ENDE // -->
                                <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                            <?php endif; ?>
                            <!-- <button type="button" title="<?php echo $this->__('Details') ?>" class="button btn-details" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Details') ?></span></span></button> -->
                            <ul class="add-to-links">
                                <?php if ($this->helper('wishlist')->isAllow()) : ?>
                                    <li><a title="<?php echo $this->__('Add to Wishlist') ?>" href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" rel="tooltip" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
                                <?php endif; ?>
                                <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                                    <li><span class="separator">|</span> <a  title="<?php echo $this->__('Add to Compare') ?> " href="<?php echo $_compareUrl ?>" rel="tooltip" class="link-compare "><?php echo $this->__('Add to Compare') ?></a></li>
                                <?php endif; ?>
                            </ul>
                        </div>
                    </div>
                    <!--  <?php $_product = Mage::getModel('catalog/product')->load($_product->getId()); ?>
                  <div class="label-product">             
                        <?php if($_product->getData('neu')){echo '<span class="neu">'.$this->__('neu').'</span>';  }?>
                        <?php if($_product->getData('neu_ab')){echo '<span class="neu_ab">'.$this->__('neu_ab').'</span>';  }?>
                        <?php if($_product->getData('sale')){echo '<span class="sale">'.$this->__('Sale').'</span>';  }?>
                        <?php if($_product->getData('aktion')){echo '<span class="aktion">'.$this->__('Aktion').'</span>';  }?>
                        <?php if($_product->getData('vorfuehrbereit')){echo '<span class="vorfuehrbereit">'.$this->__('vorführbereit').'</span>';  }?>
                    </div> --!>
                    <div class="clear"></div>
                </div>
            </li>
        <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
        </ul>
        <?php endif ?>
        <?php endforeach ?>
        <script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
    <?php endif; ?>

    <div class="toolbar-bottom">
        <?php echo $this->getToolbarHtml() ?>
    </div>
</div>
<?php endif; ?>
¿Fue útil?

Solución

While in the list.phtml the code goes like

<?php foreach ($_productCollection as $_product):
// show the data for $_product
// [...]

, the code in the compare file is like:

<?php foreach($this->getItems() as $_item):
// show the data for $_item
// $_product? I don't know what that is
// [...]

You could try to do $_item->getNoCartButton() and see if this works. If it doesn't, it means that the attribute "no_cart_button" is not loaded for this object. You then have several options:

  • first, you should do get_class($_item) to know which object you're dealing with; I don't think it's a product-object
  • the $_item should, however, have methods like getProduct() or getProductId()
  • as soon as you have the product, try to getNoCartButton() from that object
  • if it does not work, you could do a load: Mage::getModel('catalog/product')->load($productId)->getNoCartButton(). You really shouldn't do this, though, as it will crush your performance
  • you could use Mage::getModel('catalog/product')->getResource()->getRawAttributeValue($productId,'no_cart_button', $storeId) which would only add a small query to your page, but still for each product you have in compare
  • the best solution would be to check the Block-file and find the place where the product collection gets loaded. Then do
    $collection->addAttributeToSelect('no_cart_button'), which allows you to do something like $_item->getProduct()->getNoCartButton() in the
    list.phtml.

BTW, as you might have noticed, it's convention to use camel case when working with Magento. I know that getno_cart_button() works, but you should really use getNoCartButton() or getData('no_cart_button').

Otros consejos

Actually your first lines, the change from $_product to $_item an the change from No_Cart_Button to NoCartButton worked. Both changes are relevant!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange
scroll top