Magento: Get a configurable products associated simple product data
| 27 Comments | MagentoWe 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;
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
Hi John,
Yes that’s a possibility. take a look below:
Hi there is one extension which shows image of simple product when you selecting option of configurable product. Its http://extensions.sashas.org/customer-experience/configurable-image-swticher.html Please check it out , hope it will help you.
That’s nice they rolled it into an extension, but they might want to check their spelling 🙂
Hello have a problem to get is_salable(). it return nothing as if it is empty.
echo $simple_product->getIsSalable();
Hi Vishant,
You need to check isAvailable as isSalable is for product types not products.
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) ;
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.
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?
Hi
Sorry to waste our time! I figured it out – use getAttributeText(‘attribute_name’) rather than getAttributeName()
Thanks!
Glad you figured it out Matt! getAttributeText(‘attribute_name’) is for drop down values and getData(’attribute_name’) is for text attributes.
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.
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.
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.
How to get the simple product image on product view page in magento. pls give idea on that.
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.
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
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.
Hi Gregg,
Thank you for your reply! We will try.
Thanks again,
Tania
Hi Gregg – nice data retrieval. Can you retrieve a price range (Minimum – Maximum) using this code? Thank you. — Kevin
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!
I don’t know of a flag to get out-of-stock products, you might have to write a custom query.
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
Sounds like it would be custom code, that’s not something that the core of magento does.
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
Hello, Great Work.
Is there anyway to sort the simple product from the price attribute from low to high?
Thanks
Mike
Hello Gregg, Really great work. But is there any way to add to cart button for individual product in the same page