简体   繁体   中英

create a block `catalog/product_list` with custom products ids

i have some products ids in a $ids and i want to render them in magneto default catalog/product_list block,

echo $this->getLayout()
                ->createBlock('catalog/product_list', '', 
                    array(
                        'template'=> 'catalog/product/list.phtml' ,
                         "product_id" => $ids)
                )->toHtml();

this is not working either

echo $this->getLayout()
                ->createBlock('catalog/product_list', '', 
                    array(
                        'template'=> 'catalog/product/list.phtml' ,
                         "product_id" => $ids)
                )->setProductId($ids)->toHtml();

in other words i want the code version of this block

{{block type="catalog/product_list" product_id="1,2,3" template="catalog/product/list.phtml"}}

I haven't tested this, but you could try instantiating the block and giving it a product collection:

$collection = Mage::getModel('catalog/product')->getCollection()
    ->addIdFilter([1, 2, 3]);

$block = $this->getLayout()
    ->createBlock('catalog/product_list')
    ->setCollection($collection);

echo $block->toHtml();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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