2013-05-16 18 views
2

Magento的1.7.0.2

我试图下面的代码编辑: http://www.dnawebagency.com/displaying-new-products-in-magento-with-pagination

我有一直在与我设置的服务器上进行斗争,希望尽快上线,但我无法实现它。我收到以下输出:

致命错误:调用/app/design/frontend/default/cscolors/template/catalog/product/list.phtml行中的非对象的成员函数count() 35 35

行写着:

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

我能得到这个在一个干净的Magento的有几样产品的安装工作,但由于某种原因,我不能让他工作了甚至按照相同的程序。

我不知道从哪里开始调试这种情况,并找出为什么它挂在那行代码,所以任何建议或想法什么看待将不胜感激。在这个服务器上有更多的代码编辑,但他们应该远离这个问题。两个服务器之间的唯一区别实质上是几个Magento Connect附加组件以及当前主题中包含的一些额外功能和模板代码编辑。

我尝试过使用其他主题,所以某些地方出现了错误的New.php文件,但它是我在新的测试服务器上工作的一个确切副本,所以我很困惑到哪里去从这里。 (据我所知)中仅有的两个文件是上面链接中提到的两个文件,/ app/code/local/Mage/Catalog/Block/Product中的New.php,以及/应用程序/设计/前端/默认/ currentthemedir /模板/目录/产品。 (这在技术上也是无关紧要的,因为我使用默认的list.phtml文件得到相同的错误)

希望我充分描述了这一点,如果需要,我可以提供更多代码,但如前所述,我直接从上面的链接。

以下是一些完整的文件。

/app/design/frontend/default/themedir/template/catalog/product/list.phtml:

<?php 
/** 
* Magento 
* 
* NOTICE OF LICENSE 
* 
* This source file is subject to the Academic Free License (AFL 3.0) 
* that is bundled with this package in the file LICENSE_AFL.txt. 
* It is also available through the world-wide-web at this URL: 
* http://opensource.org/licenses/afl-3.0.php 
* If you did not receive a copy of the license and are unable to 
* obtain it through the world-wide-web, please send an email 
* to [email protected] so we can send you a copy immediately. 
* 
* DISCLAIMER 
* 
* Do not edit or add to this file if you wish to upgrade Magento to newer 
* versions in the future. If you wish to customize Magento for your 
* needs please refer to http://www.magentocommerce.com for more information. 
* 
* @category design 
* @package  base_default 
* @copyright Copyright (c) 2009 Irubin Consulting Inc. DBA Varien (http://www.varien.com) 
* @license  http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) 
*/ 
?> 
<?php 
/** 
* Product list template 
* 
* @see Mage_Catalog_Block_Product_List 
*/ 
?> 
<?php 
    $_productCollection=$this->getLoadedProductCollection(); 
    echo($_productCollection); 
    $_helper = $this->helper('catalog/output'); 
?> 
<?php if(!$_productCollection->count()): ?> 
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p> 
<?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->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(160,135); ?>" width="160" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" /></a> 
      <?php // Product description ?> 
      <div class="product-shop"> 
       <div class="f-fix"> 
        <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName())?></a></h2> 
        <?php if($_product->getRatingSummary()): ?> 
        <?php echo $this->getReviewsSummaryHtml($_product) ?> 
        <?php endif; ?> 
        <?php echo $this->getPriceHtml($_product, true) ?> 
        <?php if($_product->isSaleable()): ?> 
         <p><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></p> 
        <?php else: ?> 
         <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p> 
        <?php endif; ?> 
        <div class="desc std"> 
         <?php echo $_product->getShortDescription() ?> 
         <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>" class="link-more"><?php echo $this->__('Learn More') ?></a> 
        </div> 
        <?php if ($this->helper('wishlist')->isAllow() && $this->getAddToCompareUrl($_product)) : ?> 
        <ul class="add-to-links"> 
         <?php if ($this->helper('wishlist')->isAllow()) : ?> 
          <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" 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 href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare') ?></a></li> 
         <?php endif; ?>--> 
        </ul> 
        <?php endif; ?> 
       </div> 
      </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"> 
     <?php endif ?> 
      <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>"> 
       <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135, 135); ?>" width="135" height="135" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" /></a> 
       <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h2> 
       <?php if($_product->getRatingSummary()): ?> 
       <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?> 
       <?php endif; ?> 
       <?php echo $this->getPriceHtml($_product, true) ?> 

       <div class="actions"> 
        <?php if($_product->isSaleable()): ?> 
         <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 else: ?> 
         <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p> 
        <?php endif; ?> 
        <?php if ($this->helper('wishlist')->isAllow() && $this->getAddToCompareUrl($_product)) : ?> 
        <ul class="add-to-links"> 
         <?php if ($this->helper('wishlist')->isAllow()) : ?> 
          <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" 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 href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare') ?></a></li> 
         <?php endif; ?>--> 
        </ul> 
        <?php endif; ?> 
       </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; ?> 

