Magento - 仅加载可配置的产品
我有以下代码:
$_productCollection = $this->getLoadedProductCollection();
foreach ($_productCollection as $_product)
{
if ($_product->_data['type_id'] == 'configurable')
{
...
}
}
虽然它完成了它应该做的事情,但它大大减慢了页面加载时间。是否可以仅加载可配置产品并删除“可配置”检查?商店有12000种产品,大约700种是可配置的,其余的是儿童简单产品。
我发现以下代码返回所有可配置产品。我只需要当前类别中的产品:
$collectionConfigurable = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('type_id', array('eq' => 'configurable'));
I have the following code:
$_productCollection = $this->getLoadedProductCollection();
foreach ($_productCollection as $_product)
{
if ($_product->_data['type_id'] == 'configurable')
{
...
}
}
While it does what it's supposed to do, it greatly slows down page load time. Is it possible to load only configurable products and remove the check for 'configurable'? The store has 12000 products, about 700 are configurable and the rest are child simple products.
I found the following code which returns all configurable products. I need only the products within the current category:
$collectionConfigurable = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('type_id', array('eq' => 'configurable'));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
getLoadedProductCollection()
的问题是它已经加载 - 产品的数据已经从数据库中检索。仅使用当前类别的产品集合也不够好,这会忽略“层”(属性过滤器)。诀窍是首先从列表中删除加载的产品。print_r($_productCollection)
也有它的问题,您不仅输出产品,还输出数据库连接、缓存值以及产品的各个资源等资源的所有详细信息,并且等等...在这种情况下,我认为您会更满意:
The problem with
getLoadedProductCollection()
is it's already loaded - the products' data has already been retrieved from the database. Just using the current category's product collection isn't good enough either, that will ignore the "layers" (attribute filters). The trick is to remove the loaded products from the list first.print_r($_productCollection)
has it's issues too, you're not just outputting the products but also all details of the resource that is the database connection, and cached values, and the products' individual resources, and so on...In this case I think you would be happier with:
所有这些解决方案都不适合我,试试这个:
它有效,但不知道它是否正确(我是 Magento 的新手)。请告诉我。
All those solutions didn't work for me, try this:
It works but don't know if it's correct (I'm new to Magento). Let me know please.
您执行此操作的方式需要先加载所有产品,然后再解析和过滤它们。这可能更接近您正在寻找的内容:
The way you're doing this requires all products to be loaded before you parse through and filter them. This is probably closer to what you're looking for:
尝试以下
加载可配置和简单的以及尝试
Try following
For loading configurable and simple as well try
以下是仅获取可配置产品的代码:
Here is the code for getting only configurable products:
使用 \Magento\Catalog\Model\ResourceModel\Product\Collection $_productcollection,
Use
\Magento\Catalog\Model\ResourceModel\Product\Collection $_productcollection,
如果您将简单产品的可见性更改为“单独不可见”,Magento 将不会加载它以显示在产品列表页面中。
If you change Visibility of simple product to "Not Visible Individually", Magento will not load it to display in product list page.