0

我需要帮助来遍历多选项中的所有选项。

我将 Product 类与一个名为“product_properties”的新多选项属性一起使用。我需要一个函数来检查用户在前端选择的 optionID 是否与列表中的选项匹配,如果找到匹配项则返回 true。

通过这种方式,我可以检查用户是否选择“红色”作为产品的“颜色”。

在伪代码中,这就是我需要的:

参数:postedOptionID、currentObjectID

  1. 获取对象上的属性“product_properties”(多选项)。

  2. 对于“product_properties”中“颜色”的每个选项

    2.1 如果postedOptionID == optionID

    2.1.1 返回真

谢谢

4

1 回答 1

1

我终于找到了一种方法:)

  • $product_properties_name 是“ezmultioption”数据类型的类属性的名称。在我的例子中,它被称为“product_properties”,是“产品”类的一个属性。

首先获取对象的所有属性: $contentObjectAttributes = $contentObject->version($contentObject->attribute( 'current_version' ) )->contentObjectAttributes();

然后循环每个并找到'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...  
            }                                               
        }           
    }       
}   
于 2011-04-01T15:36:25.643 回答