2015-06-25 107 views
1

我正在從csv文件讀取產品sku,並且我的csv文件包含軟件包產品sku。我通過CSV數據和每個包的SKU我要添加里麪包項目,我正在通過CSVMagento如何以編程方式更新軟件包產品

傳遞

這裏穿越是我做了什麼

ini_set('auto_detect_line_endings', TRUE); 
$magentoPath = getcwd(); 
require_once ($magentoPath . '/includes/config.php'); 
require_once ($magentoPath . '/app/Mage.php'); 
Mage::app(); 

//read the csv 
$bundleCsv = Mage::getBaseDir('var').'/import/bundleImport.csv'; 
$rows = array_map('str_getcsv', file($bundleCsv)); 
$header = array_shift($rows); 
$csv = array(); 

foreach ($rows as $row) { 
    $csv[] = array_combine($header, $row); 
} 

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 

echo Mage::app()->getStore()->getId(); exit; 

foreach($csv as $key => $val){ 
    if(!isset($val['sku']) || empty($val['sku']) || $val['sku'] == ''){ 
     echo 'Not Valid Sku'; 
     continue; 
    } 

    $_product = Mage::getModel('catalog/product')->loadByAttribute('sku',$val['sku']); 

    $opt = $val['bundle_options']; 
    $optArr = explode(':', $opt); 

    $bundleOptions = array(); 
    $bundleSelections = array(); 
    foreach ($optArr as $key1 => $val1) { 
     $valTemp = explode('(', $val1); 
     $title = trim($valTemp[0]); 
     $bundleSub[$key1] = array(
       'title' => $title, // option title 
       'option_id' => $key1, 
       'delete' => '', 
       'type' => 'select', // option type 
       'required' => '1', // is option required 
       'position' => '1' // option position 
     ); 

     $skuStr = trim($valTemp[1]); 
     $skuStrTemp = explode(')', $skuStr); 
     $skuStr = trim($skuStrTemp[0]); 

     $skuTemp = explode('+', $skuStr); 

     foreach($skuTemp as $key2 => $val2){ 
      $product = Mage::getModel('catalog/product'); 
      $id = Mage::getModel('catalog/product')->getResource()->getIdBySku($val2); 

      if($id){ 
       $bundleSelectionsSub[$key2] = array (// selection ID of the option (first product under this option (option ID) would have ID of 0, second an ID of 1, etc) 
         'product_id' => $id, // if of a product in selection 
         'delete' => '', 
         'selection_price_value' => '10', 
         'selection_price_type' => 0, 
         'selection_qty' => 1, 
         'selection_can_change_qty' => 0, 
         'position' => 0, 
         'is_default' => 1 
       ); 
       $product = null; 
      }else{ 
       continue; 
      } 
     } 
     $bundleSelections[$key1] = $bundleSelectionsSub; 
    } 

    $bundleOptions = $bundleSub; 
    //echo '<pre>'; print_r($bundleOptions); exit; 
    try{ 

     $_product->setCanSaveCustomOptions (true); 
     $_product->setCanSaveBundleSelections (true); 
     $_product->setAffectBundleProductSelections (true); 

     $_product->setBundleOptionsData ($bundleOptions); 
     $_product->setBundleSelectionsData ($bundleSelections); 

     $_product->save(); 
    }catch (Exception $e) { 
     Mage::log ($e->getMessage()); 
     echo $e->getMessage(); 
    } 

    echo 1; exit; 
    $_product = null; 

} 

