0

I am trying to add a condition statement to add certain text in front of price. I've got the code to check whether it's configurable product or not:

if ($_product->getData('type_id') == 'configurable') {
    // ...
}

How can I add another code to check if the attribute options are more than one?

For example,

attribute name: color,
dropdown options: green, yellow, blue...
  • Do you mean you are trying to check if there are more than 1 "selectable attributes" like color, manufacturer, etc., or there are more than 1 "selectable options" for 1 attribute like green, yellow, blue for color? – d.yuk Jan 22 '15 at 3:35
  • thanks for your reply. i'd like to check if there are more than 1 selectable option for 1 attribute. meaning, if there are more than 1 color available. as i only have 1 attribute 'color', no size etc. – nlife Jan 22 '15 at 5:52
0

If you want to get the number of options available for the configurable attributes of a given product try below code.

UPDATED CODE:

  $product = Mage::getModel('catalog/product')->load($id);

if($product->isConfigurable()) {
    $attrs  = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);

    foreach($attrs as $attr) {
        if(trim($attr['attribute_code']) == 'color') {
            $options    = $attr['values'];

            // Check the color attribute options count is greater than 1.
            if(count($options) > 1) {
                // Do something.
            }
        }
    }
}
  • thanks for your reply. the concept is closed to what i want to achieve, i want add a condition for all configurable products which have more than 1 selectable options under attribute 'color'. i am not an expert of php coder, hopefully someone can help. thanks. – nlife Jan 22 '15 at 6:29
  • Updated the answer – Sukeshini Jan 22 '15 at 7:05

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.