How to Programmatically Add/Delete Custom Options in Magento? - See more at: http://apptha.com/blog/

In this tutorial, I would like to help out Magento developers and clients with how to programmatically add/delete custom options in Magento. At the end of this post you’d be able to add/delete custom option on your Magento website with absolute ease. Here, we are with the set of codes to add custom options in Magento, followed subsequently by deleting custom options.
Programmatically add custom option in Magento:
This will be useful for everyone who want to know how custom option works and how to add/delete the custom option programmatically in front-end or automatically when product save. Only thing you have to do it is that you’ve to place the code in the right place. There are two methods to add the custom options. Let’s discuss them one-by-one in the sections below.
Method 1:
Load the product details using the product Id. For e.g., the product ID is 1
$product_id = 1;
$product = Mage::getModel(“catalog/product”)->load($product_id);
$product->getOptions() This code is used to check whether the product already has the custom options. If not then we will add the custom option array into product using the function called addOption() and saveOption().To give an array as input to the addOption(), we are using a function called, createCustomOption()
$custom_title = “Red,Green,Blue”;
$customoptions_price = “51,23,54”;
$prod_sku = “redsku,greensku,bluesku”;
$customoption_main_title = “Color”;
$option_type = “drop_down”;
$optionData[ ] = $this-> createCustomOption($custom_title, $customoptions_price, $prod_sku , $customoption_main_title,$option_type);
if(count($product->getOptions()) == 0){
foreach ($optionData as $options) {
foreach ($options as $option) {
$opt = Mage::getModel(‘catalog/product_option’);
$opt->setProduct($product);
$opt->addOption($option);
$opt->saveOptions();
}
}
$product->setHasOptions(1)->save();
}
//This function will just create and return array
function createCustomOption($value,$customoptions_price, $sku , $title, $type, $noOption = false)
{
$custom_options = array();
if ($type && $value != “” && $value) {
$values = explode(‘,’, $value);
$skus = explode(‘,’, $sku);
$customoptions_prices = explode(‘,’, $customoptions_price);
if (count($values)) {
/**If the custom option has options*/
if (! $noOption) {
$is_required = 0;
$sort_order = 0;
$custom_options[ ] = array(
‘is_delete’ => 0 ,
‘title’ => $title ,
‘previous_group’ => ” ,
‘previous_type’ => ” ,
‘type’ => $type ,
‘is_require’ => $is_required ,
‘sort_order’ => $sort_order ,
‘values’ => array()
);
for($i = 0; $i < (count($values)) ; $i++)
{
switch ($type) {
case ‘drop_down’:
case ‘radio’:
case ‘checkbox’:
case ‘multiple’:
default:
$custom_options[count($custom_options) - 1]['values'][ ] = array(
‘is_delete’ => 0 , ‘title’ => $values[$i] , ‘option_type_id’ => – 1 , ‘price_type’ => ‘fixed’ , ‘price’ => $customoptions_prices[$i] , ‘sku’ => $skus[$i] , ‘sort_order’ => ”
);
break;
}
}
return $custom_options;
}
/**If the custom option doesn’t have options | Case: area and field*/
else {
$is_required = 0;
$sort_order = ”;
$custom_options[ ] = array(
“is_delete” => 0 , “title” => $title , “previous_group” => “text” , “price_type” => ‘fixed’ , “price” => ” , “type” => $type , “is_required” => $is_required
);
return $custom_options;
}
}
}
return false;
}
$optionData[ ] = $this-> createCustomOption($custom_title, $customoptions_price, $prod_sku , $customoption_main_title,$option_type);
The createCustomOption() function, when called will return an array similar to below:
Array
(
[0] => Array
(
[0] => Array
(
[is_delete] => 0
[title] => Color
[previous_group] => ‘’
[previous_type] => ‘’
[type] => drop_down
[is_require] => 0
[sort_order] => 0
[values] => Array
(
[0] => Array
(
[is_delete] => 0
[title] => Red
[option_type_id] => 1
[price_type] => fixed
[price] => 51
[sku] => redsku
[sort_order] => 0
)
[1] => Array
(
[is_delete] => 0
[title] => Green
[option_type_id] => 1
[price_type] => fixed
[price] => 23
[sku] => greensku
[sort_order] => 1
)
[2] => Array
(
[is_delete] => 0
[title] => Blue
[option_type_id] => 1
[price_type] => fixed
[price] => 54
[sku] => bluesku
[sort_order] => 2
)
)
)
)
)
Method 2:
In other way you can add Custom option in this manner also:
$productCollection = Mage::getModel(“catalog/product”)->load(1);
$product_id = $productCollection ->getId();
$ex_cop = array();
foreach ($product->getOptions() as $value) {
$ex_cop[ ] = $value->getTitle();
}
$custome_option = array();
$optionsfield = array(
‘type’ => ‘radio’,//select
‘is_require’ => 1,
‘sort_order’ => ’0′
);
$valuesfield = array(
array(
‘title’ => ‘Option Value 1′,
‘price’ => 0.00,
‘price_type’ => ‘fixed’,
‘sku’ => ”,
‘ sort_order’ => ’1′
),
array(
‘title’ => ‘Option Value 1′,
‘price’ => 0.00,
‘price_type’ => ‘fixed’,
‘sku’ => ”,
‘sort_order’ => ’1′
)
);
$valuesfield = array();
$custome_option[ ] = array(‘title’=>’Start Date’,'options’=>$optionsfield,’values’=>$valuesfield);
foreach($custome_option as $cp){
if(!in_array($cp['title'],$ex_cop)){
Mage::helper(‘module_name’)->setCustomOption($product_id, $cp['title'], $cp['options'], $cp['values']);
}
}
In module_name/helper/data.php add function
public function setCustomOption($productId, $title, array $optionData, array $values = array())
{
$product = Mage::getModel(‘catalog/product’)->load($productId);
//$product->getAttributeSetId();
$data = array_merge( $optionData, array(
‘product_id’ => (int)$productId,
‘title’ => $title,
‘values’ => $values,
));
$product->setHasOptions(1)->save();
$option = Mage::getModel(‘catalog/product_option’)->setData($data)->setProduct($product)->save();
return $option;
}
To delete the custom option:
if($product->getOptions() != ”){
foreach ($product->getOptions() as $opt)
{
$opt->delete();
}
$product->setHasOptions(0)->save();
}
I hope this post was beneficial and helped you learn how to programmatically add/delete custom options in Magento.
- See more at: http://apptha.com/blog/how-to-programmatically-adddelete-custom-options-in-magento/#sthash.j72efsMV.dpuf
ref http://apptha.com/blog/how-to-programmatically-adddelete-custom-options-in-magento/
How to Programmatically Add/Delete Custom Options in Magento? - See more at: http://apptha.com/blog/的更多相关文章
- AutoMapper:Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
异常处理汇总-后端系列 http://www.cnblogs.com/dunitian/p/4523006.html 应用场景:ViewModel==>Mode映射的时候出错 AutoMappe ...
- Programmatically add an application to Windows Firewall
Programmatically add an application to Windows Firewall 回答1 Not sure if this is the best way, but ...
- Add&Delete WindowService
Part One-Add: Step4: Add the new service to windows service: $commandLine = 'D:\IMS\2.16.0.42-DataSe ...
- http协议中:GET/POST/PUT/DELETE/TRACE/OPTIONS/HEAD方法
###1 HTTP/1.1协议中共定义了八种方法(有时也叫"动作")来表明Request-URI指定的资源的不同操作方式: OPTIONS 返回服务器针对特定资源所支持的HTTP请 ...
- 2016/3/26 连接数据库 网页中数据的增删改 add delete update addchuli updateChuLi test8 DBDA
主页面 test8.php <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...
- svn一次性add/delete所有文件
Linux命令行下,svn add 一次性批量上传 命令行下操作svn没有使用界面形式的TortoiseSVN直观,但是不管怎样,命令行下操作svn还是有它的有点,如果你碰到一次需要svn add许多 ...
- http的几种请求的方式(Get、Post、Put、Head、Delete、Options、Trace和Connect)
http的这几种请求方式各有各的特点,适用于各自的环境.下面我就说说这些方式的各自特点: 1.Get:它的原理就是通过发送一个请求来取得服务器上的某一资源.获取到的资源是通过一组HTTP头和呈现数据来 ...
- magento -- 如何在magento中进行产品的批量上传
花费了好多时间,阅读了magento官方论坛上几乎所有的批量上传产品的相关帖子,分析了大量相关magento代码,终于可以完全实现指产品批量上传的功能,免除网速慢,在页面之间跳来跳去,以及重复输入数据 ...
- Java Web中实现设置多个域名跨域访问
添加以下设置可允许所有域名跨域访问: response.setHeader("Access-Control-Allow-Origin","*"); 但在实际应用 ...
随机推荐
- Models——英语学习小技巧之四
Models are very important, here model means role model, is kind of like a hero. It's someone that ...
- Django Web开发【4】 用户注册与管理
几乎所有的网站都提供了用户注册与管理功能,这一节,我们将讲解如何利用Django自身提供的用户认证系统实现用户注册与管理功能. 会话认证 在上一节中,我们学习了User数据模型,并用它来保存用户信息, ...
- Java中的流程控制(三)
关于Java中的流程控制 关于Java中的流程控制 4.do while语句 do while语句的功能和while语句差不多,只不过它是在执行完第一次循环后才检测条件表达式的值,这意味着包含在大括号 ...
- JQ兼容性问题
checkbox操作 1:设置为选中状态 $(this).prop("checked", true); 2:判断是否选中 $(this).is(":check ...
- less 工具
less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性.在 more 的时候,我们并没有办法向前面翻 ...
- C#学习日志 day 1 ------ hello C# !
首先是C#的编译器的安装.这里用vs2013.我用的是Windows 8.1系统,所以安装起来并不难. 双击vs_ultimate.exe 逐步安装就好.这里用校园邮箱在dream spark 上进行 ...
- win7系统中桌面图标显示不正常问题
http://jingyan.baidu.com/article/466506580c9327f549e5f8dc.html 最近笔者在安装软件时,突然出现了桌面图标显示不正常了,一开始还以为是新安装 ...
- SMTP邮件传输协议发送邮件和附件
在以前接触的项目中,一直都是在做网站时用到了发送mail 的功能,在asp 和.net 中都有相关的发送mail 的类, 实现起来非常简单.最近这段时间因工作需要在C++ 中使用发送mail 的功能, ...
- 在PADS LAYOUT中如何隐藏不需要的鼠线?
如下图示,将net GPR_0的鼠线隐藏. 鼠标右键,选择网络----选择你要隐藏的网络------右键选择view nets----点击对话框右边View List里你所选的网络-----在右下角t ...
- ssm框架理解
SSM框架理解 最近两星期一直在学JavaEE的MVC框架,因为之前学校开的JavaEE课程就一直学的吊儿郎当的,所以现在真正需要掌握就非常手忙脚乱,在此记录下这段时间学习的感悟,如有错误,希望大牛毫 ...