The Smart Dude
The Smart Dude

Reputation: 11

Magento SOAP API v2 linking simple products to configurable product

I need help with a development project that I am working on. I am working with a Magento 1.8 install using the extension https://github.com/jreinke/magento-improve-api to extend Magento’s API to associate simple products to configurable products via a brick and mortar store’s Point of Sale system.

We use three configurable attributes: color, size, & option

Some products use all three configurable attributes but many products do not. For example: t-shirts use the color & size attributes but do not use the option attribute.

The issue that I am experiencing is that when a configurable product that does not use all three of the configurable attributes is created via the extended API the configurable product does not associate with its simple products. The reason it is not associating is because the configurable product is being created with all three configurable attributes associated with it but the simple products only has the two: size & color.

Here is the code that is being used:

from: Api/Model/Catalog/Product/Api/V2.php

    if (property_exists($productData, 'additional_attributes')) {
        $singleDataExists = property_exists((object) $productData->additional_attributes, 'single_data');
        $multiDataExists = property_exists((object) $productData->additional_attributes, 'multi_data');
        if ($singleDataExists || $multiDataExists) {
            if ($singleDataExists) {
                foreach ($productData->additional_attributes->single_data as $_attribute) {
                    $_attrCode = $_attribute->key;
                    $productData->$_attrCode = Mage::helper('bubble_api/catalog_product')
                        ->getOptionKeyByLabel($_attrCode, $_attribute->value);
                }
            }
            if ($multiDataExists) {
                foreach ($productData->additional_attributes->multi_data as $_attribute) {
                    $_attrCode = $_attribute->key;
                    $productData->$_attrCode = Mage::helper('bubble_api/catalog_product')
                        ->getOptionKeyByLabel($_attrCode, $_attribute->value);
                }
            }
        } else {
            foreach ($productData->additional_attributes as $_attrCode => $_value) {
                $productData->$_attrCode = Mage::helper('bubble_api/catalog_product')
                    ->getOptionKeyByLabel($_attrCode, $_value);
            }
        }
        unset($productData->additional_attributes);
    }

from: Api/Model/Catalog/Product/Api.php

    public function create($type, $set, $sku, $productData, $store = null)
{
    // Allow attribute set name instead of id
    if (is_string($set) && !is_numeric($set)) {
        $set = Mage::helper('bubble_api')->getAttributeSetIdByName($set);
    }

    return parent::create($type, $set, $sku, $productData, $store);
}

from: Api/Helper/Catalog/Product.php

    protected function _initConfigurableAttributesData(Mage_Catalog_Model_Product $mainProduct, $simpleProductIds, $priceChanges = array(), $configurableAttributes = array())
{
    if (!$mainProduct->isConfigurable() || empty($simpleProductIds)) {
        return $this;
    }

    $mainProduct->setConfigurableProductsData(array_flip($simpleProductIds));
    $productType = $mainProduct->getTypeInstance(true);
    $productType->setProduct($mainProduct);
    $attributesData = $productType->getConfigurableAttributesAsArray();

    if (empty($attributesData)) {
        // Auto generation if configurable product has no attribute
        $attributeIds = array();
        foreach ($productType->getSetAttributes() as $attribute) {
            if ($productType->canUseAttribute($attribute)) {
                $attributeIds[] = $attribute->getAttributeId();
            }
        }
        $productType->setUsedProductAttributeIds($attributeIds);
        $attributesData = $productType->getConfigurableAttributesAsArray();
    }
    if (!empty($configurableAttributes)){
        foreach ($attributesData as $idx => $val) {
            if (!in_array($val['attribute_id'], $configurableAttributes)) {
                unset($attributesData[$idx]);
            }
        }
    }

    $products = Mage::getModel('catalog/product')->getCollection()
        ->addIdFilter($simpleProductIds);

    if (count($products)) {
        foreach ($attributesData as &$attribute) {
            $attribute['label'] = $attribute['frontend_label'];
            $attributeCode = $attribute['attribute_code'];
            foreach ($products as $product) {
                $product->load($product->getId());
                $optionId = $product->getData($attributeCode);
                $isPercent = 0;
                $priceChange = 0;
                if (!empty($priceChanges) && isset($priceChanges[$attributeCode])) {
                    $optionText = $product->getResource()
                        ->getAttribute($attribute['attribute_code'])
                        ->getSource()
                        ->getOptionText($optionId);
                    if (isset($priceChanges[$attributeCode][$optionText])) {
                        if (false !== strpos($priceChanges[$attributeCode][$optionText], '%')) {
                            $isPercent = 1;
                        }
                        $priceChange = preg_replace('/[^0-9\.,-]/', '', $priceChanges[$attributeCode][$optionText]);
                        $priceChange = (float) str_replace(',', '.', $priceChange);
                    }
                }
                $attribute['values'][$optionId] = array(
                    'value_index' => $optionId,
                    'is_percent' => $isPercent,
                    'pricing_value' => $priceChange,
                );
            }
        }
        $mainProduct->setConfigurableAttributesData($attributesData);
    }

    return $this;
}

