2013-12-11 63 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

我已經做了,但沒有任何改變 –

相關問題