We are doing custom Magento themes, Magento design, custom Magento extensions and creative services through Magento designers & developers for worldwide shop owners.

Last News
product custom option programmatically

product custom option programmatically

many people need Adding/removing values from a product custom option programmatically , it’s not simple because you must approved all relations between table , now this snippets is very useful in world magento , it’s tested by other developers ,and this is one of testimonial:
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php $magePath = '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,167);
$productmodel = Mage::getModel('catalog/product');

foreach ($product_ids as $product_id) { /**i use this two arrays for collecte value because i uses inside setData of
current option*/$cos=array();$co=array();
$product = $productmodel->load($product_id);
$options = $product->getProductOptionsCollection();
if (isset($options)) { foreach ($options as $o) {
$title = $o->getTitle();/**
this block is for changing information of specific option from collection options inside
current product
the save method for this option in end of code
*/if ($title == "Line 1/Front") {
$o->setProduct($product); $o->setTitle("Line 1/Ftont"); $o->setType("drop_down"); $o->setIsRequire(1);$o->setSortOrder(0);

}/**
this block for update or add information of specific value inside current option
*/$optionType = $o->getType(); //test typeif ($optionType == "drop_down") { //getting collection of value related to current option$values = $o->getValuesCollection(); $found = false;
foreach ($values as $k => $v) { //test existing of value for updateif (1 == preg_match("/said$/i", $v->getTitle())) { //update and save$v->setTitle("morad")
->setSku("kk")
->setPriceType("fixed")
->setSortOrder(0)
->setPrice(floatval(13.0000));$v->setOption($o)->save();/**
this ligne is important i collecte all value required for normalize save function
related to current option
*/$cos[]=$v->toArray($co);}
} /**
create new value object you can use $option->getValueInstance() for working with
getSingleton
*/$value = Mage::getModel('catalog/product_option_value'); $value->setOption($o)
->setTitle('valueiwant')
->setSku("nn")
->setPriceType("fixed")
->setSortOrder(1)
->setPrice(12)/**
this ligne is important (relation forigien key) for related this new value
to specific option
*/->setOptionId($o->getId());$value->save();/**
this ligne is important i collecte all value required for normalize save function
related to current option
*/$cos[]=$value->toArray($co);}

$o->setData("values",$cos)->save();//var_dump($cos);
}

}
}

2 comments

  1. Excellent Post… Had to rearrange a bit the code, but you did provide an amazing example… Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*