Magento 1.9 – How to Get Configurable Attributes of a Simple Product

attributesconfigurable-productmagento-1.9product-attribute

We're currently developing our first Magento module, which will be used to sync with our platform.
The problem comes when trying to get product "variations" configurable attributes.

Let's say that the module loads a simple product to send to our API. The result will be something like:

item = {
"id": {
  "sku": "123130999"
},
"lang": "EN",
"currency": "EUR",
"category": "product",
"name": "myProduct",
"stock": "100",
"tags": ["someproduct", "sometag", "anotherone"],
"description": {
  "short": "Here is a short description.",
  "long": "And there's a long description."
},
"images": ["http://img.domain.com/myproduct-1.png", "http://img.domain.com/myproduct-1.png"],
"attributes": {
  "model": "MP34135",
  "dimensions": "25x15x0.7cm"
},
"price": {
  "final": "25000",
  "market": "22500",
  "cost": "19900"
}

}

I've managed to get that working already, but if the product is configurable we need another field to be passed. This field must contain product "variants", such as color, size, etc… Which will add something like that to the previous item:

variants : [
  {  "id": {"sku": "123130999"}, 
     "attributes":{"color":"black"}, 
     "price":{ discounted: 19900, min: 15000 }
  }, 
  {  "id": {"sku": "123130998"}, 
     "attributes":{"color":"white"}, 
     "price":{ discounted: 29900, min: 25000}
  } 
]

That's what I'm not able to manage. So my question is, which is the best way to get, for each simple product related to a configurable one, the attributes that can be "configured" at front in order to get the final product (i.e: color: black, size: small).

Thanks!

Best Answer

You'll first want to cache all of your attribute sets and their configurable attributes, so that you'll know what all of the configurable attributes are when you're processing a simple product. Pseudocode below. Not tested.

  1. Get all of the attribute set IDs, $attributeSetIds, and loop through each: foreach ($attributeSetIds as $attributeSetId)

  2. In each iteration, get all of the set's configurable attributes and cache them. The reason for those 3 filters is due to the very definition of a configurable attribute.

    $collection = Mage::getResourceModel('catalog/product_attribute_collection')
        ->setAttributeSetFilter($attributeSetId)
        ->addFieldToFilter('is_configurable', 1)
        ->addFieldToFilter('frontend_input', 'select')
        ->addFieldToFilter('is_global', 1);
    
    foreach ($collection as $attribute) {
        $cachedCpAtts[$attributeSetId][] = $attirbute->getAttributeCode();
    }
    
    // $cachedCpAtts will have all of your attribute sets' configurable attributes 
    // at the end of outer loop for `$attributeSetIds`.
    
  3. When you're processing a simple product, simply use its attribute_set_id and look up its config. attributes IDs. For example:

    $cpAttId = $cachedCpAtts[$product->getAttributeSetId()][0];
    
  4. Now it's only a matter of looking up the frontend label using the attribute ID. I believe there are a lot of resources on that front. You should probably cache all of the possible frontend labels for all configurable attributes as well.

Related Topic