如何在Magento中设置要在主页上显示的产品数量

时间:2013-12-11 06:28:32

标签: php magento

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

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>

2 个答案:

答案 0 :(得分:0)

有两种方法可以控制产品的编号。

1如果您使用的是通用magento loadedcollection,您可以更改

中的值
System -> Configuration -> Catalog -> Frontend tab

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

您可以添加setize和setpage方法来限制收集ex:

$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>

我希望上面的任何一个可以帮助你。

答案 1 :(得分: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" }}