2

I have a configurable product. I need a function that when given the product id of one of it's children products, will return the configurable attribute value it applies to the parent configurable product.

To rephrase the question. I have a configurable product with a size attribute of size, and it has three children products, one for small, medium, and large. When I get the product id of the small child product, how I can I get value of the size attribute. This should ideally be attribute agnostic, so I don't need to know beforehand that I'm getting the size attribute, it should be whatever configurable attribute applies.

I have tried the following.

$productAttributeOptions = $parent_product->getTypeInstance(true)->getConfigurableAttributesAsArray($parent_product);
$attributeOptions = array();
foreach ($productAttributeOptions as $productAttribute) {
    foreach ($productAttribute['values'] as $attribute) {
        $attributeOptions[$productAttribute['label']][$attribute['value_index']] = $attribute['store_label'];
    }
}

This gives me all the attributes for a configurable product, being small, medium, and large. Passing in the current product gives an error, because the getConfigurableAttributesAsArray function is only available to configurable products.

Searching google and stack gives me results on how to do things such as get all configurable attributes, get a specific already known attribute, and so on.

Update: To clarify, my question specifies that I shouldn't need to specify the name of the attribute, and need to return all configurable attributes regardless of name. Getting attribute from simple products of configurable products does not apply. In the answer it includes this code to make it work. addAttributeToSelect('color')-> groupByAttribute('color')

  • My question specifies that I shouldn't need to specify the name of the attribute, and need to return all configurable attributes regardless of name. That question does not apply. In the answer it includes this code to make it work. addAttributeToSelect('color')-> groupByAttribute('color'). – Goose Feb 29 '16 at 15:30
5

To me the only modification you can make to get the configurable attribute value of a simple product is the following:

$simpleProductId = $product->getProductId();
$productAttributeOptions = $parent_product->getTypeInstance(true)->getConfigurableAttributesAsArray($parent_product);
$attributeOptions = array();
foreach ($productAttributeOptions as $productAttribute) 
{
    $simpleProduct = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToFilter('entity_id',array('eq' => $simpleProductId))
    ->addAttributeToSelect($productAttribute['attribute_code'])
    ->setPageSize(1)
    ->getFirstItem();

    // This will get you the attribute option id (e.g. 15)
    $attributeOptions[] = $simpleProduct->getData($productAttribute['attribute_code']);
    // Whereas this will get you the attribute option value (e.g. Black)
    $attributeOptions[] = $simpleProduct->getData($productAttribute['attribute_code']."_value");
}

// This will display every configurable attribute value of your simple product
print_r($attributeOptions);

NB: this is assuming the simple product has not been loaded yet. In case it hasn't been loaded you can simply delete the following lines:

$simpleProduct = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToFilter('entity_id',array('eq' => $simpleProductId))
    ->addAttributeToSelect($productAttribute['attribute_code'])
    ->setPageSize(1)
    ->getFirstItem();

The main problem here is that a simple product can be assigned to several different configurable products with several different attributes. This is why you need the configurable product attributes first before being able to retrieve the attribute value of a simple product.

  • Unfortunately, this is returning an empty array. Also, I don't need to get the simple product itself, just the configurable attribute that applies to it. – Goose Feb 29 '16 at 15:41
  • @Goose you will have to get the simple product if you want to get the configurable attribute that applies to it, see the end of my edited answer for the exact reason – Raphael at Digital Pianism Feb 29 '16 at 15:42
  • Sorry, misunderstood. Still, however, attributeOptions is returning empty array when printed after the foreach. – Goose Feb 29 '16 at 15:43
  • The following line in the code I pasted $simpleProductId = $product->getProductId(); was totally made up by me as I don't have access to your code. You'll need to adapt this line to retrieve the simple product id correctly, that could be the cause. – Raphael at Digital Pianism Feb 29 '16 at 15:45
  • That line works and returns the correct product. However $attributeOptions is still returning empty unfortunately. – Goose Feb 29 '16 at 15:47

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