且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Magento-获取结果以HTML格式查看产品集合

更新时间:2023-11-30 12:42:04

在Magento中,使代码正确工作的方式比尝试处理难看的旧代码要容易得多.遇到具体问题时,我很乐意帮助您以正确的方式编写代码.而且,从长远来看,技术债务的成本会更高.

Making the code work the right way is much more easier in Magento than trying to work with ugly legacy code. I would gladly help you make the code the proper way when you have specific questions. Also, in the longterm, technical debt is gonna cost alot more.

无论如何,回到您的问题.

Anyway, back to your issue.

在Magento块中,没有像在任何应用程序$myvar = new className中那样实例化...几乎从来没有. 本教程可以帮助您更好地了解Magento的布局和块.

In Magento block are not instantiated like in any app $myvar = new className ... almost never. This tutorial can help you understand better Magento's layout and blocks.

但是,如果要创建一个块,可以使用以下方法:

But if you want to create a block a way to do it is:

$block = Mage::getSingleton('core/layout')->createBlock('catalog/product_list')

现在与产品系列有关,您应该检查Mage_Catalog_Block_Product_List::_getProductCollection的实际工作方式,因为它使用分层导航,而不是简单的产品系列.

Now related to your product collection you should check how Mage_Catalog_Block_Product_List::_getProductCollection actually works, because it uses the layered navigation, not a simple product collection.

此外,假设至少您使用的是Magento控制器且您在某个函数内,则以下代码将显示指定类别的产品的第一页:

Further, assuming that at least you are using a Magento controller and you are within a function, the following code will display the first page of products for a specified category:

//$category_id needs to be set
$layout = Mage::getSingleton('core/layout');
$toolbar = $layout->createBlock('catalog/product_list_toolbar');
$block = $layout->createBlock('catalog/product_list');
$block->setChild('toolbar', $toolbar);
$block->setCategoryId($category_id);
$block->setTemplate('catalog/product/list.phtml');  
$collection = $block->getLoadedProductCollection();
$toolbar->setCollection($collection);
//render block object 
echo $block->renderView();  

显示特定ID:

  • 您将根类别ID用于$ category_id变量(还要确保已设置显示根类别(或包含产品ID的另一个类别ID)
  • 您可以加入catalog_block_product_list_collection事件,以将ID过滤器添加到集合中(在_beforeToHtml函数中称为)
  • you use root category id for $category_id variable (also make sure that display root category is set (or another category id that contains your product ids)
  • you can hook into catalog_block_product_list_collection event to add your ID Filter to the collection (this is called in _beforeToHtml function)

但是,所有这些构造都不牢固,还有一些要注意的地方(其他子块,过滤器等)

But, all this construction is not solid and there are still some points that require attention (other child blocks, filters and so on)