BLOG

Sometimes nerdy and wordy, but mostly helpful articles on website design and development.

Magento: Get a configurable products associated simple product data

| 27 Comments | Magento

We recently helped a client with their Magento website. They were wanting to show a grid while on a configurable products page of all the associated simple products. This is an easy way to quickly show the customer what products are available along with their price.

The first step is to determine if the product is a configurable type, otherwise we don’t want to show the grid. We’ll assume you’re using the default template/catalog/product/view.phtml file and that you’ve already got your product object ($_product). Below is the code to check if the current product is configurable.

if($_product->getTypeId() == "configurable"):

Our next step is to get a product collection based on the current product. We’ll also say we want to get all attributes.

$conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
$simple_collection = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();

Now that we have our array $simple_collection, we can iterate through it like we normally would any product data. Below we loop through and create separate lines containing the sku, name and price. We use a cool helper function to output the currency with appropriate $.

foreach($simple_collection as $simple_product){
    echo $simple_product->getSku() . " - " . $simple_product->getName() . " - " . Mage::helper('core')->currency($simple_product->getPrice()) . "<br>";
}

And the final code all together.

Getting a simple products belonging to configurable product

if($_product->getTypeId() == "configurable"):
	$conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
	$simple_collection = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
	foreach($simple_collection as $simple_product){
	    echo $simple_product->getSku() . " - " . $simple_product->getName() . " - " . Mage::helper('core')->currency($simple_product->getPrice()) . "<br>";
	}
endif;

27 responses to “Magento: Get a configurable products associated simple product data

  1. This works great in listing all the simple products for a Configurable one.

    But, can it be modified to list the details of only the currently selected option?

    Thanks

  2. Hi John,

    Yes that’s a possibility. take a look below:

    $collection = Mage::getModel(‘catalog/product’)
    ->getCollection()
    ->addAttributeToFilter(‘color’, ‘blue’);

    1. That’s nice they rolled it into an extension, but they might want to check their spelling 🙂

  3. Wanting to add this to my current code which pulls out the regular price on special priced products
    my code takes the final price and regular price and only shows on special priced products.
    this code is in the view.phtml file
    However when a configurable product shows up it wont update the price for the simple products.

    helper(‘tax’)->getPrice($_product, $_product->getFinalPrice()); $_regularPrice = $this->helper(‘tax’)->getPrice($_product, $_product->getPrice()); if ($_regularPrice != $_finalPrice) echo Mage::helper(‘core’)->currency ($_product->getPrice(),true,false) ;

    1. If this isn’t working it could be that reg and final prices are the same so you’re not getting into your if statement.

  4. Hi Gregg

    Great post thanks for this!

    I have one issue, I am trying to pull out some of my own attribute values and names, but all I can get is the id’s – can you help?

  5. Hi

    Sorry to waste our time! I figured it out – use getAttributeText(‘attribute_name’) rather than getAttributeName()

    Thanks!

    1. Glad you figured it out Matt! getAttributeText(‘attribute_name’) is for drop down values and getData(’attribute_name’) is for text attributes.

  6. Yes this work perfect. But if I inspect $conf by print_r($conf) it display huge amount of config vars (also Password) which are unimportant for this information someone likes to gather.

  7. Hi,
    I have one issue, I want to change configure product image on color and size attribute but problem is that price of attribute added in product price is right but image not changing.
    Please help me.

    1. Without seeing your code it’s hard to help. This would be an easy job for javascript to check for an attribute and update the image based on it being selected.

    1. Well you could check your media.phtml file in your theme to get that data, but you should be able to use
      echo Mage::getModel('catalog/product_media_config')
      ->getMediaUrl( $product->getImage() );

      $product may differ on your theme.

  8. Hi Gregg,

    Thank you so much for posting the above solution for the configurable product pages.

    Can your solution be adapted to single product pages associated with a configurable product?

    I am trying to display a grid of images of all simple products associated with a configurable product but on the single product pages themselves, not on the parent a configurable product.

    If I only change:
    if($_product->getTypeId() == “configurable”):

    To:
    if($_product->getTypeId() == “simple”):

    It appears that “getUsedProductCollection” does not work anymore. Am I missing something?

    Thanks again,

    Tania

    1. Hi Tania,

      That function getUsedProductCollection is only available to configurable products.

      $simple_product->getImageUrl() – This method should work to get the product image, but it’s untested.

  9. Hi Gregg – nice data retrieval. Can you retrieve a price range (Minimum – Maximum) using this code? Thank you. — Kevin

  10. HI! I have both configurable products and simple products. I’m looking to display all Configurable products even if the simple products associated with it are sold out.

    I’d appreciate the help!

    1. I don’t know of a flag to get out-of-stock products, you might have to write a custom query.

  11. Hi,

    I have looked every where and i am not able to understand the solutions to ; adding attribute dropdown on product grid/list view, and changing price on change of attribute.

    Reason behind the activity is ; i am making a grocery site and i need to place package sizes i.e. 1KG 500 GM 100GM (as attributes) , and change price respectively on change.

    I know this might not be a right place to ask this question but any help will be appreciated.

    Thanks and Regards

    1. Sounds like it would be custom code, that’s not something that the core of magento does.

  12. Hey, you are a life saver 🙂

    The Code works perfectly.

    Is there anyway that they can be sorted by the Price attribute automatically from low to high?

    Kind regards

  13. Hello, Great Work.

    Is there anyway to sort the simple product from the price attribute from low to high?

    Thanks
    Mike

Comments are closed.

Get the Latest

  • Get email updates whenever we post new blog articles.
  • This field is for validation purposes and should be left unchanged.

Recent Articles