亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

How to Programmatically Add/Delete Custom Op

系統 1627 0

?

?

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/


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久一er精这里有精品 | 97成人精品视频在线播放 | 青娱乐伊人 | 精品日本亚洲一区二区三区 | 香蕉大黄香蕉在线观看 | 欧美不卡在线观看 | 欧美洲大黑香蕉在线视频 | 国产成人精品视频频 | 玖玖在线免费视频 | 国产精品视频网 | 亚洲精品视频一区 | 一区二区不卡不卡一卡 | 久久泄欲网| 我想看一级毛片免费的 | 经典国产乱子伦精品视频 | 欧美一区日韩一区中文字幕页 | 日本一区二区三区在线观看 | 久久99网| 色偷偷亚洲第一综合 | caoporm超免费公开视频 | 在线看国产 | 2020久久精品国产免费 | 精品亚洲永久免费精品 | 欧美性猛片xxxxⅹ免费 | 久久精品国产一区二区三区不卡 | 福利视频欧美一区二区三区 | 久久国产精品国产自线拍免费 | 欧美激情伦妇在线观看 | 久久福利青草精品资源站免费 | 动漫美女h片黄动漫在线观看 | 草莓视频caomei888 | 精品午夜寂寞影院在线观看 | 精品久久久久久中文字幕专区 | 精品综合一区二区三区 | 日韩欧美视频一区二区在线观看 | 性做久久久久久蜜桃花 | 韩国亚洲伊人久久综合影院 | 不卡在线视频 | 嘿咻嘿咻免费区在线观看吃奶 | 亚洲一区二区三区日本久久九 | 国产www在线观看 |