Friday 20 June 2014

Add Weight field to cutom option in Magento

1. add new field to the database:
log into phpmyadmin
select the table: catalog_product_option_type_value
click ‘SQL’ and insert

ALTER TABLE  `catalog_product_option_type_valueADD  `weightDECIMAL12) DEFAULT  '0.00' AFTER  `option_id` ;
click ‘go’ 2. add new column in the admin area
app/design/adminhtml/default/default/template/catalog/product/edit/options/type/select.phtml
after this line:

'<th class="type-type"><?php echo Mage::helper('catalog')->__('Price Type') ?></th>'+
insert:
'<th class="type-weight"><?php echo Mage::helper('catalog')->__('Weight') ?></th>'+
after this line:

<td><?php echo $this->getPriceTypeSelectHtml() ?>{{checkboxScopePrice}}</td>+
insert:
'<td><input type="text" class="input-text validate-number product-option-weight" id="product_option_{{id}}_select_{{select_id}}_weight" name="product[options][{{id}}][values][{{select_id}}][weight]" value="{{weight}}"></td>'+
3. add the weight to the array
app/code/core/Mage/Adminhtml/Block/Catalog/Product/Edit/Tab/Options/Option.php
after this line:

'price_type' => $_value->getPriceType(),
insert:
'weight' => $_value->getWeight(),
4. add custom weight(s) to the main product when added to the cart
app/code/core/Mage/Sales/Model/Quote/Item.php
BEFORE this line:

$this->setData('product'$product)
insert
$optionsWeight 0;$options $this->getOptions();
if (
is_array($options)) {unset($options[0]);unset($options[1]); $read Mage::getSingleton('core/resource')->getConnection('core_read');

       
foreach (
$options as $option{
  $temp 
$read->fetchRow('SELECT * FROM `catalog_product_option_type_value` WHERE `option_type_id` = '.$option->getValue().' LIMIT 0, 1');           
  
$optionsWeight += $temp['weight'];}
}
// $optionsWeight = 0; //if you want to turn off
change this:
->setWeight($this->getProduct()->getWeight()
to:
->setWeight($this->getProduct()->getWeight() + $optionsWeight)
for TEST purpose:
app/design/frontend/default/default/template/checkout/cart/item/default.phtml
after this line:

<?php echo $this->getProductName() ?></a>
insert:
<strong>Weight<?php echo $_item->getWeight() ?> RowWeight<?php echo $_item->getRowWeight() ?></strong>
if you finished testing delete the inserted line from default.phtml…

No comments:

Post a Comment