Magento Forum

Extending Magento Custom Options.  Saving data in the backend
 
JOEL0903
Member
 
Total Posts:  73
Joined:  2010-08-19
 

I am trying to extend the custom options in two regards.  First, I want to be able to add fields to the custom option itself.  To title, input type, is required, and sort order, I want to add Image Alt Tag, Image File Name, and Option Group.

In addition, I want to add to the options for the field, area, and the various select types.  I want to add the fields Image Alt Tag and Image File Name to the custom options.

In \app\design\adminhtml\default\default\template\catalog\product\edit\options\option.phtml, I have added the fields by appending to the javascript like so:

'<thead>'+
                
'<tr>'+
                    
'<th class="opt-title"><?php echo Mage::helper('catalog')->__('Title') ?> <span class="required">*</span></th>'+
                    
'<th class="opt-type"><?php echo Mage::helper('catalog')->__('Input Type') ?> <span class="required">*</span></th>'+
                    
'<th class="opt-req"><?php echo $this->jsQuoteEscape(Mage::helper('catalog')->__('Is Required')) ?></th>'+
                    
'<th class="opt-order"><?php echo Mage::helper('catalog')->__('Sort Order') ?></th>'+
                    
'<th class="opt-alt" style="padding-left:10px;"><a style="display:inline;color:#2d444f;text-decoration:none;" href="#" rel="nofollow" onclick="[removed]window.alert(\'If the Option Field should have an imageenter the text for the Alt tag\');return false;"><?php echo Mage::helper('catalog')->__('Image Alt Tag') ?></a> <a style="display:inline;color:#2d444f;text-decoration:none;" href="#" rel="nofollow" onclick="[removed]window.alert(\'If the Option Field should have an imageenter the text for the images Alt tag\');return false;"><img style="vertical-align:middle;" border="0" src="<?php echo $qmark; ?>"></img></a></th>'+
                    
'<th class="opt-path" style="padding-left:10px;"><a style="display:inline;color:#2d444f;text-decoration:none;" href="#" rel="nofollow" onclick="[removed]window.alert(\'If the Option Field should have an imageenter the path relative to \\n/media/images/optfieldimages\');return false;"><?php echo Mage::helper('catalog')->__('Image File Name') ?></a> <a style="display:inline;color:#2d444f;text-decoration:none;" href="#" rel="nofollow" onclick="[removed]window.alert(\'If the Option Field should have an imageenter the path relative to \\n/media/images/optfieldimages\');return false;"><img style="vertical-align:middle;" border="0" src="<?php echo $qmark; ?>"></img></a></th>'+            
                    
'<th class="a-right"><?php echo $this->jsQuoteEscape($this->getDeleteButtonHtml()) ?></th>'+
                
'</tr>'+
            
'</thead>'+
            
'<tr>'+
                
'<td><input type="text" class="required-entry input-text" id="<?php echo $this->getFieldId() ?>_{{id}}_title" name="<?php echo $this->getFieldName() ?>[{{id}}][title]" value="{{title}}">{{checkboxScopeTitle}}</td>'+
                
'<td><?php echo $this->getTypeSelectHtml() ?></td>'+
                
'<td class="opt-req"><?php echo $this->getRequireSelectHtml() ?></td>'+
                
'<td><input type="text" class="validate-zero-or-greater input-text" name="<?php echo $this->getFieldName() ?>[{{id}}][sort_order]" value="{{sort_order}}"></td>'+
                
'<td class="opt-alt" style="padding-left:10px;"><input style="" type="text" class="input-text" name="<?php echo $this->getFieldName() ?>[{{id}}][image_alt]" value="{{image_alt}}"></td>'+
                
'<td class="opt-path" style="padding-left:10px;width:200px;"><input style="width: 90% !important;" type="text" class="input-text" name="<?php echo $this->getFieldName() ?>[{{id}}][image_path]" value="{{image_path}}"></td>'+
                
'<td>&nbsp;</td>'+
            
'</tr>'+
                
'<tr>'+
                    
'<td class="opt-title" colspan="2"><span style="font-weight:bold">Option Group</span>'+
                    
'&nbsp;<select  name="<?php echo $this->getFieldName() ?>[{{id}}][option_group]">'+
                    
'<option value="">-- Please select --</option>'+
            
'<option value="0">Vehicle Options</option>'+
                    
'<option value="1">Group 1</option>'+
                    
'<option value="2">Group 2</option>'+
                    
'<option value="3">Group 3</option>'+
                    
