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/的更多相关文章

  1. 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 ...

  2. Programmatically add an application to Windows Firewall

    Programmatically add an application to Windows Firewall 回答1   Not sure if this is the best way, but ...

  3. Add&Delete WindowService

    Part One-Add: Step4: Add the new service to windows service: $commandLine = 'D:\IMS\2.16.0.42-DataSe ...

  4. http协议中:GET/POST/PUT/DELETE/TRACE/OPTIONS/HEAD方法

    ###1 HTTP/1.1协议中共定义了八种方法(有时也叫"动作")来表明Request-URI指定的资源的不同操作方式: OPTIONS 返回服务器针对特定资源所支持的HTTP请 ...

  5. 2016/3/26 连接数据库 网页中数据的增删改 add delete update addchuli updateChuLi test8 DBDA

    主页面 test8.php <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  6. svn一次性add/delete所有文件

    Linux命令行下,svn add 一次性批量上传 命令行下操作svn没有使用界面形式的TortoiseSVN直观,但是不管怎样,命令行下操作svn还是有它的有点,如果你碰到一次需要svn add许多 ...

  7. http的几种请求的方式(Get、Post、Put、Head、Delete、Options、Trace和Connect)

    http的这几种请求方式各有各的特点,适用于各自的环境.下面我就说说这些方式的各自特点: 1.Get:它的原理就是通过发送一个请求来取得服务器上的某一资源.获取到的资源是通过一组HTTP头和呈现数据来 ...

  8. magento -- 如何在magento中进行产品的批量上传

    花费了好多时间,阅读了magento官方论坛上几乎所有的批量上传产品的相关帖子,分析了大量相关magento代码,终于可以完全实现指产品批量上传的功能,免除网速慢,在页面之间跳来跳去,以及重复输入数据 ...

  9. Java Web中实现设置多个域名跨域访问

    添加以下设置可允许所有域名跨域访问: response.setHeader("Access-Control-Allow-Origin","*"); 但在实际应用 ...

随机推荐

  1. Problem F: Exponentiation大数求幂

    DescriptionProblems involving the computation of exact values of very large magnitude and precision ...

  2. [C#参考]细说进程、应用程序域与上下文之间的关系

    原文转载链接:http://www.cnblogs.com/leslies2/archive/2012/03/06/2379235.html Written by:风尘浪子 引言 本文主要是介绍进程( ...

  3. R与数据分析旧笔记(十八完结) 因子分析

    因子分析 因子分析 降维的一种方法,是主成分分析的推广和发展 是用于分析隐藏在表面现象背后的因子作用的统计模型.试图用最少的个数的不可测的公共因子的线性函数与特殊因子之和来描述原来观测的每一分量 因子 ...

  4. SQL 简单练习

    USE study; SELECT * FROM EMP --查询雇员姓名的最后三个字母 ) FROM EMP ; --查询10部门雇员进入公司的星期数 --1 查询部门30中的所有员工 --2 列出 ...

  5. 在CentOS 7 / Gnome 3 双屏时设置主屏

    在Windows中设置扩展显示器为主屏的方式非常清楚,但在Linux中就不是那么明显了,下面介绍如何完成这个设置 ------------------------------------------- ...

  6. poj 3252 Round Numbers 数位dp

    题目链接 找一个范围内二进制中0的个数大于等于1的个数的数的数量.基础的数位dp #include<bits/stdc++.h> using namespace std; #define ...

  7. android-SQLite 和 Content

    SQLite 游标(Cursor)相当于指向底层数据中结果集的指针,而不是提取和返回结果值的副本,是在结果集中对位置(行)进行控制的管理方式. moveToFirst:把游标移动到查询结果的第一行 m ...

  8. #308 (div.2) A. Vanya and Table

    1.题目描写叙述:点击打开链接 2.解题思路:本题是一道简单的模拟题.每次扫描一个输入的长方形,然后将内部全部点都+1,终于统计数组全部元素的和就可以. 3.代码: #define _CRT_SECU ...

  9. HBA简介及原理

    HBA,即主机总线适配器英文“Host Bus Adapter”缩写.是一个使计算机在服务器和存储装置间提供输入/输出(I/O)处理和物理连接的电路板和/或集成电路适配器. 简介 主机总线适配器(Ho ...

  10. struts Value Stack 值栈

    首先声明:本文是从博客园博友的文章转载过来的,感觉说的不错.在此附上地址:http://www.cnblogs.com/jerryxing/archive/2012/04/23/2467299.htm ...