How to get the options of a configurable attribute in Magento?
Asked Answered
G

2

7

we want to export/import configurable products through the Magento-API in another system. What is important for us, are the values of the configurable products like a T-Shirt which has 3 colors (red, green and blue).

We receive the configurable attributes with the following function:

public function options($productId, $store = null, $identifierType = null)
{
    $product = $this->_getProduct($productId, $store, $identifierType);

    if (!$product->getId()) {
        $this->_fault('not_exists');
    }

    $configurableAttributeCollection = $product->getTypeInstance()->getConfigurableAttributes();

    $result = array();
    foreach($configurableAttributeCollection as $attribute){
        $result[$attribute->getProductAttribute()->getAttributeCode()] = $attribute->getProductAttribute()->getFrontend()->getLabel();
        //Attr-Code:    $attribute->getProductAttribute()->getAttributeCode()
        //Attr-Label:   $attribute->getProductAttribute()->getFrontend()->getLabel()
        //Attr-Id:      $attribute->getProductAttribute()->getId()
    }


    return $result;
}

But how is it possible to get the options used in that product (e.a. blue, green, red if the configurable attribute is "color") with the now available label/id from the configurable attribute which we got through the above function?

Answers are very appreciated!

Tim

Gladysglagolitic answered 4/1, 2011 at 14:32 Comment(3)
The question is not clear. What do you mean by "get the used values in that product with the now available label/id?Starling
We want to get the options like red, blue and green (if the configurable attribute is "color"). And with the above stated funtion we get the information about the used configurable attributes.Gladysglagolitic
So you want the "color options" for a given product [red, green, blue]?Tupper
G
7

Since we couldn't find a better solution, this is what I came up with:

public function options($productId, $store = null, $identifierType = null)
{
    $_product = $this->_getProduct($productId, $store, $identifierType);

    if (!$_product->getId()) {
        $this->_fault('not_exists');
    }

    //Load all simple products
    $products = array();
    $allProducts = $_product->getTypeInstance(true)->getUsedProducts(null, $_product);
    foreach ($allProducts as $product) {
        if ($product->isSaleable()) {
            $products[] = $product;
        } else {
            $products[] = $product;
        }
    }

    //Load all used configurable attributes
    $configurableAttributeCollection = $_product->getTypeInstance()->getConfigurableAttributes();

    $result = array();
    //Get combinations
    foreach ($products as $product) {
        $items = array();
        foreach($configurableAttributeCollection as $attribute) {
            $attrValue = $product->getResource()->getAttribute($attribute->getProductAttribute()->getAttributeCode())->getFrontend();
            $attrCode = $attribute->getProductAttribute()->getAttributeCode();
            $value = $attrValue->getValue($product);
            $items[$attrCode] = $value[0];
        }
        $result[] = $items;
    }

    return $result;
}

Hope this helps anybody.

Gladysglagolitic answered 5/7, 2011 at 6:55 Comment(0)
F
1

I'm not 100% sure that I understand the question ... assuming you want the values and labels for the configurable options for a specific product I assume this would work (tested on version 1.4.0.1)

public function options($productId, $store = null, $identifierType = null)
{
    $product = $this->_getProduct($productId, $store, $identifierType);

    if (!$product->getId()) {
        $this->_fault('not_exists');
    }

    $configurableAttributeCollection = $product->getTypeInstance()->getConfigurableAttributes();

    $result = array();
    foreach($configurableAttributeCollection as $attribute){
        $result[$attribute->getProductAttribute()->getAttributeCode()] = array(
                $attribute->getProductAttribute()->getFrontend()->getLabel() => $attribute->getProductAttribute()->getSource()->getAllOptions()
        );
        //Attr-Code:    $attribute->getProductAttribute()->getAttributeCode()
        //Attr-Label:   $attribute->getProductAttribute()->getFrontend()->getLabel()
        //Attr-Id:      $attribute->getProductAttribute()->getId()
    }


    return $result;
}

again not sure exactly what your looking for, but the $attribute->getProductAttribute()->getSource()->getAllOptions() function gave me the available options label and value.

Hope this helps. if not, let me where I misunderstood. Thanks!

Fenrir answered 30/6, 2011 at 0:27 Comment(1)
Thank you. I will give it a try and come back to your answer!Gladysglagolitic

© 2022 - 2024 — McMap. All rights reserved.