/应用/代码/本地/法师/目录/座/产品/ New.php

<?php 
/** 
* Magento 
* 
* NOTICE OF LICENSE 
* 
* This source file is subject to the Open Software License (OSL 3.0) 
* that is bundled with this package in the file LICENSE.txt. 
* It is also available through the world-wide-web at this URL: 
* http://opensource.org/licenses/osl-3.0.php 
* If you did not receive a copy of the license and are unable to 
* obtain it through the world-wide-web, please send an email 
* to [email protected] so we can send you a copy immediately. 
* 
* DISCLAIMER 
* 
* Do not edit or add to this file if you wish to upgrade Magento to newer 
* versions in the future. If you wish to customize Magento for your 
* needs please refer to http://www.magentocommerce.com for more information. 
* 
* @category Mage 
* @package  Mage_Catalog 
* @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com) 
* @license  http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 
*/ 

/** 
* New products block 
* 
* @category Mage 
* @package Mage_Catalog 
* @author  Magento Core Team <[email protected]> 
*/ 
//Code 

class Mage_Catalog_Block_Product_New extends Mage_Catalog_Block_Product_List 
{ 
    function get_prod_count() 
    { 
     //unset any saved limits 
     Mage::getSingleton('catalog/session')->unsLimitPage(); 
     return (isset($_REQUEST['limit'])) ? intval($_REQUEST['limit']) : 12; 
    }// get_prod_count 

    function get_cur_page() 
    { 
     return (isset($_REQUEST['p'])) ? intval($_REQUEST['p']) : 1; 
    }// get_cur_page 

    /** 
    * Retrieve loaded category collection 
    * 
    * @return Mage_Eav_Model_Entity_Collection_Abstract 
    **/ 
    protected function _getProductCollection() 
    { 
     $todayDate = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT); 

     $collection = Mage::getResourceModel('catalog/product_collection'); 
     $collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds()); 

     $collection = $this->_addProductAttributesAndPrices($collection) 
     ->addStoreFilter() 
     ->addAttributeToFilter('news_from_date', array('date' => true, 'to' => $todayDate)) 
     ->addAttributeToFilter('news_to_date', array('or'=> array(
      0 => array('date' => true, 'from' => $todayDate), 
      1 => array('is' => new Zend_Db_Expr('null'))) 
     ), 'left') 
     ->addAttributeToSort('news_from_date', 'desc') 
     ->setPageSize($this->get_prod_count()) 
     ->setCurPage($this->get_cur_page()); 

     $this->setProductCollection($collection); 

     return $collection; 
    }// _getProductCollection 
}// Mage_Catalog_Block_Product_New 
?> 

/app/code/local/Mage/Catalog/Block/Product/List.php

