Magento Forum

   
Adding/removing values from a product custom option programmatically
 
miah
Jr. Member
 
Total Posts:  6
Joined:  2011-05-05
 

Hi,

I’ve been trying to update a large number of products in Magento 1.3.3.0 - specifically I need to check each one for a custom option (a drop down list) and if a specific value is missing from that list, I need to then add it in.

So far I’ve managed to successfully get the list of products I’m interested in, check each one for the options I’m after, but after a lot of searching and trying various things, I can’t figure out how to update the values.

In the code snippet below, I can successfully update the title of a non-drop down list, but if I try to update anything on a drop-down list option (title, add a value, etc), I get an exception:

Fatal error: Uncaught exception ‘Mage_Core_Exception’ with message ‘Select type options required values rows.’ in .../magento/app/Mage.php:407
Stack trace:
#0 .../magento/app/code/core/Mage/Catalog/Model/Product/Option.php(295): Mage::throwException(’Select type opt...’)
#1 .../magento/app/code/core/Mage/Core/Model/Abstract.php(252): Mage_Catalog_Model_Product_Option->_afterSave()
#2 .../magento-product-update.php(113): Mage_Core_Model_Abstract->save()
#3 {main}
thrown in .../magento/app/Mage.php on line 407

I’m really at a bit of a loss with this one, if anyone can point me in the right direction, or even just validate that my approach is solid, that would be much appreciated!

Summarised code is as follows:

$magePath = ‘./magento/app/Mage.php’;
require_once $magePath;

Varien_Profiler::enable();
Mage::setIsDeveloperMode(true);
ini_set(’display_errors’, 1);

umask(0);

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$product_ids = array(1,2,3);

$productmodel = Mage::getModel(’catalog/product’);

foreach ($products as $product_id) {
$product = $productmodel->load($product_id);

$options = $product->getProductOptionsCollection();

if (isset($options)) {
foreach ($options as $o) {
$title = $o->getTitle();

if ($title == “Line 1/Ftont") {
$o->setTitle("Line 1/Front");
$o->save();
}

$optionType = $o->getType();
if ($optionType == “drop_down") {
$values = $o->getValuesCollection();
$found = false;
foreach ($values as $k => $v) {
if (1 == preg_match("/myoptionname$/i", $v->getTitle())) {
$found = true;
}
}
if ($found) {
$missingvalue = false;
foreach ($values as $k => $v) {
if (1 == preg_match("/^valueiwant/i", $v->getTitle())) {
$missingvalue = true;
}
}
if (!$goldribbon) {
// $value = Mage::getModel(’catalog/product_option_value’);
// $value->setOption($o)
// ->setTitle(’valueiwant’)
// ->setPrice(1);
// $o->addValue($value);
// $o->save();
}
}
}
}
}
}

 
Magento Community Magento Community
Magento Community
Magento Community
 
miah
Jr. Member
 
Total Posts:  6
Joined:  2011-05-05
 
arabgento - 06 November 2011 04:14 PM

hi miah
i Worked approximately 5 to 6 hours to find a solution
To your problem
And this gift for you in the Muslim Holiday
Code works a hundred percent
You will not find this on the web or in the wiki or doc
the code worked perfectly for me

Hi - cheers for this, much appreciated, seems to do the job nicely. As always, it’s about the detail!

I still need to sort out deleting values from custom options; my attempts thus far appear to break the database so that you can’t view the custom options in the admin interface. Suspect I’ve missed a relationship or not deleted enough records, but can’t see why.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Darren Felton
Jr. Member
 
Avatar
Total Posts:  2
Joined:  2012-07-31
Minneapolis
 

arabgento,

First post ever on this board. And I just wanted to give a big THANK YOU. I’ve spent about 6 hours trying to create Product Custom Options & add values to the Custom Option via a programmed script.

I spent some time on other forum posts, however it was only from referencing your post that I was able to finally accomplish this task.

Thank you.

 
Magento Community Magento Community
Magento Community
Magento Community
 
nuwinda
Jr. Member
 
Total Posts:  4
Joined:  2012-05-29
 

Also use catalog_product_save_after event insted of catalog_product_prepare_save

 
Magento Community Magento Community
Magento Community
Magento Community
 
magento123
Member
 
Avatar
Total Posts:  64
Joined:  2010-12-30
 

Hello Arabgento,

I have created a new custom option “test”

I have made it saved in database as well , but i want to know how to show that on frontend inline with all the other custom options.

Can you please help me with that.

I am trying getValuesHtml,getOptionHtml functions
for this task.

Please help me in this

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
magento123
Member
 
Avatar
Total Posts:  64
Joined:  2010-12-30
 

Hello All,

I did it using code in Select.php,

But now i want add to cart button next to every option instead of checkbox or mult-select, please help me in that

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
magento123
Member
 
Avatar
Total Posts:  64
Joined:  2010-12-30
 

Hey Guys,

Sorry to bother ,

I did it all created custom option in backend, shown in frontend and added to cart just wasn’t the actual client requirement.

He wanted to be added as different product whenever we add to cart,

So i am now using bundle products for this

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top