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","*"); 但在实际应用 ...
随机推荐
- Hibernate学习之检索策略
一.类级别的检索策略 类级别可选的检索策略包括立即检索和延迟检索, 默认为延迟检索 –立即检索: 立即加载检索方法指定的对象 –延迟检索: 延迟加载检索方法指定的对象,在使用具体的属性时,再进行加载 ...
- ThinkPHP第二十五天(自动完成、用户名密码PHP正则、移位或加密函数)
1.ThinkPHP自动完成功能 跟昨天的自动验证功能类似,也是需要在自定义的UserModel类,进行使用. 使用方法:定义$_auto属性 $_auto = array( array(完成字段,完 ...
- Use eplipse to develop Python project
Source: This is the example how to use eclipse and python. http://www.360doc.com/content/15/0206/10/ ...
- ortoiseSVN无法编辑日志信息的解决方法
提交时忘记编写日志,想重新编辑日志信息,弹出错误提示: DAV 请求失败:可能是版本库的 pre-revprop-change 钩子执行失败或者不存在 至少有一个属性变更失败:版本库未改变 设置属性 ...
- POJ 1226 Substrings(后缀数组+二分答案)
[题目链接] http://poj.org/problem?id=1226 [题目大意] 求在每个给出字符串中出现的最长子串的长度,字符串在出现的时候可以是倒置的. [题解] 我们将每个字符串倒置,用 ...
- LRU算法的设计
一道LeetCode OJ上的题目,要求设计一个LRU(Least Recently Used)算法,题目描述如下: Design and implement a data structure for ...
- c++实现将表达式转换为逆波兰表达式
https://github.com/Lanying0/lintcode 所属: 数据结构->线性结构->栈 问题: 给定一个表达式字符串数组,返回该表达式的逆波兰表达式(即去掉括号). ...
- [置顶] js模板方法的思路及实现
在js中如何实现设计模式中的模板方法? 思路的产生必然要求熟悉js,如何实现?就很简单了,都知道在js中如果定义两个相同名称的方法,前一个方法就会被后一个方法覆盖掉,使用此特点就可以实现模板方法. 例 ...
- 现场故障案例:AIX安装Oracle10G runInstaller弹出错误一例
AIX安装Oracle10G runInstallert弹出错误一例 环境: 系统:AIX5300-08 数据库:Oracle 10g(64bit) AIX客户机卸载oracle软件后,又一次安装or ...
- 基于 dbms_redefinition 在线重定义表
Oracle 支持在线重定义表,也就是说我们可以在修改表结构(DDL)的同时进行相关的DQL.DML操作,使得前端的DML根本感觉不到表结构实际上已经发生了变化,对于用户而言是完全透明的.当然在线重定 ...