Here we will create a custom code to upload custom option in magento 1.x version using csv file.
This is easy option to bulk custom option upload method.
<?php
require_once('app/Mage.php');
umask(0);
Mage::app();
$app = Mage::app('default');
Mage::getSingleton('core/session', array('name' => 'adminhtml'));
Mage::app()->setCurrentStore(0);
function getOptions(){
return array(
array(
'title' => 'option_one',
'price' => 'option_one_price',
'price_type' => 'fixed',
'sort_order' => '1'
),
array(
'title' => 'option_two',
'price' => 'option_two_price',
'price_type' => 'fixed',
'sort_order' => '2'
)
);
}
$option = array(
'title' => 'Capacity',
'type' => 'drop_down', // could be drop_down ,checkbox , multiple
'is_require' => 1,
'sort_order' => 0,
'values' => getOptions()
);
$obj = Mage::getModel('catalog/product');
$row = 1;
// Create a csv file custom_option.csv with sku codes
if (($handle = fopen("custom_option.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($row == 1){ $row++; continue; }
$num = count($data);
$row++;
$sku = $data[0];
$product_id = $obj->getIdBySku($sku);
$product = $obj->load($product_id);
$optionInstance = $product->getOptionInstance()->unsetOptions();
$product->setHasOptions(1);
$optionInstance->addOption($option);
$optionInstance->setProduct($product);
$product->save();
unset($product);
echo "Done";
}
fclose($handle);
}
?>
This is easy option to bulk custom option upload method.
<?php
require_once('app/Mage.php');
umask(0);
Mage::app();
$app = Mage::app('default');
Mage::getSingleton('core/session', array('name' => 'adminhtml'));
Mage::app()->setCurrentStore(0);
function getOptions(){
return array(
array(
'title' => 'option_one',
'price' => 'option_one_price',
'price_type' => 'fixed',
'sort_order' => '1'
),
array(
'title' => 'option_two',
'price' => 'option_two_price',
'price_type' => 'fixed',
'sort_order' => '2'
)
);
}
$option = array(
'title' => 'Capacity',
'type' => 'drop_down', // could be drop_down ,checkbox , multiple
'is_require' => 1,
'sort_order' => 0,
'values' => getOptions()
);
$obj = Mage::getModel('catalog/product');
$row = 1;
// Create a csv file custom_option.csv with sku codes
if (($handle = fopen("custom_option.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($row == 1){ $row++; continue; }
$num = count($data);
$row++;
$sku = $data[0];
$product_id = $obj->getIdBySku($sku);
$product = $obj->load($product_id);
$optionInstance = $product->getOptionInstance()->unsetOptions();
$product->setHasOptions(1);
$optionInstance->addOption($option);
$optionInstance->setProduct($product);
$product->save();
unset($product);
echo "Done";
}
fclose($handle);
}
?>
No comments:
Post a Comment