我需要關於我正在開發的開發項目的幫助。我正在使用Magento 1.8安裝,使用擴展https://github.com/jreinke/magento-improve-api來擴展Magento的API,以通過實體店的銷售點系統將簡單產品與可配置產品相關聯。Magento SOAP API v2將簡單產品鏈接到可配置產品
我們使用三種配置屬性:顏色,大小,&選項
有些產品使用這三種配置的屬性,但很多產品沒有。例如: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.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>
嘗試此解決方案的結果是,可配置產品是使用分配給它的零配置屬性創建的。 :( – 2014-10-09 15:31:13