我阅读了Csv文档中的产品库,我的剪辑含有 b产品库。 我正在通过剪辑数据,每张 b子的ku子,我想在它内添加我通过CSV通过的捆绑物品。
在这方面,我已经做了些什么。
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 (?, ?, ?)
Any help would be appreciated.