的代碼,但這個給了我下面的錯誤作爲

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`aero_dev`.`catalog_product_bundle_option_value`, CONSTRAINT `FK_CAT_PRD_BNDL_OPT_VAL_OPT_ID_CAT_PRD_BNDL_OPT_OPT_ID` FOREIGN KEY (`option_id`) REFERENCES `catalog_product_bundle_option` (`opt), query was: INSERT INTO `catalog_product_bundle_option_value` (`option_id`, `store_id`, `title`) VALUES (?, ?, ?) 

任何幫助,將不勝感激。

+0

嘗試不使用'option_id'=> $ key1, –

+0

或使它=>''另請看看這個:http://stackoverflow.com/questions/3108775/programmatically-add-bundle-products-in -magento-using-the-sku-id-of-simple-it –

+0

它沒有爲我開火,所以我爲此編寫了自定義查詢 – rajatsaurastri

回答

1

我無法使用上述方法使它工作,所以我嘗試編寫自定義查詢以將捆綁項目放入現有捆綁軟件產品中。當我查看分貝時,我發現基本上有3個表涉及創建捆綁項目。這些都是

  • catalog_product_bundle_option
  • catalog_product_bundle_option_value
  • catalog_product_bundle_selection

我通過這些表去,並試圖尋找是哪個Magento提出如果我創建Magento管理捆綁項目。

所以經過一番研究,我已經做了類似的東西 -

foreach($csv as $key => $val){ 
    if(!isset($val['sku']) || empty($val['sku']) || $val['sku'] == ''){ 
     echo 'Not Valid Sku'; 
     continue; 
    } 

    $_product = Mage::getModel('catalog/product')->loadByAttribute('sku',trim($val['sku'])); 

    $_product->setCanSaveCustomOptions (true); 
    $_product->setCanSaveBundleSelections (true); 
    $_product->setAffectBundleProductSelections (true); 

    $opt = $val['bundle_options']; 
    $optArr = explode(':', $opt); 

    //get the db write connection 
    $connection = Mage::getSingleton('core/resource')->getConnection('core_write'); 
    $connection->beginTransaction(); 

    foreach ($optArr as $key1 => $val1) { 
     $valTemp = explode('(', $val1); 
     $title = trim($valTemp[0]); 

     //insert into catalog_product_bundle_option with parent product id and type 
     $__fields = array(); 
     $__fields['parent_id'] = $_product->getId(); 
     $__fields['required'] = 1; 
     $__fields['type'] = 'select'; 
     $__fields['position'] = $key1+1; 
     $connection->insert($catalog_product_bundle_option, $__fields); 
     $opt_id = $connection->lastInsertId(); 
     $connection->commit(); 

     //inert into catalog_product_bundle_option_value with option id, store id, title 
     $__fields = array(); 
     $__fields['option_id'] = $opt_id; 
     $__fields['store_id'] = 0; 
     $__fields['title'] = $title; 
     $connection->insert($catalog_product_bundle_option_value, $__fields); 
     $val_id = $connection->lastInsertId(); 
     $connection->commit(); 

     $skuStr = trim($valTemp[1]); 
     $skuStrTemp = explode(')', $skuStr); 
     $skuStr = trim($skuStrTemp[0]); 

     $skuTemp = explode('+', $skuStr); 
     $pos = 1; 
     foreach($skuTemp as $key2 => $val2){ 
      $id = Mage::getModel('catalog/product')->getResource()->getIdBySku($val2); 

      //insert into catalog_product_bundle_selection with option_id, parent product id, product id, position, is_default, selection_price_type, selection_price_value, selection_qty, selection_can_change_qty 
      $__fields = array(); 
      $__fields['option_id'] = $opt_id; 
      $__fields['parent_product_id'] = $_product->getId(); 
      $__fields['product_id'] = $id; 
      $__fields['position'] = $pos + 1; 
      $__fields['selection_price_type'] = 0; 
      $__fields['selection_price_value'] = 10; 
      $__fields['selection_qty'] = 1; 
      $__fields['selection_can_change_qty'] = 0; 
      $connection->insert($catalog_product_bundle_selection, $__fields); 
      $connection->commit(); 
      $pos++; 
     } 
    } 

    //update product 
    $_product->save(); 
    $_product = null; 

} 

我的CSV包含2列一個SKU,另一種是捆綁選項 例子 - SKU - 12345678 捆綁選項 - item01(ZIPLOCK18X24 + ZIPLOCK16X20) :item02(ZIPLOCK14X20 + XEROMOCR84208X11)

其中item01是選項標題,後面是簡單商品sku ZIPLOCK18X24,ZIPLOCK16X20和:多個選項標題的分隔單位。

我希望它可以幫助別人。

相關問題