'<option value="4">Group 4</option>'+
                    
'</select>'+
            
'&nbsp;<a style="display:inline;color:#2d444f;text-decoration:none;" href="#" rel="nofollow" onclick="[removed]window.alert(\'Use these fields to put custom options of the same time together on the page.  The Sort Order will apply inside each group.  Leave off if you do not wish to change the grouping of the custom options.\\n\\nFor instancethis can be used instead of isRequired to separate fields into those hidden and those shown\');return false;"><img style="vertical-align:middle;" border="0" src="<?php echo $qmark; ?>"></img></a>'+
                
'</tr>'+

In \app\design\adminhtml\default\default\template\catalog\product\edit\options\type\select.phtml, I have added these lines to the variable OptionTemplateSelectedRow

'<td><input style="" type="text" class="input-text" name="product[options][{{id}}][values][{{select_id}}][image_alt]" value="{{image_alt}}"></td>'+            
            
'<td class="type-last last"><input style="width: 90% !important;" type="text" class="input-text" name="product[options][{{id}}][values][{{select_id}}][image_path]" value="{{image_path}}"></td>'+
 
Magento Community Magento Community
Magento Community
Magento Community
 
JOEL0903
Member
 
Total Posts:  73
Joined:  2010-08-19
 

In \app\design\adminhtml\default\default\template\catalog\product\edit\options\type\text.phtml I have added the following lines:

'<td><input style="" type="text" class="input-text" name="product[options][{{option_id}}][image_alt]" value="{{image_alt}}"></td>'
            
'<td class="type-last last"><input style="width: 90% !important;" type="text" class="input-text" name="product[options][{{option_id}}][image_path]" value="{{image_path}}"></td>'+

Once I get the code working, I\’ll add the .phtml files to my module instead of modifying the core files.

<?xml version="1.0"?>
<config>
  <
modules>
    <
Sadg_Advancedcustomoptions>
      <
version>0.1.0</version>
    </
Sadg_Advancedcustomoptions>
  </
modules>
  <global>
    <
helpers>
      <
advancedcustomoptions>
        <class>
Sadg_Advancedcustomoptions_Helper</class>
      </
advancedcustomoptions>
    </
helpers>
    <
blocks>
        <
advancedcustomoptions>
            <
adminhtml>
                <
rewrite>
                    <
catalog_product_edit_tab_options_option>Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Options_Option</catalog_product_edit_tab_options_option>
                </
rewrite>
            </
adminhtml>
        </
advancedcustomoptions>
    </
blocks>
  </global>
</
config>
 
Magento Community Magento Community
Magento Community
Magento Community
 
JOEL0903
Member
 
Total Posts:  73
Joined:  2010-08-19
 

<?php
class Sadg_Advancedcustomoptions_Block_Adminhtml_Catalog_Product_Edit_Tab_Options_Option extends Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Options_Option
{

    
/**
     * Class constructor
     */
    
public function __construct()
    
{
        parent
::__construct();
        
$this->setTemplate(\'catalog/product/edit/options/option.phtml\');
        
$this->setCanReadPrice(true);
        
$this->setCanEditPrice(true);
    
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
JOEL0903
Member
 
Total Posts:  73
Joined:  2010-08-19
 

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->htmlEscape($option->getTitle());
                
$value['type'$option->getType();
                
$value['is_require'$option->getIsRequire();
                
$value['sort_order'$option->getSortOrder();
                
$value['can_edit_price'$this->getCanEditPrice();
                
$value['image_alt'$this->getImageAlt();
                
$value['image_path'$this->getImagePath();

                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->htmlEscape($_value->getTitle()),
                            
'price' => ($showPrice)
                                ? 
$this->getPriceValue($_value->getPrice(), $_value->getPriceType()) : '',
                            
'price_type' => ($showPrice) ? $_value->getPriceType() : 0,
                            
'sku' => $this->htmlEscape($_value->getSku()),
                            
'sort_order' => $_value->getSortOrder(),
                            
'image_alt' => htmlEscape($_value->getImageAlt()),
                            
'image_path' => htmlEscape($_value->getImagePath())
                        );

                        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->htmlEscape($option->getSku());
                    
$value['max_characters'$option->getMaxCharacters();
                    
$value['file_extension'$option->getFileExtension();
                    
$value['image_size_x'$option->getImageSizeX();
                    
$value['image_size_y'$option->getImageSizeY();
                    
$value['image_alt'$this->getImageAlt();
                    
$value['image_path'$this->getImagePath();
                    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;
    
}
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top