简体   繁体   English

Magento 1.7展示2类产品

[英]Magento 1.7 show products from 2 categories in block

I wanted to create a block that would display products from 2 (or more) categories. 我想创建一个可以显示2个(或更多)类别产品的模块。 I've tried to do that in several ways but they don't work. 我已经尝试了几种方法来做到这一点,但是它们不起作用。

How I tried: 我如何尝试:

/app/code/local/Mage/Catalog/Block/Product/Fulllist.php (same as List.php with changed _getProductCollection() function:) /app/code/local/Mage/Catalog/Block/Product/Fulllist.php (与List.php相同,但_getProductCollection()函数已更改:)

protected function _getProductCollection()
{
$_productCollection = Mage::getModel('catalog/product')
    ->getCollection()
    ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('category_id', array(
        array('finset' => '30'),
        array('finset' => '32'))
    )
        ->addAttributeToSort('created_at', 'desc');

        return $_productCollection;
}

Then, at one of CMS pages i tried to use: 然后,在CMS页面之一中,我尝试使用:

{{block type="catalog/product_fulllist" template="catalog/product/list.phtml"}}

But that doesn't seem to work, page is clear. 但这似乎不起作用,页面很清晰。 Am I missing any part? 我缺少任何部分吗? Or is there an easier way to do that? 还是有更简单的方法来做到这一点?

Greetings! 问候!

Hello check below code 你好检查下面的代码

$collection = Mage::getModel('catalog/product')->getCollection(); 

$collection->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id=entity_id', null, 'left');

$collection->addAttributeToFilter('category_id', array('in' => array('finset'=>'30,31')));

$collection->addAttributeToSort('created_at', 'desc');

$collection->addAttributeToSelect('*');

return $collection;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM