2016-01-22 165 views
3

我們正在使用擴展程序通過csv文件上傳產品。magento可配置產品未上傳

其簡單的產品工作正常。但是當我們嘗試上傳可配置產品時,它給出了錯誤。

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

回答

1

問題是簡單的產品沒有鏈接到可配置的產品。你需要這個屬性:simples_skus。

在該專欄中,您需要將簡單產品SKU的位置放在每個需要的位置之後:;

希望工程!