Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dynamically adding a file type custom option to multiple products (IDs 100, 101, and 102) in Magento.
<?php
require_once 'app/Mage.php';
Mage::app('default');
$productIds = array(100, 101, 102);
$option = array(
'title' => 'Test Option',
'type' => 'file',
'is_require' => 1,
'price' => 10,
'price_type' => 'fixed',
'sku' => 'testsku',
'file_extension' => 'png,jpg',
'image_size_x' => '100',
'image_size_y' => '200'
);
foreach ($productIds as $productId) {
$product = Mage::getModel('catalog/product')->load($productId);
$optionInstance = $product->getOptionInstance()->unsetOptions();
$product->setHasOptions(1);
if (isset($option['is_require']) && ($option['is_require'] == 1)) {
$product->setRequiredOptions(1);
}
$optionInstance->addOption($option);
$optionInstance->setProduct($product);
$product->save();
}
@shalintripathi

This comment has been minimized.

Copy link

shalintripathi commented Mar 14, 2016

Can you suggest where to add this code .. !! In Magento directory .. !! Do I need to create a separate module and use this logic or this script alone will work.

Thanks

@HeavyGustav

This comment has been minimized.

Copy link

HeavyGustav commented Jul 4, 2016

@shalintripathi
Create a new file names xxx.php under Magento root directory and copy the content into the file.

@selvamesk

This comment has been minimized.

Copy link

selvamesk commented Oct 6, 2016

I created a new file and named it test.php under Magento root directory and copy the content into the file. After that I run www.mysite.com/test.php. Its not working. Custom options didnt added to multiple products.
Can you suggest where I did mistake?

Thanks.

@mano42023

This comment has been minimized.

Copy link

mano42023 commented May 10, 2018

$product->save();
is not working

@Niroshan3

This comment has been minimized.

Copy link

Niroshan3 commented May 25, 2018

Anyone know how to programmatically create add to cart with custom option type file with params.?
Note problem in send file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.