如何在 Magento 中每行显示特定类别的新产品?
正如标题所示,我喜欢在主页中显示从特定类别调用的新产品,这些类别之间用行分隔。
第 1 行 ->第 1 类新产品
第 2 行 ->第 2 类新产品
第 3 行 ->第 3 类新产品
...等等
在我的 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
基本上我正在考虑复制 new.phtml
并将其命名为 new_category_1.phtml
、new_category_2.phtml
等,分别从类别id 1、类别id 2中获取“新”产品。
我玩了 Mage::getModel('catalog/category')->getCollection();
、getProductCollection
和 getCatId
但不能让它在 new.phml
(app/design/frontend/default/default/template/catalog/product/
) 的副本中工作。
下面的代码可以工作,但不会加载指定类别 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; ?>
任何想法表示赞赏。
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
Row 2 -> New products from Category 2
Row 3 -> New products from Category 3
...etc
In my Magento Admin CMS, under Home Page they will be called in as separate blocks:
<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.
I played with 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/
).
The code below works but does not load "new" assigned products within category id assigned, it loads all products within it.
<?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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面的集合查询应该会得到您想要的
产品“新颖性”由两个属性决定:news_from_date 和 news_to_date,因此您需要向过滤器添加两个附加属性。上面执行此操作的具体方法调用是
直接从新产品块中获取的
Th following collection query should get you what you want
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. The specific method calls from above that do this are
They're taken directly from the New Product block at
万分感谢艾伦。您的代码仍然列出了指定类别中的所有产品,但一个微小的更改修复了它。这是任何可能感兴趣的人的最终代码。
有没有办法不重复 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.
Is there a way not to duplicate the code from new.php and still be able to apply the filters? I'm not a coder per se, but I'm assuming an intermediary file will take care of that.