Magento:显示最近添加的产品主页



我想在主页上显示我最近添加的 8 种产品。我该怎么做?

将此代码片段用于块文件表单,前面的答案将给出一个结构更好的集合,因为:

  • 按状态筛选产品 已启用
  • 按可见性筛选产品(可配置的简单产品(默认情况下为"不单独可见")不会在此集合中显示)

$_productCollection = Mage::getModel("catalog/product") ->getCollection() ->addAttributeToSelect('*') ->addAttributeToFilter('visibility', 4) ->addAttributeToFilter( 'status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED) );

<b> To show recently added products,</b>
        1. Create Block file and template file (phtml) 
        2. To show in homepage go to admin panel. Click on CMS->Pages->Homepage(Homepage will be set based on your theme). Inside that Click on Design tab and add the below code:
    <reference name="content">
        <block type="rileytheme/recentproducts" name="recentproducts_recentproducts" template="recentproducts/recentproducts.phtml"></block>
        <block type="cms/block" name="myelement"><action method="setBlockId"<block_id>homepage_block</block_id></action></block>
    </reference>

块文件:

        //class Namespace_Module_Block_Filename
        class MGS_Rileytheme_Block_Recentproducts extends Mage_Core_Block_Template { 
         public function getRecentProducts() { <br/>
            $products = Mage::getModel("catalog/product") 
                        ->getCollection()
                        ->addAttributeToSelect('*') 
                        ->setOrder('entity_id', 'DESC')
                        ->setPageSize(5); //set page size as your wish
            return $products;
          } 

查看文件 (phtml):

 <?php $products = $this->getRecentProducts(); ?>
<?php shuffle($products); ?>
<div class="container">
 <div class="box recently" style="padding-left:15px; padding-right:15px;">
  <h1 class="text-center fw400 text-red"><?php echo $this->__('Recent Products') ?></h3>
  <div class="listing-type-grid  catalog-listing">
     <?php $_collectionSize = count($products) ?>
       <?php $i=0; foreach ($products as $_res): ?>
         <?php $_product = Mage::getModel('catalog/product')->load($_res->getId()); ?>
            <?php if ($i++%3==0): ?><tr><?php endif ?>
              <div class="col-xs-12 col-sm-6 col-md-3 prod-list">
                  <div class="grid_list">
                        <div class="product">
                            <div class="image-container">
                        <?php if($_product->getProductLabel()): ?>
                            <a href="<?php echo $_product->getProductUrl() ?>">
                                <span class="onsale <?php echo strtolower($_product->getAttributeText('product_label')) ?>"><?php echo $_product->getAttributeText('product_label') ?></span>
                            </a>
                        <?php endif ?>
                            <img alt="" class="img-responsive" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(800,800); ?>">
                            <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->__('View detail')?>" class="icon-left icon-top ">
                                <i class="fa fa-eye"></i>
                            </a>
                            <a href="<?php echo $_product->getProductUrl()?>" title="<?php echo $this->__('Add to Cart')?>"  class="icon-right icon-top">
                                <i class="fa fa-shopping-cart"></i>
                            </a>
                            <?php if($this->helper('wishlist')->isAllow()): ?>
                                <!--<a class="icon-left ves-boxcolor icon-bottom" href="<?php echo $this->helper('wishlist')->getAddUrl($_product)?>" title="<?php echo $this->__('Add to wishlist')?>">
                                    <i class="fa fa-heart"></i>
                                </a>-->
                            <?php endif;?>
                            <?php if($this->getAddToCompareUrl($_product)): ?>
                            <!--<a class="icon-right boxcolor icon-bottom" href="<?php echo $this->getAddToCompareUrl($_product)?>" title="<?php echo $this->__('Add to compare')?>">
                                <i class="fa fa-retweet"></i>
                            </a>-->
                            <?php endif;?>
                    </div>
                                 <!--<div class="media-productlist">
                                    <a href="<?php // echo $_product->getProductUrl() ?>" title="<?php // echo $this->htmlEscape($_product->getName()) ?>">
                                    <img class="product-image" src="<?php // echo $this->helper('catalog/image')->init($_product, 'small_image');?>" alt="<?php // echo $this->htmlEscape($_product->getName()) ?>" />
                                    </a>
                                 </div>-->
                            <div class="product-list-data">
                                <a class="product-name" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>)"><?php echo $this->htmlEscape($_product->getName()) ?></a>
                                  <div class="product-list-data-inner">
                                     <div class="cart-item-price">
                                        <?php  $_product->getPrice();?>
                                         <span class="price"><?php echo $_formattedActualPrice = Mage::helper('core')->currency($_product->getPrice(),true,false);?></span>
                                     </div>
                                  <div class="cart-item-stars">
                                    <div class="rating-shop-item">
                                       <?php if($_product->getRatingSummary()): ?>
                                           <?php echo $this->getReviewsSummaryHtml($_product) ?>
                                        <?php else:?>
                                           <span class="star"></span>
                                           <span class="star"></span>
                                           <span class="star"></span>
                                           <span class="star"></span>
                                           <span class="star"></span>
                                        <?php endif; ?>
                                    </div>
                                    <div class="space10"></div>
                                  </div>
                  <div class="book-now-btn">
                      <button onclick="setLocation('<?php echo $_product->getProductUrl() ?>')" title="<?php echo $this->__('Book Now') ?>" type="button" class="btn btn-primary btn-sm"><?php echo $this->__('Book Now') ?></button>
                  </div>
                                 </div>
                            </div>
                        </div>
                   </div>
              </div>
              <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
              <?php if ($i%3==0 && $i!=$_collectionSize): ?></tr><?php endif ?>
        <?php endforeach ?>
       <?php for($i;$i%3!=0;$i++): ?>
          <td class="empty-product">&nbsp;</td>
        <?php endfor ?>
        <?php if ($i%3==0): ?>&nbsp;<?php endif ?>
  </div>
 </div>
</div>

最新更新