How to Add Custom Attributes to Custom Options in Magento

比如新增2个字段upc和ean

1fed7aa7-1e17-414a-9f7d-1c42408c2f8b

做法如下:

1,setup里新建2个字段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php 
$installer = Mage::getResourceModel('sales/setup', 'sales_setup');
$installer->startSetup();
$this->getConnection()->addColumn(
$this->getTable('catalog/product_option_type_value'),
'upc',
'VARCHAR(20) NULL'
);
$this->getConnection()->addColumn(
$this->getTable('catalog/product_option_type_value'),
'ean',
'VARCHAR(20) NULL'
);
$installer->endSetup();
?>

 

2,改写block

Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Options_Type_Select

设置自定义的phtml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magento.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magento.com for more information.
*
* @category Mage
* @package Mage_Adminhtml
* @copyright Copyright (c) 2006-2014 X.commerce, Inc. (http://www.magento.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

/**
* customers defined options
*
* @category Mage
* @package Mage_Adminhtml
* @author Magento Core Team <core@magentocommerce.com>
*/

class EM_Supplier_Block_Adminhtml_Catalog_Product_Edit_Tab_Options_Type_Select extends
Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Options_Type_Select
{
/**
* Class constructor
*/
public function __construct()
{
parent::__construct();
$this->setTemplate('supplier/catalog_product_edit_options_type_select.phtml');
$this->setCanEditPrice(true);
$this->setCanReadPrice(true);
}
}

 

catalog_product_edit_options_type_select.phtml如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE_AFL.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magento.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magento.com for more information.
*
* @category design
* @package default_default
* @copyright Copyright (c) 2006-2014 X.commerce, Inc. (http://www.magento.com)
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
?>

<script type="text/javascript">
//<![CDATA[
OptionTemplateSelect = '<table class="border" cellpadding="0" cellspacing="0">'+
'<input type="hidden" class="required-option-select-type-rows" name="validation_{ {option_id}}_result" value="" >'+
'<thead>'+
'<tr class="headings">'+
'<th class="type-title"><?php echo Mage::helper('catalog')->__('Title') ?> <span class="required">*</span></th>'+
<?php if ($this->getCanReadPrice() !== false) : ?>
'<th class="type-price"><?php echo Mage::helper('catalog')->__('Price') ?></th>' +
'<th class="type-type"><?php echo Mage::helper('catalog')->__('Price Type') ?></th>' +
<?php endif; ?>
'<th class="type-sku"><?php echo Mage::helper('catalog')->__('SKU') ?></th>'+
'<th class="type-upc"><?php echo Mage::helper('catalog')->__('UPC') ?></th>'+
'<th class="type-ean"><?php echo Mage::helper('catalog')->__('EAN') ?></th>'+
'<th class="type-order"><?php echo Mage::helper('catalog')->__('Sort Order') ?></th>'+
'<th class="type-butt last">&nbsp;</th>'+
'</tr>'+
'</thead>'+
'<tbody id="select_option_type_row_{ {option_id}}">'+
'</tbody>'+
'<tfoot>'+
'<tr>'+
'<td colspan="100" class="a-right"><?php echo $this->getAddButtonHtml() ?></td>'+
'</tr>'+
'</tfoot>'+
'</table>';

OptionTemplateSelectRow = '<tr id="product_option_{ {id}}_select_{ {select_id}}">'+
'<td>'+
'<input type="hidden" name="product[options][{ {id}}][values][{ {select_id}}][option_type_id]" value="{ {option_type_id}}">'+
'<input type="hidden" id="product_option_{ {id}}_select_{ {select_id}}_is_delete" name="product[options][{ {id}}][values][{ {select_id}}][is_delete]" value="">'+
'<input type="text" class="required-entry input-text select-type-title" id="product_option_{ {id}}_select_{ {select_id}}_title" name="product[options][{ {id}}][values][{ {select_id}}][title]" value="{ {title}}">{ {checkboxScopeTitle}}</td>'+
<?php if ($this->getCanReadPrice() !== false) : ?>
'<td><input type="text" class="input-text validate-number product-option-price" id="product_option_{ {id}}_select_{ {select_id}}_price" name="product[options][{ {id}}][values][{ {select_id}}][price]" value="{ {price}}"<?php if ($this->getCanEditPrice() === false) : ?> disabled="disabled"<?php endif; ?>></td>' +
'<td><?php echo $this->getPriceTypeSelectHtml() ?>{ {checkboxScopePrice}}</td>' +
<?php else : ?>
'<input type="hidden" id="product_option_{ {id}}_select_{ {select_id}}_price" name="product[options][{ {id}}][values][{ {select_id}}][price]">' +
'<input type="hidden" name="product[options][{ {id}}][values][{ {select_id}}][price_type]" id="product_option_{ {id}}_select_{ {select_id}}_price_type">' +
<?php endif; ?>
'<td><input type="text" class="input-text" name="product[options][{ {id}}][values][{ {select_id}}][sku]" value="{ {sku}}"></td>'+
'<td><input type="text" class="input-text" name="product[options][{ {id}}][values][{ {select_id}}][upc]" value="{ {upc}}"></td>'+
'<td><input type="text" class="input-text" name="product[options][{ {id}}][values][{ {select_id}}][ean]" value="{ {ean}}"></td>'+
'<td><input type="text" class="validate-zero-or-greater input-text" name="product[options][{ {id}}][values][{ {select_id}}][sort_order]" value="{ {sort_order}}"></td>'+
'<td class="last"><span title="Delete row"><?php echo $this->getDeleteButtonHtml() ?></span></td>'+
'</tr>';

selectOptionType = {
div : 'select_option_type_row',
itemCount : 0,
templateSyntax : /(^|.|\r|\n)({ {(\w+)}})/,
templateText : OptionTemplateSelectRow,
add : function(data) {

this.template = new Template(this.templateText, this.templateSyntax);

if (data.target || data.srcElement) {//data is Event (work in IE and Firefox)
element = $(Event.findElement(data, 'button'));
optionId = element.readAttribute('id').sub('add_select_row_button_', '');
data = {};
data.option_type_id = '-1';
data.select_id = this.itemCount;
} else {
optionId = data.option_id;
data.select_id = data.option_type_id;
this.itemCount = data.item_count;
}

data.id = optionId;

Element.insert($(this.div+'_'+data.id), {'bottom':this.template.evaluate(data)});

if (data.checkboxScopeTitle) {
//set disabled
if ($('product_option_'+data.id+'_select_'+data.select_id+'_title') && data.scopeTitleDisabled) {
$('product_option_'+data.id+'_select_'+data.select_id+'_title').disable();
}
}
if (data.checkboxScopePrice) {
//set disabled
if ($('product_option_'+data.id+'_select_'+data.select_id+'_price') && data.scopePriceDisabled) {
$('product_option_'+data.id+'_select_'+data.select_id+'_price').disable();
$('product_option_'+data.id+'_select_'+data.select_id+'_price_type').disable();
}
}

if (data.price_type) {
$A($('product_option_'+data.id+'_select_'+data.select_id+'_price_type').options).each(function(option){
if (option.value==data.price_type) option.selected = true;
});
}

this.itemCount++;
this.bindRemoveButtons();
},
remove : function(event){
var element = $(Event.findElement(event, 'tr'));

if(element){
$(element.readAttribute('id')+'_is_delete').value = '1';
element.addClassName('no-display');
element.addClassName('ignore-validate');
element.hide();
}
},
bindRemoveButtons : function(){
var buttons = $$('.delete-select-row');
for(var i=0;i<buttons.length;i++){
if(!$(buttons[i]).binded){
$(buttons[i]).binded = true;
Event.observe(buttons[i], 'click', this.remove.bind(this));
}
}
},
bindAddButton : function()
{
var buttons = $$('.add-select-row');
for(var i=0;i<buttons.length;i++){
if(!$(buttons[i]).binded){
$(buttons[i]).binded = true;
Event.observe(buttons[i], 'click', this.add.bind(this));
}
}
}
}

if ($('option_panel_type_select')) {
$('option_panel_type_select').remove();
}

selectOptionType.bindRemoveButtons();

Validation.addAllThese([
['required-option-select-type-rows', <?php echo $this->helper('core')->jsonEncode(Mage::helper('catalog')->__('Please add rows to option.')); ?>, function(v, elm) {
var optionContainerElm = elm.up('div.grid');
var selectTypesFlag = false;
selectTypeElements = $$('#'+optionContainerElm.id+' .select-type-title');
selectTypeElements.each(function(elm){
if (elm.id && elm.up('tr').visible()) {
selectTypesFlag = true;
}
});
elm.advaiceContainer = optionContainerElm.id+'_advice';
return selectTypesFlag;
}]]);

if($('add_select_row_button')){
Event.observe('add_select_row_button', 'click', selectOptionType.add.bind(selectOptionType));
}
//]]>
</script>

3,重写block

Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Options_Option

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php

class EM_Supplier_Block_Adminhtml_Catalog_Product_Edit_Tab_Options_Option extends Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Options_Option
{
public function getOptionValues()
{
$optionsArr = array_reverse($this->getProduct()->getOptions(), true);
// $optionsArr = $this->getProduct()->getOptions();

if (!$this->_values) {
$showPrice = $this->getCanReadPrice();
$values = array();
$scope = (int) Mage::app()->getStore()->getConfig(Mage_Core_Model_Store::XML_PATH_PRICE_SCOPE);
foreach ($optionsArr as $option) {
/* @var $option Mage_Catalog_Model_Product_Option */

$this->setItemCount($option->getOptionId());

$value = array();

$value['id'] = $option->getOptionId();
$value['item_count'] = $this->getItemCount();
$value['option_id'] = $option->getOptionId();
$value['title'] = $this->escapeHtml($option->getTitle());
$value['type'] = $option->getType();
$value['is_require'] = $option->getIsRequire();
$value['sort_order'] = $option->getSortOrder();
$value['can_edit_price'] = $this->getCanEditPrice();

if ($this->getProduct()->getStoreId() != '0') {
$value['checkboxScopeTitle'] = $this->getCheckboxScopeHtml($option->getOptionId(), 'title',
is_null($option->getStoreTitle()));
$value['scopeTitleDisabled'] = is_null($option->getStoreTitle())?'disabled':null;
}

if ($option->getGroupByType() == Mage_Catalog_Model_Product_Option::OPTION_GROUP_SELECT) {

// $valuesArr = array_reverse($option->getValues(), true);

$i = 0;
$itemCount = 0;
foreach ($option->getValues() as $_value) {
/* @var $_value Mage_Catalog_Model_Product_Option_Value */
$value['optionValues'][$i] = array(
'item_count' => max($itemCount, $_value->getOptionTypeId()),
'option_id' => $_value->getOptionId(),
'option_type_id' => $_value->getOptionTypeId(),
'title' => $this->escapeHtml($_value->getTitle()),
'price' => ($showPrice)
? $this->getPriceValue($_value->getPrice(), $_value->getPriceType()) : '',
'price_type' => ($showPrice) ? $_value->getPriceType() : 0,
'sku' => $this->escapeHtml($_value->getSku()),
'upc' => $this->escapeHtml($_value->getUpc()),
'ean' => $this->escapeHtml($_value->getEan()),
'sort_order' => $_value->getSortOrder(),
);

if ($this->getProduct()->getStoreId() != '0') {
$value['optionValues'][$i]['checkboxScopeTitle'] = $this->getCheckboxScopeHtml(
$_value->getOptionId(), 'title', is_null($_value->getStoreTitle()),
$_value->getOptionTypeId());
$value['optionValues'][$i]['scopeTitleDisabled'] = is_null($_value->getStoreTitle())
? 'disabled' : null;
if ($scope == Mage_Core_Model_Store::PRICE_SCOPE_WEBSITE) {
$value['optionValues'][$i]['checkboxScopePrice'] = $this->getCheckboxScopeHtml(
$_value->getOptionId(), 'price', is_null($_value->getstorePrice()),
$_value->getOptionTypeId());
$value['optionValues'][$i]['scopePriceDisabled'] = is_null($_value->getStorePrice())
? 'disabled' : null;
}
}
$i++;
}
} else {
$value['price'] = ($showPrice)
? $this->getPriceValue($option->getPrice(), $option->getPriceType()) : '';
$value['price_type'] = $option->getPriceType();
$value['sku'] = $this->escapeHtml($option->getSku());
$value['max_characters'] = $option->getMaxCharacters();
$value['file_extension'] = $option->getFileExtension();
$value['image_size_x'] = $option->getImageSizeX();
$value['image_size_y'] = $option->getImageSizeY();
if ($this->getProduct()->getStoreId() != '0' &&
$scope == Mage_Core_Model_Store::PRICE_SCOPE_WEBSITE) {
$value['checkboxScopePrice'] = $this->getCheckboxScopeHtml($option->getOptionId(),
'price', is_null($option->getStorePrice()));
$value['scopePriceDisabled'] = is_null($option->getStorePrice())?'disabled':null;
}
}
$values[] = new Varien_Object($value);
}
$this->_values = $values;
}

return $this->_values;
}

}

 

坚持原创技术分享,您的支持将鼓励我继续创作!