Magento – Getting custom attribute value from simple product load based on product collection

attributescollection;dropdown-attributesimple-product

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?

Edit

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))

Best Answer

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));
    }
}