1

正如标题所暗示的那样,我喜欢在主页中显示从特定类别中调用的新产品,并在主页中按行分隔。

第 1 行 -> 第 1 类的新产品

第 2 行 -> 第 2 类的新产品

第 3 行 -> 第 3 类的新产品

...ETC

在我的 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.phtmlnew_category_2.phtml等等,并分别从类别 id 1 和类别 id 2 中获取“新”产品。

我玩过Mage::getModel('catalog/category')->getCollection();getProductCollectiongetCatId无法让它在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>&nbsp;</td>
            <?php endfor ?>
            </tr>
    </table>
    <script type="text/javascript">decorateTable('new-products-list-table');</script>
</div>

<?php endif; ?>

任何想法表示赞赏。

4

2 回答 2

1

下面的集合查询应该可以得到你想要的

$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('*');

一个产品的“新鲜度”由两个属性决定,news_from_date 和 news_to_date,因此您想向过滤器添加两个附加属性。从上面执行此操作的特定方法调用是

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')->            

它们直接取自新产品块

/app/code/core/Mage/Catalog/Block/Product/New.php
于 2009-09-20T08:03:07.683 回答
0

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? I'm not a coder per se, but I'm assuming an intermediary file will take care of that.

于 2009-09-20T19:58:38.847 回答