如何在 Magento 中每行显示特定类别的新产品?

发布于 2024-08-05 00:13:44 字数 4615 浏览 3 评论 0原文

正如标题所示,我喜欢在主页中显示从特定类别调用的新产品,这些类别之间用行分隔。

第 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.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; ?>

任何想法表示赞赏。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∞觅青森が 2024-08-12 00:13:44

下面的集合查询应该会得到您想要的

$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

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. 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
ぇ气 2024-08-12 00:13:44

万分感谢艾伦。您的代码仍然列出了指定类别中的所有产品,但一个微小的更改修复了它。这是任何可能感兴趣的人的最终代码。

$_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()): ?> 

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文