2013-12-11 37 views
0

我有一个new.phtml文件和new.php文件,我确信主页上显示的产品仅来自这些文件。目前只有6种产品正在展出。我只是想知道如何能够显示更多的产品(应增加3的乘法)像9,12,15等 我试图在两个文件中替换所有'6'到'9',但我无法得到我要找的东西?请任何人都帮助我,给我一些技巧! 这里是new.php文件代码:如何设置Magento主页上显示的产品数

class Mage_Catalog_Block_Pro duct_New extends Mage_Catalog_Block_Product_Abstract 
{ 
protected $_productsCount = null; 

const DEFAULT_PRODUCTS_COUNT = 5; 

/** 
* Initialize block's cache 
*/ 
protected function _construct() 
{ 
    parent::_construct(); 

    $this->addColumnCountLayoutDepend('empty', 10) 
     ->addColumnCountLayoutDepend('one_column', 5) 
     ->addColumnCountLayoutDepend('two_columns_left', 4) 
     ->addColumnCountLayoutDepend('two_columns_right', 4) 
     ->addColumnCountLayoutDepend('three_columns', 3); 

    $this->addData(array(
     'cache_lifetime' => 86400, 
     'cache_tags'  => array(Mage_Catalog_Model_Product::CACHE_TAG), 
    )); 
} 

/** 
* Get Key pieces for caching block content 
* 
* @return array 
*/ 
public function getCacheKeyInfo() 
{ 
    return array(
     'CATALOG_PRODUCT_NEW', 
     Mage::app()->getStore()->getId(), 
     Mage::getDesign()->getPackageName(), 
     Mage::getDesign()->getTheme('template'), 
     Mage::getSingleton('customer/session')->getCustomerGroupId(), 
     'template' => $this->getTemplate(), 
     $this->getProductsCount() 
    ); 
} 

/** 
* Prepare collection with new products and applied page limits. 
* 
* return Mage_Catalog_Block_Product_New 
*/ 
protected function _beforeToHtml() 
{ 
    $todayStartOfDayDate = Mage::app()->getLocale()->date() 
     ->setTime('00:00:00') 
     ->toString(Varien_Date::DATETIME_INTERNAL_FORMAT); 

    $todayEndOfDayDate = Mage::app()->getLocale()->date() 
     ->setTime('23:59:59') 
     ->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('or'=> array(
      0 => array('date' => true, 'to' => $todayEndOfDayDate), 
      1 => array('is' => new Zend_Db_Expr('null'))) 
     ), 'left') 
     ->addAttributeToFilter('news_to_date', array('or'=> array(
      0 => array('date' => true, 'from' => $todayStartOfDayDate), 
      1 => array('is' => new Zend_Db_Expr('null'))) 
     ), 'left') 
     ->addAttributeToFilter(
      array(
       array('attribute' => 'news_from_date', 'is'=>new Zend_Db_Expr('not null')), 
       array('attribute' => 'news_to_date', 'is'=>new Zend_Db_Expr('not null')) 
       ) 
     ) 
     ->addAttributeToSort('news_from_date', 'desc') 
     ->setPageSize(9) 
     ->setCurPage(1) 
    ; 

    $this->setProductCollection($collection); 

    return parent::_beforeToHtml(); 
} 

/** 
* Set how much product should be displayed at once. 
* 
* @param $count 
* @return Mage_Catalog_Block_Product_New 
*/ 
public function setProductsCount($count) 
{ 
    $this->_productsCount = $count; 
    return $this; 
} 

/** 
* Get how much products should be displayed at once. 
* 
* @return int 
*/ 
public function getProductsCount() 
{ 
    if (null === $this->_productsCount) { 
     $this->_productsCount = self::DEFAULT_PRODUCTS_COUNT; 
    } 
    return $this->_productsCount; 
} 

}

Here is my new.phtml file: 

<?php $_helper = $this->helper('catalog/output'); ?> 
<?php if (($_products = $this->getProductCollection()) && $_products->getSize()): ?> 
<?php $_columnCount = 3 ?> 
<div class="block-new-top"><strong class="cat-tit"><?php echo $this->__('New Products') ?></strong></div> 
    <div class="block-new"> 
<div class="clear"></div> 
<div class="block-content"> 
    <?php $m=1; ?> 
    <?php $i=0; foreach ($_products->getItems() as $_product): ?> 
     <?php if ($i++%$_columnCount==0 ): ?> 
      <ul class="products-grid" id="proid<?php echo $m; ?>"> 
       <?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($_product->getName()) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(160) ?>" width="160" height="160" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" /></a> 
       <div class="align-prodname-price-review"> 
         <h3 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h3> 

         <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?> 
       </div> 

       <div class="new-des"> 

       <?php 

           $position=50; // Define how many character you want to display. 

           $message= $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description'); 
           $post = substr($message, 0, $position); 

           echo $post; 
           echo "..."; 

           ?> 


       </div> 

       <div class="actions"> 
       <div class="pri"><?php echo $this->getPriceHtml($_product, true, '-new') ?></div> 
       <div class="view-more"> 
        <?php if($_product->isSaleable()): ?> 
         <div class="detail-container"><a href="<?php echo $_product->getProductUrl() ?>"> View More>> </a></div> 
        <?php else: ?> 
         <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p> 
        <?php endif; ?> 
       </div> 

       </div> 
      </li> 
     <?php if ($i%$_columnCount==0 || $i==count($_products)): ?> 
      </ul> 
     <?php endif ?> 
     <?php $m++; ?> 
    <?php endforeach; ?> 
</div> 
<?php endif; ?> 
</div> 
<div class="block-new-bottom"></div> 
+0

正在使用的产品loadedcollection这些文件,或不得不编写自定义代码来获取产品? –

+0

我认为它是一个自定义代码 –

+0

您是指在magento样本数据中使用的最畅销产品? –

回答

0

有些情况下,你可以控制的没有产品的两个三种方式。

1如果你使用的是通用的Magento loadedcollection你可以从

System -> Configuration -> Catalog -> Frontend tab 

2如果您正在使用自定义代码来获取产品

您可以添加的setSize和setpage方法来限制更改值集合例如:

$collection = Mage::getModel('...') 
     ->getCollection() 
     ->setPageSize(20) 
     ->setCurPage(1); 

OR

$collection = Mage::getModel('...')->getCollection(); 
$collection->getSelect()->limit(20); 

3根据您所使用的页面的方式,您还可以设置产品限价从布局XML 例如:

<block type="catalog/product_list" name="bestsellers"> 
    <action method="setLimit"><value>3</value></action> 
</block> 

我希望任何来自上面的帮助你。

+0

感谢您的帮助!在cms主页我可以看到{{block type =“catalog/product_new”name =“catalog.new”template =“catalog/product/new.phtml”_productsCount =“6”}}如果我将此值从6更改为9甚至没有改变。 –

+0

尝试清除一次缓存 –

+0

如果您只使用通用magento产品集合,也可以在上面更改。您是否可以复制您的phtml文件的代码? –

0

尝试下面的代码来设置的产品数量在Magento主页计数

{{block type="catalog/product_new" category_id="3" products_count="9" name="home.catalog.product.new" alias="product_homepage" template="catalog/product/new.phtml" }} 
+0

我试过但没有变化,仍然可以看到相同的6种产品! –

+0

清除你的magento var/cache并检查 –

+0

我已经做了,但没有任何改变 –

相关问题