我们正在使用一个扩展程序通过CSV文件上传产品。
对于简单的产品而言,它可以正常工作。但当我们尝试上传可配置产品时,它会出现错误。
Error : Skip import row, product with duplicate attribute combination
在下面的代码中,出现了上述错误信息。我想知道Excel表格中有什么问题,导致出现这个错误:
for ($i = $totalProcessedRows + 1; $i <= sizeof($data); $i++) {
if(strtolower($data[$i][0]) != "configurable" && $data[$i][0] != "") {
$attributeOptionCheck[] = $data[$i][10];
$associatedSkuCheck[] = $data[$i][5];
$totalProcessedRows++;
} else {
if(count($attributeOptionCheck) != count(array_unique($attributeOptionCheck)))
$errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, product with duplicate attribute combination');
elseif(count($associatedSkuCheck) != count(array_unique($associatedSkuCheck)))
$errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row,associated products with duplicate skus');
break;
}
}
这是可配置产品的完整功能。
public function importConfigurableProducts($profileId,$rowCount) {
$helper = Mage::helper("mpmassuploadaddons");
$collection = Mage::getModel('mpmassuploadaddons/profilesession')->getCollection()
->addFieldToFilter('session_id',array('eq' => $profileId));
$result = array();
$data = array();
$csvFile = "";
$imgDir = "";
if (count($collection) > 0) {
foreach ($collection as $value) {
$csvFile = $value->getCsvFile();
$imgDir = $value->getImageFile();
}
$targetPath = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId;
$csvPath = $targetPath.'/'.$csvFile;
$fp = fopen($csvPath, 'r');
while(!feof($fp) ){
$data[] = fgetcsv($fp, 1024);
}
fclose($fp);
$info = $data[$rowCount];
$totalProcessedRows = $rowCount;
$attributeOptionCheck = array();
$associatedSkuCheck = array();
$associatedSkuCheck[] = $info[5];
$errors = array();
for ($i = $totalProcessedRows + 1; $i <= sizeof($data); $i++) {
if(strtolower($data[$i][0]) != "configurable" && $data[$i][0] != "") {
$attributeOptionCheck[] = $data[$i][10];
$associatedSkuCheck[] = $data[$i][5];
$totalProcessedRows++;
} else {
if(count($attributeOptionCheck) != count(array_unique($attributeOptionCheck)))
$errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, product with duplicate attribute combination');
elseif(count($associatedSkuCheck) != count(array_unique($associatedSkuCheck)))
$errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row,associated products with duplicate skus');
break;
}
}
if(empty($errors)) {
$wholedata = array(
'category' => $info[1],
'name' => $info[2],
'description' => $info[3],
'short_description' => $info[4],
'sku' => $info[5],
'price' => $info[6],
'tax_class_id' => $info[10],
'is_in_stock' => $info[11],
'_super_attribute_code' => $info[12],
'images' => $info[17]
);
// $wholedata = array(
// 'category' => $info[0],
// 'name' => $info[1],
// 'description' => $info[2],
// 'short_description' => $info[3],
// 'sku' => $info[4],
// 'price' => $info[5],
// 'tax_class_id' => $info[9],
// 'is_in_stock' => $info[10],
// 'stock' => $info[11],
// 'weight' => $info[12],
// 'images' => $info[13]
// );
if(isset($info[6])){
$specialdata=array(
'special_price' => $info[7],
'special_from_date' => $info[8],
'special_to_date' => $info[9]
);
$wholedata=array_merge($wholedata,$specialdata);
}
if(isset($info[20])){
$systemattribute=$data[0][20];
$othersystemattribute=array(
$systemattribute => $info[20]
);
$wholedata=array_merge($wholedata,$othersystemattribute);
}
list($wholedata, $errors) = $this->validatePost($wholedata);
if(empty($errors)) {
$customAttributeData = array();
$mediaTypeAttributedata = array();
if(isset($data[0][18])) {
$customAttributeCodes = explode('(', $data[0][18]);
if($customAttributeCodes[0] != "") {
list($customAttributeData, $errors) = $this->validateCustomAttributeValues($customAttributeCodes[0],$info[18]);
$mediaTypeAttributedata = $this->getMediaImageAttributesData($customAttributeCodes[0],$info[18]);
}
}
if(empty($errors)) {
$j = $rowCount;
for ($i = $j + 1; $i <= sizeof($data); $i++) {
if(strtolower($data[$i][0]) != "configurable" && $data[$i][0] != "") {
$associateWholedata = array(
'type_id' => $data[$i][0],
'name' => $data[$i][2],
'sku' => $data[$i][5],
'_super_attribute_code' => $data[$i][12],
'_super_attribute_option' => $data[$i][13],
'_super_attribute_price_corr' => $data[$i][14],
'_super_attribute_qty' => $data[$i][15],
'_super_attribute_weight' => $data[$i][16]
);
list($associateWholedata, $errors) = $this->validatePost($associateWholedata);
if(!empty($errors))
break;
} else {
break;
}
}
if(empty($errors)) {
$profile = Mage::getModel('mpmassuploadaddons/profilesession')->load($profileId);
$additionalInfo = array(
'type_id' => 'configurable',
'userid' => $profile->getSellerId(),
'wstoreids' => Mage::app()->getStore()->getStoreId(),
'attribute_set_id' => $profile->getAttributesetId()
);
$wholedata = array_merge($wholedata,$additionalInfo);
$proid = $this->saveConfigNewProduct($wholedata);
$objprod = Mage::getModel('catalog/product')->load($proid);
$websites = array();
foreach(Mage::app()->getWebsites(true) as $website) {
$websites[] = $website->getId();
}
$objprod->setWebsiteIds($websites)->save();
$marketplaceProduct = Mage::getModel('marketplace/product')->getCollection()
->addFieldToFilter("mageproductid",array("eq" => $proid))
->getFirstItem();
$marketplaceProduct->setUserid($profile->getSellerId())->save();
$totalProcessedRows = $rowCount;
$count = 1;
for ($i = $totalProcessedRows + 1; $i <= sizeof($data); $i++) {
if(strtolower($data[$i][0]) != "configurable" && $data[$i][0] != "") {
$associateWholedata = array(
'type_id' => 'simple',
'name' => $data[$i][2],
'sku' => $data[$i][5],
'stock' => $data[$i][15],
'weight' => $data[$i][16],
'status' => 1,
'visibility' => 1,
'mainid' => $proid
);
$configProduct = Mage::getModel("catalog/product")->load($proid);
$associatedPrice = $configProduct->getPrice();
$configattr = Mage::getModel('catalog/product_type_configurable')->getConfigurableAttributesAsArray($configProduct);
foreach (explode(',', $data[$i][12]) as $key => $attribute) {
$optionIds = explode(',', $data[$i][13]);
$optionId = $this->getAttributeOptionIdbyOptionText($attribute , $optionIds[$key]);
$optionPrice = explode(',', $data[$i][14]);
$associatedPrice = $associatedPrice + $optionPrice[$key];
foreach ($configattr as $value) {
if($value['attribute_code'] == $attribute) {
$optionString = $attribute."|price|".$value['id']."|".$optionId;
}
}
$associateWholedata[$attribute] = $optionId;
$associateWholedata[$optionString] = $optionPrice[$key];
$associateWholedata['price'] = $associatedPrice;
}
$this->quickcreate($associateWholedata);
$totalProcessedRows++;
$count++;
} else {
break;
}
}
if(isset($info[17]) && $info[17] != "") {
$images = array_reverse(explode(',',$info[17]));
foreach ($images as $image) {
$objprod = Mage::getModel('catalog/product')->load($proid);
$imgp = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId."/".$imgDir."/";
$checkimg = glob($imgp . $image);
if(count($checkimg) != 0) {
$filepath = $imgp.$image;
$objprod->addImageToMediaGallery($filepath, array ('image','small_image','thumbnail'), true, false);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$objprod->save();
}
}
}
try {
Mage::dispatchEvent('mp_add_customattribute_mass', array('product_id' => $proid,'customattribute' => $customAttributeData));
if(Mage::getModel('customattribute/customattribute')) {
if(count($mediaTypeAttributedata)) {
foreach ($mediaTypeAttributedata as $imageType => $image) {
if($image != "") {
$objprod = Mage::getModel('catalog/product')->load($proid);
$imgp = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId."/".$imgDir."/";
$checkimg = glob($imgp . $image);
if(count($checkimg) != 0) {
$filepath = $imgp.$image;
$objprod->addImageToMediaGallery($filepath, array ($imageType), true, false);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$objprod->save();
}
}
}
}
}
} catch(Exception $e) {
}
if(isset($info[19]) && $info[19] != "") {
$wholedata['id'] = $proid;
$data = json_decode($info[19],true);
$i = 0;
foreach($data as $k => $d) {
foreach ($d as $key => $value) {
if($key == 'values') {
$wholedata['selectoptions'][$i] = $d[$key];
} else {
$wholedata['options'][$i][$key] = $value;
$wholedata['options'][$i]['customoptindex'] = $i;
}
}
$i++;
}
Mage::dispatchEvent('mp_customoption_setdata', $wholedata);
}
$result['savedRows'] = $count;
$result['rowsCount'] = $totalProcessedRows;
$result['success'] = $helper->__("successfully saved.");
} else {
$result['savedRows'] = 0;
$result['rowsCount'] = $totalProcessedRows;
$result['errors'] = current($errors);
}
} else {
$result['savedRows'] = 0;
$result['rowsCount'] = $totalProcessedRows;
$result['errors'] = current($errors);
}
} else {
$result['savedRows'] = 0;
$result['rowsCount'] = $totalProcessedRows;
$result['errors'] = current($errors);
}
} else {
$result['savedRows'] = 0;
$result['rowsCount'] = $totalProcessedRows;
$result['errors'] = current($errors);
}
}
return $result;
}
可配置产品的另一个相关功能:
public function getConfigurableSuperAttributes() {
$allowedSets = explode(',',Mage::getStoreConfig('marketplace/marketplace_options/attributesetid'));
$attributeCodes = array();
foreach ($allowedSets as $attributeSetId) {
$attributes = Mage::getModel('catalog/product_attribute_api')->items($attributeSetId);
$attributeCodes[$attributeSetId] = array();
$temp = array();
foreach($attributes as $attribute) {
$data = Mage::getModel('catalog/resource_eav_attribute')->load($attribute['attribute_id']);
if($data['frontend_input'] == 'select' && $data['is_user_defined'] == 1 && $data['is_configurable'] == 1&& $data['is_visible'] == 1 && $data['is_global'] == 1) {
array_push($temp, $data['attribute_code']);
}
}
$attributeCodes[$attributeSetId] = implode(",",$temp);
}
return json_encode($attributeCodes);
}
这是该文件的完整代码:http://pastebin.com/vZsf0kZG