Created
September 28, 2017 15:31
-
-
Save DavaGordon/e64bcb4de4d59e76ebdc987308cc48aa to your computer and use it in GitHub Desktop.
Upload Custom Options By CSV - Magento 1 - Programmatically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$mageFilename = 'app/Mage.php'; | |
require_once $mageFilename; | |
Mage::setIsDeveloperMode(true); | |
ini_set('display_errors', 1); | |
umask(0); | |
Mage::app('admin'); | |
Mage::register('isSecureArea', 1); | |
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); | |
set_time_limit(0); | |
function getOptions($product_id){ | |
return array( | |
array( | |
'title' => 'Option Value', | |
'price' =>0, | |
'price_type' => 'fixed', | |
'sku' => $product_id.'-customsku', | |
'sort_order' => '0' | |
) | |
); | |
} | |
$uploadfile = basename("allproducts.csv"); | |
$csv = new Varien_File_Csv(); | |
$data = $csv->getData('/home/rcare/public_html/'.$uploadfile); //path to csv | |
$message = ''; | |
$count = 1; | |
$obj = Mage::getModel('catalog/product'); | |
foreach($data as $_data){ | |
if($obj->getIdBySku($_data[0])) { | |
$product_id = $obj->getIdBySku($_data[0]); | |
$option = array( | |
'title' => 'Label', | |
'type' => 'checkbox', | |
'is_require' => 1, | |
'sort_order' => 0, | |
'values' => getOptions($product_id) | |
); | |
$product = $obj->load($product_id); | |
$optionInstance = $product->getOptionInstance()->unsetOptions(); | |
$product->setHasOptions(1); | |
$optionInstance->setProduct($product); | |
$product->setProductOptions([$option]); | |
$product->setCanSaveCustomOptions(true); | |
$product->save(); | |
$message .= $product_id . ' Custom VAT Option Added<br />'; | |
unset($product); | |
$count++; | |
} | |
} | |
echo $message; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment