Magento-获取结果以HTML格式查看产品集合 [英] Magento - get results view HTML for a collection of products

查看:85
本文介绍了Magento-获取结果以HTML格式查看产品集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Web服务获得了magento ID的列表.我将它们加载到数组$product_ids中,所以有这样的内容:

I get a list of magento ids from a web service. I load these into and array $product_ids, so I have something like this:

Array
(
    [0] => 1965
    [1] => 3371
    [2] => 1052
)

然后我可以将其放入集合:

I can then make this into a collection:

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

使用我的Magento检查器,我已经看到类别页面使用了 Mage_Catalog_Block_Product_List 以显示产品列表.我想在课堂上做类似的事情.我尝试加载:

Using my Magento inspector, I've seen that the category pages use the class Mage_Catalog_Block_Product_List to display lists of products. I'd like to do something similar in my class. I've tried loading:

$ProductList = new Mage_Catalog_Block_Product_List();
$ProductList->setCollection($collection);

然后我尝试按以下方式加载结果的HTML:

And then I've tried to load the HTML of the results as follows:

$CollectionHTML = $ProductList->_toHtml();

但是$CollectionHTML为空.

如何获取您在列表视图中看到的HTML(即frontend/base/default/template/catalog/product/list.phtml的生成输出,但已给出我的收藏)?

How would I get the HTML of what you see in the list view (i.e. the generated output of frontend/base/default/template/catalog/product/list.phtml, but given my collection)?

推荐答案

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

这篇关于Magento-获取结果以HTML格式查看产品集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