<?php 
/** 
* Magento 
* 
* NOTICE OF LICENSE 
* 
* This source file is subject to the Open Software License (OSL 3.0) 
* that is bundled with this package in the file LICENSE.txt. 
* It is also available through the world-wide-web at this URL: 
* http://opensource.org/licenses/osl-3.0.php 
* If you did not receive a copy of the license and are unable to 
* obtain it through the world-wide-web, please send an email 
* to [email protected] so we can send you a copy immediately. 
* 
* DISCLAIMER 
* 
* Do not edit or add to this file if you wish to upgrade Magento to newer 
* versions in the future. If you wish to customize Magento for your 
* needs please refer to http://www.magentocommerce.com for more information. 
* 
* @category Mage 
* @package  Mage_Catalog 
* @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com) 
* @license  http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 
*/ 


/** 
* Product list 
* 
* @category Mage 
* @package Mage_Catalog 
* @author  Magento Core Team <[email protected]> 
*/ 
class Mage_Catalog_Block_Product_List extends Mage_Catalog_Block_Product_Abstract 
{ 
    /** 
    * Default toolbar block name 
    * 
    * @var string 
    */ 
    protected $_defaultToolbarBlock = 'catalog/product_list_toolbar'; 

    /** 
    * Product Collection 
    * 
    * @var Mage_Eav_Model_Entity_Collection_Abstract 
    */ 
    protected $_productCollection; 

    /** 
    * Retrieve loaded category collection 
    * 
    * @return Mage_Eav_Model_Entity_Collection_Abstract 
    */ 
    protected function _getProductCollection() 
    { 
     if (is_null($this->_productCollection)) { 
      $layer = $this->getLayer(); 
      /* @var $layer Mage_Catalog_Model_Layer */ 
      if ($this->getShowRootCategory()) { 
       $this->setCategoryId(Mage::app()->getStore()->getRootCategoryId()); 
      } 

      // if this is a product view page 
      if (Mage::registry('product')) { 
       // get collection of categories this product is associated with 
       $categories = Mage::registry('product')->getCategoryCollection() 
        ->setPage(1, 1) 
        ->load(); 
       // if the product is associated with any category 
       if ($categories->count()) { 
        // show products from this category 
        $this->setCategoryId(current($categories->getIterator())); 
       } 
      } 

      $origCategory = null; 
      if ($this->getCategoryId()) { 
       $category = Mage::getModel('catalog/category')->load($this->getCategoryId()); 
       if ($category->getId()) { 
        $origCategory = $layer->getCurrentCategory(); 
        $layer->setCurrentCategory($category); 
       } 
      } 
      $this->_productCollection = $layer->getProductCollection(); 

      $this->prepareSortableFieldsByCategory($layer->getCurrentCategory()); 

      if ($origCategory) { 
       $layer->setCurrentCategory($origCategory); 
      } 
     } 

     return $this->_productCollection; 
    } 

    /** 
    * Get catalog layer model 
    * 
    * @return Mage_Catalog_Model_Layer 
    */ 
    public function getLayer() 
    { 
     $layer = Mage::registry('current_layer'); 
     if ($layer) { 
      return $layer; 
     } 
     return Mage::getSingleton('catalog/layer'); 
    } 

    /** 
    * Retrieve loaded category collection 
    * 
    * @return Mage_Eav_Model_Entity_Collection_Abstract 
    */ 
    public function getLoadedProductCollection() 
    { 
     return $this->_getProductCollection(); 
    } 

    /** 
    * Retrieve current view mode 
    * 
    * @return string 
    */ 
    public function getMode() 
    { 
     return $this->getChild('toolbar')->getCurrentMode(); 
    } 

    /** 
    * Need use as _prepareLayout - but problem in declaring collection from 
    * another block (was problem with search result) 
    */ 
    protected function _beforeToHtml() 
    { 
     $toolbar = $this->getToolbarBlock(); 

     // called prepare sortable parameters 
     $collection = $this->_getProductCollection(); 

     // use sortable parameters 
     if ($orders = $this->getAvailableOrders()) { 
      $toolbar->setAvailableOrders($orders); 
     } 
     if ($sort = $this->getSortBy()) { 
      $toolbar->setDefaultOrder($sort); 
     } 
     if ($dir = $this->getDefaultDirection()) { 
      $toolbar->setDefaultDirection($dir); 
     } 
     if ($modes = $this->getModes()) { 
      $toolbar->setModes($modes); 
     } 

     // set collection to toolbar and apply sort 
     $toolbar->setCollection($collection); 

     $this->setChild('toolbar', $toolbar); 
     Mage::dispatchEvent('catalog_block_product_list_collection', array(
      'collection' => $this->_getProductCollection() 
     )); 

     $this->_getProductCollection()->load(); 

     return parent::_beforeToHtml(); 
    } 