Here is an example of the configurable product XML used:

    <soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:Magento" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
  <soapenv:Header />
  <soapenv:Body>
    <urn:catalogProductCreate soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <sessionId xsi:type="xsd:string">93de69dab0c0ba19b00a328808ce3d8a</sessionId>
  <type xsi:type="xsd:string">configurable</type>
  <set xsi:type="xsd:string">4</set>
  <sku xsi:type="xsd:string">WN0016</sku>
  <productData xsi:type="urn:catalogProductCreateEntity">
    <name xsi:type=“xsd:string">example product</name>
    <description xsi:type="xsd:string">description.</description>
    <short_description>short description</short_description>
    <weight xsi:type="xsd:string">0.50</weight>
    <status xsi:type="xsd:string">2</status>
    <price>39.99</price>
    <visibility xsi:type="xsd:string">4</visibility>
    <tax_class_id xsi:type="xsd:string">2</tax_class_id>
    <category_ids xsi:type="urn:ArrayOfString" soapenc:arrayType="xsd:string[]">
      <key>category_id</key>
      <value>60</value>
      <key>category_id</key>
      <value>7</value>
      <key>category_id</key>
      <value>0</value>
    </category_ids>
    <additional_attributes xsi:type="urn:catalogProductAdditionalAttributesEntity">
      <single_data xsi:type="urn:associativeArray" soapenc:arrayType="urn:associativeEntity[]">
        <associativeArray>
          <key>color</key>
          <value></value>
        </associativeArray>
        <associativeArray>
          <key>size</key>
          <value></value>
        </associativeArray>
      </single_data>
    </additional_attributes>
    <associated_skus xsi:type="urn:ArrayOfString" soapenc:arrayType="xsd:string[]">
      <value>9214018662</value>
    </associated_skus>
    <associated_skus xsi:type="urn:ArrayOfString" soapenc:arrayType="xsd:string[]">
      <value>9214018664</value>
    </associated_skus>
    <associated_skus xsi:type="urn:ArrayOfString" soapenc:arrayType="xsd:string[]">
      <value>9214018658</value>
    </associated_skus>
    <associated_skus xsi:type="urn:ArrayOfString" soapenc:arrayType="xsd:string[]">
      <value>9214018665</value>
    </associated_skus>
    <associated_skus xsi:type="urn:ArrayOfString" soapenc:arrayType="xsd:string[]">
      <value>9214018663</value>
    </associated_skus>
    <associated_skus xsi:type="urn:ArrayOfString" soapenc:arrayType="xsd:string[]">
      <value>9214018660</value>
    </associated_skus>
    <associated_skus xsi:type="urn:ArrayOfString" soapenc:arrayType="xsd:string[]">
      <value>9214018659</value>
    </associated_skus>
    <associated_skus xsi:type="urn:ArrayOfString" soapenc:arrayType="xsd:string[]">
      <value>9214018657</value>
    </associated_skus>
      </productData>
      <storeView xsi:type="xsd:string">1</storeView>
    </urn:catalogProductCreate>
  </soapenv:Body>
</soapenv:Envelope>

Upvotes: 0

Views: 1661

Answers (1)

Jaimin Sutariya
Jaimin Sutariya

Reputation: 252

You need to add configurable_attributes tag in your xml. Please replace your code in xml from:

<additional_attributes xsi:type="urn:catalogProductAdditionalAttributesEntity">
  <single_data xsi:type="urn:associativeArray" soapenc:arrayType="urn:associativeEntity[]">
    <associativeArray>
      <key>color</key>
      <value></value>
    </associativeArray>
    <associativeArray>
      <key>size</key>
      <value></value>
    </associativeArray>
  </single_data>
</additional_attributes>

to:

<configurable_attributes xsi:type="urn:ArrayOfString" soapenc:arrayType="xsd:string[]">
    <value>color</value>
</configurable_attributes>
<configurable_attributes xsi:type="urn:ArrayOfString" soapenc:arrayType="xsd:string[]">
    <value>size</value>
</configurable_attributes>

Hope this helps.

Upvotes: 0

Related Questions