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","*"); 但在实际应用 ...
随机推荐
- Java学习之利用集合发牌小练习
/* * 思路: * A:创建一个HashMap集合 * B:创建一个ArrayList集合 * C:创建花色数组和点数数组 * D:从0开始往HashMap里面存储编号,并存储对应的牌同时往Arra ...
- CSS 基础总结
CSS基础 Doctype 声明位于文档中的最前面,处于 标签之前.告知浏览器的解析器,用什么文档类型 规范来解析这个文档. 在标准模式中,浏览器根据规范呈现页面: 在混杂模式中,页面以一种比较宽松的 ...
- SQL Server 中大小写区分的处理
SQL Server 中大小写区分的处理. 默认情况下,SQL Server 里面是不区分大小写的: E:\>sqlcmd -S "localhost\SQLEXPRESS" ...
- 有感于NC的强大
第一次知道nc(netcat)是好几年前的事了,那个时候天比现在更蓝,草比现在更绿,卤煮也还是一个刚上大学不久的青葱骚年... 现在把这个01年的老古董拿出来说好像有点炒冷饭的意思,资料也铺天盖地了说 ...
- 清理8组nodes中表的历史数据,平均每个node中的表有1.5亿条记录,需要根据date_created字段清理8000W数据记录,这个字段没有索引。
清理8组nodes中表的历史数据,平均每个node中的表有1.5亿条记录,需要根据date_created字段清理8000W数据记录,这个字段没有索引. 环境介绍 线上磁盘空间不足,truncate ...
- Git安装及基本使用
准备: Git软件,github账号. Git安装: 直接百度搜git下载,windows和mac不同平台的.官网上的下载地址很慢或者根本下不了. 默认配置安装. github: 网址:https:/ ...
- maven项目启动
1服务install 2 build (tomcat:run)
- 使用COCOS2D-X JSB开发,在js页面中设置iOS键盘模式
XYSDK.h void setKeyboardType(int type); XYSDK.cpp voidXYSDK::setKeyboardType(int type) { #if (CC_TAR ...
- 2013杭州网络赛D题HDU 4741(计算几何 解三元一次方程组)
Save Labman No.004 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- MSSQL:修改tempdb设置增加DW性能
Temp DB 在DW中变得非常重要,因为要进行大量的运算,如果内存不够数据就会放在Temp DB中 1. 把Temp DB移动到高性能的磁盘上. 2. 增加tempdb 的大小 3. 把Auto S ...