[英]How can I show NEW products per row from specific categories in Magento?
As the title suggests, I like to show new products called in from specific categories separated by rows in the home page. 就像标题所暗示的那样,我希望显示在首页中按行分隔的特定类别中调用的新产品。
Row 1 -> New products from Category 1
第1行->第1类的新产品
Row 2 -> New products from Category 2
第2行->第2类的新产品
Row 3 -> New products from Category 3
第3行->第3类的新产品
...etc
...等等
In my Magento Admin CMS, under Home Page they will be called in as separate blocks: 在我的Magento Admin CMS的“主页”下,它们将作为单独的块被调用:
<block type="catalog/product_new" name="home.catalog.product.new_category_1" alias="product_new_category_1" template="catalog/product/new_category_1.phtml">
<block type="catalog/product_new" name="home.catalog.product.new_category_2" alias="product_new_category_2" template="catalog/product/new_category_2.phtml">
<block type="catalog/product_new" name="home.catalog.product.new_category_3" alias="product_new_category_3" template="catalog/product/new_category_3.phtml">
...etc
Basically I'm thinking of duplicating new.phtml
and calling it new_category_1.phtml
, new_category_2.phtml
, etc and get “new” products from category id 1, category id 2 respectively. 基本上,我正在考虑复制
new.phtml
并将其new_category_1.phtml
, new_category_2.phtml
等,并分别从类别ID 1,类别ID 2获取“新”产品。
I played with Mage::getModel('catalog/category')->getCollection();
我玩过
Mage::getModel('catalog/category')->getCollection();
, getProductCollection
and getCatId
and can't get it working in a copy of new.phml
( app/design/frontend/default/default/template/catalog/product/
). ,
getProductCollection
和getCatId
,并且无法在new.phml
的副本( app/design/frontend/default/default/template/catalog/product/
)中使用它。
The code below works but does not load "new" assigned products within category id assigned, it loads all products within it. 下面的代码有效,但是不会在分配的类别ID中加载“新”分配的产品,而是在其中加载所有产品。
<?php
$cat_id = 46; // category id
$category = Mage::getModel('catalog/category')->load($cat_id);
$products = $category->getProductCollection()->addCategoryFilter($category)->addAttributeToSelect('*');
if (($products=($_products = $this->getProductCollection()) && $_products->getSize())): ?>
<div class="hp-report">
<div class="head-alt">
<h2 class="title"><?php echo $this->__('New Products') ?></h2>
</div>
<table cellspacing="0" class="generic-product-grid" id="new-products-list-table">
<tr>
<?php $i=0; foreach ($_products->getItems() as $_product): ?>
<?php if ($i>=4): continue; endif; ?>
<td>
<p class="product-image">
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(170) ?>" width="170" height="170" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" /></a>
</p>
<p><a class="product-name" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>)"><?php echo $this->htmlEscape($_product->getName()) ?></a></p>
<?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
<?php echo $this->getPriceHtml($_product, true, '-new') ?>
<?php if($_product->isSaleable()): ?>
<a href="<?php echo $this->getAddToCartUrl($_product) ?>"><img src="<?php echo $this->getSkinUrl('images/btn_add_to_cart.gif') ?>" alt="<?php echo $this->__('Add to Cart') ?>" title="<?php echo $this->__('Add to Cart') ?>" /></a>
<?php else: ?>
<div class="out-of-stock"><?php echo $this->__('Out of stock') ?></div>
<?php endif; ?>
<div class="clear"></div>
<ul class="add-to">
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<li><a href="<?php echo $this->getAddToWishlistUrl($_product) ?>" class="link-cart"><?php echo $this->__('Add to Wishlist') ?></a></li>
<?php endif; ?>
<?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
<li><a href="<?php echo $_compareUrl ?>"><?php echo $this->__('Add to Compare') ?></a></li>
<?php endif; ?>
</ul>
</td>
<?php $i++; endforeach; ?>
<?php for($i;$i%4!=0;$i++): ?>
<td> </td>
<?php endfor ?>
</tr>
</table>
<script type="text/javascript">decorateTable('new-products-list-table');</script>
</div>
<?php endif; ?>
Any thoughts appreciated. 任何想法表示赞赏。
Th following collection query should get you what you want 以下收集查询应为您提供所需的信息
$todayDate = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$products = $category->
getProductCollection()->
addCategoryFilter($category)->
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')->
addAttributeToSelect('*');
A products "newness" is determined by two attributes, news_from_date and news_to_date, so you want to add two additional attributes to the filter. 产品的“新颖性”取决于两个属性,即news_from_date和news_to_date,因此您要向过滤器添加两个附加属性。 The specific method calls from above that do this are
从上面执行此操作的特定方法是
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')->
They're taken directly from the New Product block at 它们直接取自位于
/app/code/core/Mage/Catalog/Block/Product/New.php
Million thanks Alan. 万分感谢艾伦。 Your code was still listing all the products within the assigned category, but a minor change fixed it.
您的代码仍在列出分配的类别中的所有产品,但是进行了较小的更改。 Here's the final code to anyone who might be interested.
这是任何有兴趣的人的最终代码。
$_products = $category->
getProductCollection()->
addCategoryFilter($category)->
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')->
addAttributeToSelect('*');
if (($this->getProductCollection()) && $_products->getSize()): ?>
Is there a way not to duplicate the code from new.php and still be able to apply the filters? 有没有办法不复制来自new.php的代码,仍然能够应用过滤器? I'm not a coder per se, but I'm assuming an intermediary file will take care of that.
我本身不是编码人员,但是我假设中介文件可以解决这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.