I had faced many issues while writing code to upload products in bulk. One of the major challenge was to create custom option. Below is what I did. But I still need to open each products and save again for that to be visible on front end. Any solution will be much appreciated.
> this adds a dropdown option
> option is given in below format(in csv file)
Option1|Option2|Option3
>if there is price difference for each options, the format would be,
Option1|Option2-100|Option3-200
CODE:
if($csv[$oc]!=''){ //$oc is the colum where option value is saved in csv file.
$valarray = array();
$iv = 0;
$so = 1;
foreach(explode('|',$csv[$oc]) as $oval){
$divided = explode('-',$oval);
$sku = strtoupper($title[$oc].'-'.$divided[0]);
if($divided[1]==''){
$price=0;
}else{
$price=$divided[1];
}
$valarray[$iv] = array(
'is_delete' => 0,
'title' => $divided[0],
'price_type' => 'fixed',
'price' => $price,
'sku' => $sku,
'option_type_id'=> -1,
'sort_order' => $so,
);
$iv += 1;
$so += 1;
}
$optionData = array(
'previous_group' => 'text',
'title' => $title[$oc],
'type' => 'drop_down',
'is_require' => 1,
'sort_order' => $oc,
// 'price' => 0,
'price_type' => 'fixed',
'values' => $valarray
)
;
$option = Mage::getModel('catalog/product_option')
->setProductId($product->getId())
->setStoreId($product->getStoreId())
->addData($optionData);
try{
$option->save();
$product->addOption($option);
}
catch(Exception $ex){
print_r('Cound not save option');
}
$product['status']=2;
try{
$product->save();
}
catch(Exception $ex){
print_r('Cound not SAVE option');
}
$product->setHasOptions(1);
$product->save();
}
Another issue that occurred was ending up creating option for products that did not require option. (Later I solved it by adding the first if clause and setting hasoption to 1 for only the products that required it ). How ever i could not find a way to delete options which was created unnecessarily. So I ended up changing is_require option to 0 in the catalog_product_option table (directly editing from mysql database tables).
update catalog_product_option set is_require=0 where product_id>xxx and product_id < yyy;
You also need to set hasoption to 0 for all the products that was created. Fortunately it was all for a single category of products for me.
$_category = Mage::getModel('catalog/category')->load(xxx); // xxx = category id
$catname = $_category->getName();
$collection = $_category->getProductCollection();
$collection->addAttributeToSelect('*');
$result = array();
foreach ($collection as $_product) {
$_prod = Mage::getModel('catalog/product')->load($_product->getId());
$_prod->setHasOptions(0);
$_prod->save();
}
> this adds a dropdown option
> option is given in below format(in csv file)
Option1|Option2|Option3
>if there is price difference for each options, the format would be,
Option1|Option2-100|Option3-200
CODE:
if($csv[$oc]!=''){ //$oc is the colum where option value is saved in csv file.
$valarray = array();
$iv = 0;
$so = 1;
foreach(explode('|',$csv[$oc]) as $oval){
$divided = explode('-',$oval);
$sku = strtoupper($title[$oc].'-'.$divided[0]);
if($divided[1]==''){
$price=0;
}else{
$price=$divided[1];
}
$valarray[$iv] = array(
'is_delete' => 0,
'title' => $divided[0],
'price_type' => 'fixed',
'price' => $price,
'sku' => $sku,
'option_type_id'=> -1,
'sort_order' => $so,
);
$iv += 1;
$so += 1;
}
$optionData = array(
'previous_group' => 'text',
'title' => $title[$oc],
'type' => 'drop_down',
'is_require' => 1,
'sort_order' => $oc,
// 'price' => 0,
'price_type' => 'fixed',
'values' => $valarray
)
;
$option = Mage::getModel('catalog/product_option')
->setProductId($product->getId())
->setStoreId($product->getStoreId())
->addData($optionData);
try{
$option->save();
$product->addOption($option);
}
catch(Exception $ex){
print_r('Cound not save option');
}
$product['status']=2;
try{
$product->save();
}
catch(Exception $ex){
print_r('Cound not SAVE option');
}
$product->setHasOptions(1);
$product->save();
}
Another issue that occurred was ending up creating option for products that did not require option. (Later I solved it by adding the first if clause and setting hasoption to 1 for only the products that required it ). How ever i could not find a way to delete options which was created unnecessarily. So I ended up changing is_require option to 0 in the catalog_product_option table (directly editing from mysql database tables).
update catalog_product_option set is_require=0 where product_id>xxx and product_id < yyy;
You also need to set hasoption to 0 for all the products that was created. Fortunately it was all for a single category of products for me.
$_category = Mage::getModel('catalog/category')->load(xxx); // xxx = category id
$catname = $_category->getName();
$collection = $_category->getProductCollection();
$collection->addAttributeToSelect('*');
$result = array();
foreach ($collection as $_product) {
$_prod = Mage::getModel('catalog/product')->load($_product->getId());
$_prod->setHasOptions(0);
$_prod->save();
}
No comments:
Post a Comment