2011-01-04 28 views
7

chúng tôi muốn xuất/nhập các sản phẩm có thể định cấu hình thông qua Magento-API trong một hệ thống khác. Điều quan trọng đối với chúng tôi là giá trị của các sản phẩm có thể cấu hình như áo phông có 3 màu (đỏ, lục và lam).Làm thế nào để có được các tùy chọn của một thuộc tính cấu hình trong Magento?

Chúng tôi nhận được các thuộc tính cấu hình với chức năng sau:

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

Nhưng làm thế nào là nó có thể để có được lựa chọn sử dụng trong sản phẩm đó (ea màu xanh, màu xanh lá cây, đỏ nếu các thuộc tính cấu hình là "màu") với nhãn/id hiện có từ thuộc tính có thể định cấu hình mà chúng tôi đã nhận được thông qua hàm trên?

Câu trả lời rất được đánh giá cao!

Tim

+0

Câu hỏi không rõ ràng. Bạn có ý nghĩa gì khi "lấy giá trị đã sử dụng trong sản phẩm đó với nhãn/id hiện có? –

+0

Chúng tôi muốn có các tùy chọn như đỏ, xanh dương và xanh lá cây (nếu thuộc tính có thể định cấu hình là" màu "). Chúng tôi muốn có thông tin về các thuộc tính có thể cấu hình được sử dụng – Tim

+0

Vì vậy, bạn muốn "tùy chọn màu" cho một sản phẩm nhất định [đỏ, xanh lá cây, xanh dương?] –

Trả lời

7

Vì chúng ta không thể tìm thấy một giải pháp tốt hơn, đây là những gì tôi đã đưa ra:

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

Hope this helps bất cứ ai.

+3

Vòng lặp foreach đầu tiên trong hàm này có vẻ hơi dư thừa ... –

1

Tôi không chắc chắn 100% rằng tôi hiểu câu hỏi ... giả sử bạn muốn các giá trị và các nhãn cho các tùy chọn cấu hình cho một sản phẩm cụ thể Tôi giả định này sẽ làm việc (thử nghiệm trên phiên bả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; 
} 

một lần nữa không chắc chắn chính xác những gì bạn đang tìm kiếm, nhưng chức năng $attribute->getProductAttribute()->getSource()->getAllOptions() đã cho tôi nhãn và giá trị tùy chọn khả dụng.

Hy vọng điều này sẽ hữu ích. nếu không, hãy cho tôi biết tôi đã hiểu lầm ở đâu. Cảm ơn!

+0

Cảm ơn bạn, tôi sẽ thử lại và trả lời câu trả lời của bạn! – Tim

Các vấn đề liên quan