I have built a collection (includes ->addAttributeToSelect('*')) to generate a CSV and then from this collection, I need to fetch the associated simple products so that I can load different sizes, colours etc:-

$associatedProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$product);

And I can then load each simple product in a foreach loop:-

foreach ($associatedProducts as $simple) {
        //load product
        $simple_product = Mage::getModel('catalog/product')->load($simple->getId());

I have a custom attribute (dropdown) and I need to only load simple products if this dropdown value is set to 'Yes' but I can only seem to be able to fetch this value for a configurable product:-

$product->getAttributeText('attribute_code'); <-- this works

Trying to get the attribute from the simple product doesn't work:-

$simple->getAttributeText('attribute_code'); <-- this doesn't work

And it returns bool(false) rather than a string.

There is obviously lots more of this script but I am hoping I have just pulled out what is relevant above.

Can anyone shed any light as to what I need to do differently for me to get the attribute value from the simple product loading?


Here is a sample SQL output for one of the simple products as per the comments in @Richard's answer below:-

SELECT 1 AS `status`, `e`.`entity_id`, `e`.`type_id`, `e`.`attribute_set_id`, `cat_index`.`position` AS `cat_index_position`, `e`.`entity_id`, `e`.`attribute_set_id`, `e`.`type_id`, `e`.`cost`, `e`.`created_at`, `e`.`gift_message_available`, `e`.`has_options`, `e`.`image_label`, `e`.`is_recurring`, `e`.`links_exist`, `e`.`links_purchased_separately`, `e`.`links_title`, `e`.`msrp`, `e`.`msrp_display_actual_price_type`, `e`.`msrp_enabled`, `e`.`name`, `e`.`news_from_date`, `e`.`news_to_date`, `e`.`price`, `e`.`price_type`, `e`.`price_view`, `e`.`recurring_profile`, `e`.`required_options`, `e`.`shipment_type`, `e`.`short_description`, `e`.`sku`, `e`.`sku_type`, `e`.`small_image`, `e`.`small_image_label`, `e`.`special_from_date`, `e`.`special_price`, `e`.`special_to_date`, `e`.`tax_class_id`, `e`.`thumbnail`, `e`.`thumbnail_label`, `e`.`updated_at`, `e`.`url_key`, `e`.`url_path`, `e`.`visibility`, `e`.`weight`, `e`.`weight_type`, `e`.`product_teaser`, `e`.`manufacturer`, `e`.`manufacturer_value`, `e`.`product_size_women`, `e`.`product_size_women_value`, `e`.`image`, `e`.`colour`, `e`.`colour_value` FROM `catalog_product_flat_1` AS `e` INNER JOIN `catalog_category_product_index` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id='1' AND cat_index.category_id = '3' WHERE (e.type_id = 'configurable') AND (e.visibility IN(4, 3, 2))

Have you looked at the prototype for getUsedProducts? It's public function getUsedProducts($requiredAttributeIds = null, $product = null) so you can call getUsedProducts(array(id_of_your_attribute), $product)

Looping through that array and then loading each child is probably not the most efficient way of doing what you're after. You could either add the other attributes you need to the getUsedProducts, or if there's lots and adding by id doesn't make sense, just use getUsedProductCollection and add call addAttributeToSelect or addAttributeToFilter yourself, see what getUsedProducts does itself:

if (is_array($requiredAttributeIds)) {
    foreach ($requiredAttributeIds as $attributeId) {
        $attribute = $this->getAttributeById($attributeId, $product);
        if (!is_null($attribute))
            $collection->addAttributeToFilter($attribute->getAttributeCode(), array('notnull'=>1));
| improve this answer | |
  • Useful information, thanks @Richard. I posted a follow up question though as I learned that I am able to get another custom attribute but not this one with the same code. – zigojacko Aug 12 '15 at 14:19
  • You could probably have just edited this question. Did you notice it was attribute_id not attribute_code? you might want to debug what sql is getting generated for the collection – Richard Aug 12 '15 at 14:24
  • I did think about editing it but wasn't sure if there was slightly different... In the array for getUsedProducts I tried with the ID of the attributes but it makes no difference. I've already var dumped every simple product but I guess I could try and see if I can figure anything out from the SQL generated - thanks. – zigojacko Aug 12 '15 at 15:20
  • Can't even get getSelect to work (like echo $simple_product->getSelect();). This is a script located in the Magento root to generate a CSV. – zigojacko Aug 12 '15 at 15:37
  • getSelect() is called on the collection, which is where $simple_product is being loaded from because you're calling ->getAttributeText before (if you are still) loading the whole object. You want to see if you're successfully getting it to request the attribute you want – Richard Aug 12 '15 at 15:40

Don't know if it's a hack or the normal way, but you can get the attribute id using

$attrId = $simple->getResource()
    ->getAttributeRawValue($simple->getId(), 'attribute_name', 0);

and the text using

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.