Magento SOAP API v2将简单产品链接到可配置产品

时间:2014-10-03 16:00:51

标签: php xml api magento magento-1.8

我需要帮助处理我正在开发的开发项目。我正在使用扩展程序https://github.com/jreinke/magento-improve-api进行Magento 1.8安装,以扩展Magento的API,通过实体店的销售点系统将简单产品与可配置产品相​​关联。

我们使用三个可配置的属性:颜色,大小和&选项

有些产品使用所有三种可配置属性,但许多产品没有。例如:T恤使用颜色&大小属性但不使用选项属性。

我遇到的问题是,当通过扩展API创建不使用所有三个可配置属性的可配置产品时,可配置产品不会与其简单产品相关联。它没有关联的原因是因为可配置产品是使用与之关联的所有三个可配置属性创建的,但简单产品只有两个:size&颜色。

以下是正在使用的代码:

来自: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);
    }

来自: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);
}

来自: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;
}

以下是使用的可配置产品XML的示例:

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

1 个答案:

答案 0 :(得分:0)

您需要在xml中添加configurable_attributes标记。 请从以下位置替换xml中的代码:

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

为:

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

希望这有帮助。