简体   繁体   English

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

[英]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. 我正在使用扩展名https://github.com/jreinke/magento-improve-api进行Magento 1.8安装,以扩展Magento的API,从而通过实体店的销售点系统将简单产品与可配置产品相​​关联。

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. 例如:T恤使用颜色和尺寸属性,但不使用选项属性。

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. 我遇到的问题是,当通过扩展的API创建了不使用所有三个可配置属性的可配置产品时,该可配置产品未与其简单产品相关联。 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 来自: 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 来自:Api /模型/目录/产品/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 来自: 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: 这是使用的可配置产品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>

You need to add configurable_attributes tag in your xml. 您需要在xml中添加configurable_attributes标记。 Please replace your code in xml from: 请从以下位置替换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>

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. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM