php Magento - 只加载可配置的产品

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5280392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:50:53  来源:igfitidea点击:

Magento - load only configurable products

phpmagento

提问by Vincent

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.

虽然它做了它应该做的事情,但它大大减慢了页面加载时间。是否可以仅加载可配置产品并取消对“可配置”的检查?商店有12000个产品,大约700个是可配置的,其余是子简单的产品。

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'));

回答by clockworkgeek

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.

问题getLoadedProductCollection()是它已经加载了——产品的数据已经从数据库中检索到了。仅使用当前类别的产品集合也不够好,这将忽略“层”(属性过滤器)。诀窍是首先从列表中删除加载的产品。

// First make a copy, otherwise the rest of the page might be affected!
$_productCollection = clone $this->getLoadedProductCollection();
// Unset the current products and filter before loading the next.
$_productCollection->clear()
                   ->addAttributeToFilter('type_id', 'configurable')
                   ->load();

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...

print_r($_productCollection)也有问题,您不仅要输出产品,还要输出作为数据库连接的资源的所有详细信息、缓存值以及产品的各个资源,等等...

In this case I think you would be happier with:

在这种情况下,我认为您会更满意:

print_r($_productCollection->toArray())

回答by Fred K

All those solutions didn't work for me, try this:

所有这些解决方案都不适合我,试试这个:

$_productCollection1 = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('type_id','configurable'); 

foreach ($_productCollection1 as $product1) {
    echo $product1->getName();
    ...
}

It works but don't know if it's correct (I'm new to Magento). Let me know please.

它有效,但不知道它是否正确(我是 Magento 的新手)。请让我知道。

回答by Mukesh

Try following

尝试以下

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

    foreach($collection as $product)
    {

    }

For loading configurable and simple as well try

对于加载可配置和简单的以及尝试

$collection->addAttributeToFilter('type_id', array('in' => array('configurable','simple')));

回答by philwinkle

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:

您这样做的方式要求在解析和过滤它们之前加载所有产品。这可能更接近您正在寻找的内容:

$_productCollection = $this ->getLoadedProductCollection()
                            ->addAttributeToFilter('type_id','configurable');

回答by Deepak Kumar

Here is the code for getting only configurable products:

这是仅获取可配置产品的代码:

 $Config_products  =  Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToFilter('type_id','configurable');

回答by ndlinh

If you change Visibility of simple product to "Not Visible Individually", Magento will not load it to display in product list page.

如果您将简单产品的可见性更改为“单独不可见”,Magento 将不会加载它以显示在产品列表页面中。