如何在Magento中获取可配置属性的选项?

我们希望通过另一个系统中的Magento-API导出/导入可配置产品。对我们来说重要的是可配置产品的价值,如T恤,有3种颜色(红色,绿色和蓝色)。 我们使用以下函数接收可配置属性:
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;
}
但是如何使用我们通过上述函数获得的可配置属性中的现有标签/ id来获取该产品中使用的选项(例如蓝色,绿色,红色,如果可配置属性为“颜色”)? 答案非常感谢! 蒂姆     
已邀请:
由于我们找不到更好的解决方案,这就是我想出的:
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;
}
希望这可以帮助任何人。     
我不是100%确定我理解这个问题...假设你想要特定产品的可配置选项的值和标签,我认为这样可行(在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;
}
再次不确定你究竟在寻找什么,但
$attribute->getProductAttribute()->getSource()->getAllOptions()
功能给了我可用的选项标签和价值。 希望这可以帮助。如果没有,请让我误解。谢谢!     

要回复问题请先登录注册