Как получить параметры настраиваемого атрибута в Magento? - PullRequest
7 голосов
/ 04 января 2011

мы хотим экспортировать / импортировать настраиваемые продукты через Magento-API в другой системе. Для нас важны значения настраиваемых продуктов, таких как футболка, которая имеет 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;
}

Но как можно получить параметры, используемые в этом продукте (например, синий, зеленый, красный, если настраиваемый атрибут «color»), с доступной теперь меткой / идентификатором из настраиваемого атрибута, который мы получили через вышеупомянутую функцию

Ответы очень ценятся!

Тим

Ответы [ 2 ]

7 голосов
/ 05 июля 2011

Так как мы не могли найти лучшего решения, вот что я придумал:

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

Надеюсь, это кому-нибудь поможет.

1 голос
/ 30 июня 2011

Я не уверен на 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() дала мне метку и значение доступных опций.

Надеюсь, это поможет.если нет, позвольте мне, где я неправильно понял.Спасибо!

...