【问题标题】:How to get the options of a configurable attribute in Magento?如何在 Magento 中获取可配置属性的选项?
【发布时间】:2011-06-03 10:35:42
【问题描述】:

我们想通过 Magento-API 在另一个系统中导出/导入可配置产品。对我们来说重要的是可配置产品的价值,例如具有 3 种颜色(红色、绿色和蓝色)的 T 恤。

我们收到具有以下功能的可配置属性:

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,以获取该产品中使用的选项(如果可配置属性为“颜色”,则为蓝色、绿色、红色) ?

非常感谢您的回答!

提姆

【问题讨论】:

  • 问题不清楚。 “使用现在可用的标签/ID 获取该产品中使用的值是什么意思?
  • 我们想要获得红色、蓝色和绿色等选项(如果可配置属性是“颜色”)。通过上述功能,我们可以获得有关使用的可配置属性的信息。
  • 所以您想要给定产品 [红、绿、蓝] 的“颜色选项”?

标签: api magento configurable


【解决方案1】:

由于我们找不到更好的解决方案,这就是我想出的:

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;
}

希望这对任何人都有帮助。

【讨论】:

    【解决方案2】:

    我不是 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() 函数为我提供了可用的选项标签和值。

    希望这会有所帮助。如果没有,请让我在哪里误解。谢谢!

    【讨论】:

    • 谢谢。我会试一试,然后再回复你的答案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 2014-10-31
    • 1970-01-01
    • 2016-05-02
    相关资源
    最近更新 更多