Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 3
Add weight to a product when a custom option is selected? 
 
jinxy
Jr. Member
 
Total Posts:  2
Joined:  2009-05-17
 

Hi,

I hope someone can help me with this as it is driving me crazy!

How can I add weight to an item in my cart if a customer has selected a custom option?

For example, I offer framing for photos as a custom option but need to increase the weight of the item in the basket so the correct shipping charge is calculated using the table rates weight v destination.

Any assistance would be greatly appreciated.

Many thanks. smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
szotyi
Sr. Member
 
Total Posts:  153
Joined:  2008-11-29
 

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…

 
Magento Community Magento Community
Magento Community
Magento Community
 
Snowenvy
Jr. Member
 
Avatar
Total Posts:  3
Joined:  2009-12-17
 

Wow!  This would be so useful in my store.  I added it, just like you said, with no problems, the testing part worked out great!

But now, I can’t add anything to my shopping cart.

I’ve tried using other browsers and all but, it always comes back saying, “ Could not add this item to your cart.”

Any suggestions?

This feature of adding additional weight would come in handy for almost any online store and I think it should be standardized!

 
Magento Community Magento Community
Magento Community
Magento Community
 
ale.sm
Jr. Member
 
Total Posts:  3
Joined:  2010-03-04
 

Hey szoty, thanks for a nice stuff bro!

But i had i problem. i dont get the weight add. i only get the original weight.
in my debugs, i see, probably the problem are in the function $option->getValue()
cuz no value are echoed.

i use magento 1.3 u have some solution ?

thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
jinxy
Jr. Member
 
Total Posts:  2
Joined:  2009-05-17
 

Thanks szotyi for your code, I ended up using bits of it to achieve what I have been trying to do for a while now. Thank you for your input - very helpful.

andrew north -> I had the same error as you when adding items with no custom options to the basket. I found that removing the line “unset($itemOptions2[0]);unset($itemOptions2[1]);” resolved the issue, but the code I am using is heavily modified to the the original posted by szotyi.

Not really sure why this weight field on custom options is not a standard feature.

 
Magento Community Magento Community
Magento Community
Magento Community
 
ale.sm
Jr. Member
 
Total Posts:  3
Joined:  2010-03-04
 
jinxy - 05 March 2010 02:04 AM

Thanks szotyi for your code, I ended up using bits of it to achieve what I have been trying to do for a while now. Thank you for your input - very helpful.

andrew north -> I had the same error as you when adding items with no custom options to the basket. I found that removing the line “unset($itemOptions2[0]);unset($itemOptions2[1]);” resolved the issue, but the code I am using is heavily modified to the the original posted by szotyi.

Not really sure why this weight field on custom options is not a standard feature.

Hy bro !

u can post your step, or u documentation ?

it really be very helpful !

Thanks

ps:. i still having problem , and the script not work.

 
Magento Community Magento Community
Magento Community
Magento Community
 
ale.sm
Jr. Member
 
Total Posts:  3
Joined:  2010-03-04
 

omg ! i can’t believe!

i make debuggs, re-code all, try all solutions using Magento Classes, and now my problem are solved.

well. propably my error is because i have ( i dont knw how ) a value em option_type_id in my mySQL like “ 68’ “
yes a ( ‘ ) are added and i dont know how, so its generates a error in $option->getValue();

and to let the code fine, i add just a litle stuff. not neccessary but i add.

i change to

foreach ($options as $option{

$valueofoption 
$option->getValue();

  
$temp $read->fetchRow("SELECT * FROM catalog_product_option_type_value WHERE option_type_id = '$valueofoption' LIMIT 0, 1");   
  
$optionsWeight += $temp['weight'];

well. now it works perfect! Thanks for share this stuff. is very usefull!

 
Magento Community Magento Community
Magento Community
Magento Community
 
adrienne1983
Jr. Member
 
Total Posts:  2
Joined:  2010-04-24
 

Hi,

I added the code and it works, but I do have one problem.

If a product has a text field custom option, the product cannot be added to the cart if letters are entered into the text field. You can add the product to the cart if numbers are used or if you add single quotes around the letters.

Can anyone help me find a solution to this?

 
Magento Community Magento Community
Magento Community
Magento Community
 
zuiko
Guru
 
Avatar
Total Posts:  304
Joined:  2009-02-06
Nantes France
 

Hello,

I think it could be better that the possibility to add weight to a custom option would be integrated to the core because it’s very usefull and extends the usability of custom options(that are dedicated to options without impact to the stock as everyone knows).

It could be less risky than modifying the database and the core code eachtime you upgrade Magento.

 
Magento Community Magento Community
Magento Community
Magento Community
 
lisali
Enthusiast
 
Avatar
Total Posts:  889
Joined:  2008-04-28
London, UK
 

I agree - I see NO reason why, when you add a custom option inside a simple product, you can specify price etc, but NOT weight?

Please see here for a feature suggestion I made and add your voice:

http://www.magentocommerce.com/bug-tracking/issue?issue=9459

Thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
zuiko
Guru
 
Avatar
Total Posts:  304
Joined:  2009-02-06
Nantes France
 

Comment posted grin

 
Magento Community Magento Community
Magento Community
Magento Community
 
lisali
Enthusiast
 
Avatar
Total Posts:  889
Joined:  2008-04-28
London, UK
 

Thanks so much, Zuiko! Let’s hope they look into it.

 
Magento Community Magento Community
Magento Community
Magento Community
 
zuiko
Guru
 
Avatar
Total Posts:  304
Joined:  2009-02-06
Nantes France
 

It’s an old request yous can see this post :

on 1.1.8
2008-12-04 !!!

 
Magento Community Magento Community
Magento Community
Magento Community
 
lisali
Enthusiast
 
Avatar
Total Posts:  889
Joined:  2008-04-28
London, UK
 

Wow. That’s very unfortunate that the Magento team ignores user requests, and do not even bother to follow up or comment on issues. Almost 2 years later…

 
Magento Community Magento Community
Magento Community
Magento Community
 
zuiko
Guru
 
Avatar
Total Posts:  304
Joined:  2009-02-06
Nantes France
 

I think they saw it but did’nt understood the interest because you can do every thing with configurable products.

But when you don’t need to manage the stock of this particular product configurable products is a hammer to kill a fly !

 
Magento Community Magento Community
Magento Community
Magento Community
 
jessp855
Jr. Member
 
Total Posts:  22
Joined:  2009-10-12
 

This would really help but im trying to apply this tutorial instead of weight i would like to change it to description, can anybody help me how to come with similar solution like weight?

I already tried changing some value like sku to description but it wont save my description.

Thanks!

Image Attachments
custom-option-description.jpg
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 3