2011-01-04 18 views
7

queremos exportar/importar productos configurables a través de la API de Magento en otro sistema. Lo que es importante para nosotros, son los valores de los productos configurables, como una camiseta que tiene 3 colores (rojo, verde y azul).¿Cómo obtener las opciones de un atributo configurable en Magento?

Recibimos los atributos configurables con la siguiente función:

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

Pero ¿cómo es posible obtener las opciones utilizadas en ese producto (EA azules, verdes, rojos si el atributo configurable es "color") con la etiqueta/id ahora disponible del atributo configurable que obtuvimos a través de la función anterior?

¡Las respuestas son muy apreciadas!

Tim

+0

La pregunta no está clara. ¿A qué te refieres con "obtener los valores utilizados en ese producto con la etiqueta/id disponible ahora? –

+0

Queremos obtener las opciones como rojo, azul y verde (si el atributo configurable es" color "). Y con lo mencionado anteriormente funtion obtenemos la información sobre los atributos configurables utilizados. – Tim

+0

¿Desea las "opciones de color" para un producto determinado [rojo, verde, azul]? –

Respuesta

7

Ya que no pudimos encontrar una solución mejor, esto es lo que ocurrió:

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

Espero que esto ayude a nadie.

+3

El primer bucle Foreach en esta función parece un poco redundante ... –

1

No estoy 100% seguro de que entiendo la pregunta ... suponiendo que desea que los valores y etiquetas para las opciones configurables para un producto específico Asumo que esto funcionaría (probado en la versión 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; 
} 

de nuevo no estoy seguro exactamente lo que busca, pero la función $attribute->getProductAttribute()->getSource()->getAllOptions() me dio las opciones de etiqueta y valor disponible.

Espero que esto ayude. si no, déjame donde lo malentendí. ¡Gracias!

+0

Gracias. Lo intentaré y vuelvo a su respuesta! – Tim

Cuestiones relacionadas