Upload Custom Options In Magento Version 1.x using CSV File - Bulk Upload

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);
    }  
  
?>

Rakesh Singh Uniyal

I’m Rakesh Singh Uniyal (MCA) and I write to help people work on programming and technology. The tips, tutorials and information provided in this blog has helped many people to solve their programming and web development related issues.
I work as a Freelance PHP/Magento/Wordpress Developer.

No comments:

Post a Comment