Magento - load only configurable products

后端 未结 6 1510
北荒
北荒 2020-12-01 05:10

I have the following code:

$_productCollection = $this->getLoadedProductCollection();

foreach ($_productCollection as $_product)
{
  if ($_product->_d         


        
6条回答
  •  Happy的楠姐
    2020-12-01 05:17

    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.

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

    In this case I think you would be happier with:

    print_r($_productCollection->toArray())
    

提交回复
热议问题