Search code examples
phpezpublish

Traverse ez-multioption attribute options in EZ Publish PHP-side


I need help to traverse all options in a multioption.

I use the Product-class with a new multioption-attribute called "product_properties". I need a function to check if the optionID the user chose on the front-end matches an option in the list, and return true if a match is found.

This way I can check if e.g. the user chose "Red" as the "Color" on a product.

In pseudo-code this is what I need:

Parameters: postedOptionID, currentObjectID

  1. Fetch attribute "product_properties" (multioption) on object .

  2. For each option for "Color" in "product_properties"

    2.1 If postedOptionID == optionID

    2.1.1 return true

Thanks


Solution

  • I finally found a way :)

    • $product_properties_name is the name of a class-attribute that's an 'ezmultioption'-datatype. In my case it's called 'product_properties' and is an attribute on the 'Product'-class.

    First get all of the object's attribute: $contentObjectAttributes = $contentObject->version($contentObject->attribute( 'current_version' ) )->contentObjectAttributes();

    and then loop each and find 'product_properties':

    // Loop all attributes of the object's class         
    foreach(array_keys($contentObjectAttributes) as $key)        
    {
        $contentObjectAttribute = $contentObjectAttributes[$key];
        $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();           
        $attributeIdentifier = $contentClassAttribute->attribute("identifier");     
    
        // Get 'product_properties'-attribute
        if ($attributeIdentifier == $product_properties_name)
        {               
            // Get the multioption
            $multioption_list = $contentObjectAttribute->content();
    
            // Loop all multioption lists (Color, Make, Brand etc.)
            foreach($multioption_list->attribute('multioption_list') as $index => $option)
            {       
                // Loop through this multioption and get all options (if 'Color', get 'Blue', 'Red', 'Green' etc.)
                foreach($option['optionlist'] as $option)
                {
                    $optionValue = trim($option['value']);
    
                    // if there's a match on $optionValue, do something interesting...  
                }                                               
            }           
        }       
    }