2012-11-29 67 views
3

我想請求我的查詢一些幫助。致命錯誤:調用一個非對象的成員函數count()

我創建了一個CMS頁面,並希望顯示列表中特定類別的產品。所以,我創建了一個模塊,並粘貼從目錄/產品/ list.phtml的完整代碼,以便看看它是否會制定出...

不幸的是,我得到這個錯誤:Fatal error: Call to a member function count() on a non-object in /Applications/MAMP/htdocs/mysite/app/design/frontend/bootstrapped/default/template/cmsproducts/index.phtml on line 23

下面是從列表的代碼一個.phtml

<?php echo get_class($this); 
/** 
* Magento responsive theme 
* 
* @category design 
* @package  bootstrapped_default 
* @copyright Copyright (c) 2012 Magenthon (http://magenthon.com) 
* @license  http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) 
* @author  Vedran Subotic <[email protected]> 
*/ 
?> 
<?php 
/** 
* Product list template 
* 
* @see Mage_Catalog_Block_Product_List 
*/ 
?> 
<?php 
    $_productCollection=$this->getLoadedProductCollection(); 
    $_helper = $this->helper('catalog/output'); 
?> 
<?php if(!$_productCollection->count()): ?> 
<div class="alert fade in"> 
    <a class="close" data-dismiss="alert">×</a> 
    <?php echo $this->__('There are no products matching the selection.') ?> 
</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 unstyled row-fluid" id="products-list"> 
     <?php foreach ($_productCollection as $_product): ?> 
     <li class="item<?php if(++$_iterator == sizeof($_productCollection)): ?> last<?php endif; ?> row-fluid"> 
      <?php // Product Image ?> 
      <div class="span4"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image thumbnail"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(225); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a></div> 
      <?php // Product description ?> 
      <div class="span8"> 
       <div class="thumbnail data"> 
        <div class="caption"> 
        <?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> 
        <?php if($_product->getRatingSummary()): ?> 
         <?php echo $this->getReviewsSummaryHtml($_product) ?> 
        <?php endif; ?> 
        <div class="row-fluid"> 
         <span class="pull-right"> 
          <?php if($_product->isSaleable()): ?> 
           <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="btn btn-danger" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button> 
          <?php else: ?> 
           <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p> 
          <?php endif; ?> 
         </span> 
         <?php echo $this->getPriceHtml($_product, true) ?> 
        </div> 
        <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> 
        <p class="add-to-links unstyled"> 
         <?php if ($this->helper('wishlist')->isAllow()) : ?> 
         <a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist label label-info"><?php echo $this->__('Add to Wishlist') ?></a> 
         <?php endif; ?> 
         <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?> 
         <span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare label label-info"><?php echo $this->__('Add to Compare') ?></a> 
         <?php endif; ?> 
        </p> 
        </div> 
       </div> 
      </div> 
     </li><hr> 
     <?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 unstyled thumbnails row-fluid"> 
      <?php endif ?> 
      <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?> span4"> 
       <div class="thumbnail"> 
       <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image thumbnail"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(225); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a> 

       <div class="caption"> 
        <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> 
        <?php if($_product->getRatingSummary()): ?> 
        <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?> 
        <?php endif; ?> 
        <?php echo $this->getPriceHtml($_product, true) ?> 
        <?php if($_product->isSaleable()): ?> 
         <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="btn btn-danger btn-mini" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button> 
        <?php else: ?> 
         <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p> 
        <?php endif; ?> 
        <p class="add-to-links unstyled"> 
         <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?> 
         <a href="<?php echo $_compareUrl ?>" class="link-compare label label-info pull-right"><?php echo $this->__('Add to Compare') ?></a> 
         <?php endif; ?> 
         <?php if ($this->helper('wishlist')->isAllow()) : ?> 
          <a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist label label-info"><?php echo $this->__('Add to Wishlist') ?></a> 
         <?php endif; ?> 

        </p> 
       </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 
     /* @var $toolbar Mage_Catalog_Block_Product_List_Toolbar */ 
     // get toolbars limits - overrides default pager limit 
     $toolbar = Mage::getBlockSingleton('catalog/product_list_toolbar')->getAvailableLimit(); 

     /* @var $pager Mage_Page_Block_Html_Pager */ 
     $pager = $this->getLayout()->createBlock('page/html_pager', 'bootstrapped.standalone.pager'); 

     // set limit - uses category's instead of default pager's (10-20-50) 
     $pager->setAvailableLimit($toolbar); 
     // need to set collection 
     $pager->setCollection($_productCollection); 
     //set custom template 
     $pager->setTemplate('catalog/product/list/pager.phtml'); 
     echo $pager->toHtml(); 
     ?> 
    </div> 
