Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento - load only configurable products

Tags:

php

magento

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'));
like image 881
Vincent Avatar asked Mar 12 '11 03:03

Vincent


People also ask

What is the difference between simple product and configurable product in Magento?

The core differences between Simple products and Configurable products are SKUs. Simple products only have a single SKU, while configurable products have multiple SKUs.


3 Answers

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())
like image 135
clockworkgeek Avatar answered Oct 02 '22 14:10

clockworkgeek


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.

like image 34
Fred K Avatar answered Oct 02 '22 13:10

Fred K


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');
like image 21
philwinkle Avatar answered Oct 02 '22 12:10

philwinkle