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

Posted

技术标签:

【中文标题】Magento SOAP API v2 将简单产品链接到可配置产品【英文标题】:Magento SOAP API v2 linking simple products to configurable product 【发布时间】:2014-11-28 17:22:32 【问题描述】:

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

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

有些产品使用所有三个可配置属性,但许多产品不使用。例如:T 恤使用颜色和尺寸属性,但不使用选项属性。

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

这是正在使用的代码:

来自: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】:

你需要在你的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>

希望这会有所帮助。

【讨论】:

尝试了这个解决方案,结果是创建可配置产品时分配给它的可配置属性为零。 :(

以上是关于Magento SOAP API v2 将简单产品链接到可配置产品的主要内容,如果未能解决你的问题,请参考以下文章

Magento 2 SOAP API WSDL 匿名访问

Magento Soap API在本地工作但不是远程工作

SOAPEngine 的 completeWithDictionary 为空

如何在 Magento 中使用 REST API 获取产品信息

Magento:通过 API 删除产品后更新购物车总数

用于添加产品愿望清单的 Magento 自定义 API