    /** 
    * Retrieve Toolbar block 
    * 
    * @return Mage_Catalog_Block_Product_List_Toolbar 
    */ 
    public function getToolbarBlock() 
    { 
     if ($blockName = $this->getToolbarBlockName()) { 
      if ($block = $this->getLayout()->getBlock($blockName)) { 
       return $block; 
      } 
     } 
     $block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, microtime()); 
     return $block; 
    } 

    /** 
    * Retrieve additional blocks html 
    * 
    * @return string 
    */ 
    public function getAdditionalHtml() 
    { 
     return $this->getChildHtml('additional'); 
    } 

    /** 
    * Retrieve list toolbar HTML 
    * 
    * @return string 
    */ 
    public function getToolbarHtml() 
    { 
     return $this->getChildHtml('toolbar'); 
    } 

    public function setCollection($collection) 
    { 
     $this->_productCollection = $collection; 
     return $this; 
    } 

    public function addAttribute($code) 
    { 
     $this->_getProductCollection()->addAttributeToSelect($code); 
     return $this; 
    } 

    public function getPriceBlockTemplate() 
    { 
     return $this->_getData('price_block_template'); 
    } 

    /** 
    * Retrieve Catalog Config object 
    * 
    * @return Mage_Catalog_Model_Config 
    */ 
    protected function _getConfig() 
    { 
     return Mage::getSingleton('catalog/config'); 
    } 

    /** 
    * Prepare Sort By fields from Category Data 
    * 
    * @param Mage_Catalog_Model_Category $category 
    * @return Mage_Catalog_Block_Product_List 
    */ 
    public function prepareSortableFieldsByCategory($category) { 
     if (!$this->getAvailableOrders()) { 
      $this->setAvailableOrders($category->getAvailableSortByOptions()); 
     } 
     $availableOrders = $this->getAvailableOrders(); 
     if (!$this->getSortBy()) { 
      if ($categorySortBy = $category->getDefaultSortBy()) { 
       if (!$availableOrders) { 
        $availableOrders = $this->_getConfig()->getAttributeUsedForSortByArray(); 
       } 
       if (isset($availableOrders[$categorySortBy])) { 
        $this->setSortBy($categorySortBy); 
       } 
      } 
     } 

     return $this; 
    } 
} 

回答

0

我发现这个问题,这是一个愚蠢的问题。感谢大家帮我锁定这个问题。

将整个生产服务器复制到我的文件到文件后,错误显示在我的服务器上。我注意到用所有核心magento文件覆盖它解决了这个问题。

我对所有文件夹进行了差异比较,没有发现很多差异,所以我开始逐个替换文件夹以缩小范围。当我到达“/ includes /”时它开始工作。不同之处在于config.php文件。这是一个只有几行文本的小文件,不同之处在于它是一个非注释行,指的是magento编译器。

我忘记了我已经启用并运行生产服务器上的编译器,编辑模板文件并没有给我这样的问题。然而,函数代码是一个不同的故事。重新运行编译器解决了我的问题,并且代码现在按预期工作。

2

致命错误:调用在/app/design/frontend/default/cscolors/template/catalog/product/list.phtml成员函数计数()非对象上线路35上

和你的行是count()):?>

$ _productCollection是一个非对象,所以你需要检查为什么没有创建一个实例。

+0

'$ _productCollection-> count()'是magento方法,用于检索产品数量在列表页上 – Mufaddal

+0

@Satish:对不起,这是错误的。非对象错误意味着根本没有类的实例,而是$ _productCollection === null。 –

+0

好的,我纠正了我的回复更具体。 – oscprofessionals

相关问题