</div> 
<?php endif; ?> 

而對於我的CMS塊:

{{block type="core/template" category_id="41" template="cmsproducts/index.phtml"}} 

indexController.php爲我的自定義模塊:

<?php 
class Thaneuk_Cmsproducts_IndexController extends Mage_Core_Controller_Front_Action{ 
    public function IndexAction() { 
    } 
} 

我搜索了各種方法將產品從類別添加到CMS頁面,但每種方法都會導致錯誤或產品不顯示。

任何人都可以幫助或引薦我一步一步指導工程?

在此先感謝

+0

發佈一些代碼.. –

+1

'$ _productCollection'是不是一個對象,這意味着'$這個 - > getLoadedProductCollection()'返回'null'或'false'。 – Tchoupi

回答

0

簡單的修復,使其不會拋出錯誤會

<?php if(is_object($_productCollection) && !$_productCollection->count()): ?> 

但你仍然需要弄清楚在你的代碼中的漏洞,爲什麼$_productCollection是沒有得到加載。

+0

這是非常令人費解的,因爲我已經從list.phtml中複製了加載在主站點中的代碼,但是當我將它用作CMS頁面中模塊的模板時... – user1704524

+0

是的錯誤不再與你的代碼,我可以看到頁面上的塊類echo get_class($ this);但沒有產品 – user1704524

+0

最有可能你正在調用塊無法訪問的方法... –

0

解決了它!

<?php 
$category_id = "43"; // category_id for "Featured Products" 
$_productCollection = Mage::getResourceModel('catalog/product_collection') 
->addAttributeToSelect(array('name', 'price', 'small_image', 'short_description'), 'inner') 
->addCategoryFilter(Mage::getModel('catalog/category')->load($category_id)); 
?> 
<?php if($_productCollection->count()): ?> 
     <div class="featured"> 
     <br /> 
     <ul id="featured-product-list"> 
     <div class="block-title"> 
     <h3><span><?php echo $this->__('FEATURED PRODUCT'); ?></span></h3> 
    </div> 
     <?php 
     $products = array(); 
     foreach ($_productCollection as $_product) { 
     ?> 
     <li class="featured-product"> 
      <!--<h6>Price <?php echo Mage::helper('core')->currency($_product->getPrice());; ?></h6>--> 
      <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>"> 
      <img class="product-image" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(225, 200); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" /> 
      </a> 
     </li> 
     <strong><a class="view-item-button" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->__('Order'); ?> <?php echo $this->htmlEscape($_product->getName())?><?php echo $this->__('&#8482;'); ?></a></strong> 
     <div class="clearfix"></div> 
     <p> 
      <?php echo $_product->_data['short_description']; ?> 
     </p> 
     <div class="clearfix"></div> 
     <?php } ?> 
     </ul> 
</div> 
<?php endif; ?> 
+0

@Gershon Herczeg雖然^上述代碼意味着我必須爲每個產品創建一個模板文件,我希望在CMS頁面內顯示! – user1704524

+0

@Gershon Herczeg我試着從模板文件中刪除類別ID,並在塊代碼中指定它,但它加載了每個類別的每個產品...我會繼續調整,直到我找到它的權利 – user1704524

-1

同樣的錯誤發生在我身上。我在XAMPP服務器上使用了magento 1.9.1。 我只刷新所有magento索引,並且所有工作正常。

去系統/索引管理 選擇所有的選項並執行「重新索引數據」行動

相關問題