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","*"); 但在实际应用 ...
随机推荐
- A - FatMouse' Trade
Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the wareho ...
- leetcode find median sorted arrays python
# @link http://www.cnblogs.com/zuoyuan/p/3759682.html class Solution(object): def findMedianSortedAr ...
- Oracle经典书籍推荐
转自:http://www.cnblogs.com/fjfzhkb/archive/2007/12/05/983381.html 很多网友询问如何选择入门书籍,学Oracle有什么好书,这里给出一些常 ...
- hadoop搭建杂记:Linux下虚拟机集群网络搭建
VirtualBox搭建hadoop伪分布式模式 VirtualBox搭建hadoop伪分布式模式 master: ip:192.168.56.120 机器名: master 启动NameNode 启 ...
- 安卓里面JSON处理和JAVA SE里面的JSON包
今天编译安卓项目遇到这个问题 com.android.dex.DexException: Multiple dex files define的解决办法 大致意思就是引用了 相同的包 在JAVA SE里 ...
- [转载]HDFS的'Block'和MapReduce的'Split'之间的关系和区别
http://www.cnblogs.com/xuxm2007/archive/2011/09/01/2162011.html hadoop的分块有两部分,其中第一部分更为人熟知一点. 第一部分就 ...
- Android模拟器PANIC: Could not open:问题解决方法
最主要的就是环境变量没有配置或者我们使用的是绝对路径配置环境变量.这时我们只需要修改一下Android的环境变量就可以了. 具体解决方法如下: ①在环境变量中创建变量名:ANDROID_SDK_HOM ...
- VS2010/MFC对话框:文件对话框
文件对话框 上一讲介绍的是消息对话框,本节讲解文件对话框.文件对话框也是很常用的一类对话框. 文件对话框的分类 文件对话框分为打开文件对话框和保存文件对话框,相信大家在Windows系统中 ...
- thoughtbot/capybara-webkit
thoughtbot/capybara-webkit A capybara driver that uses WebKit via QtWebKit. Qt Dependency and Instal ...
- 基于xml文件实现系统属性配置管理
文章标题:基于xml文件实现系统属性配置管理 . 文章地址: http://blog.csdn.net/5iasp/article/details/11774501 作者: javaboy2012 E ...