Adding Textbox Option To Your Products
7 posts
• Page 1 of 1
Adding Textbox Option To Your Products
Step 1
Edit catalog/template/default/content/Product.tpl
Look for the line : :
Insert this below that line:
Look for this code:
Replace that code with:
Step 2
Edit the file library/cart/cart.php
Look for this line of code:
Insert this above that line of code:
And insert this below that line of code:
Step 3
To create a textbox for your product:
1) Create an option with the format [input:optionName]
example: [input:Monogram Initials]
2) Create option values with the format [attribute:attributeValue].
*It is mandatory that you create an option value with the name [type:text].
You can add other textbox attributes, for example,
a. [size:20]
b. [maxlenght:30]
3) Add the option value(s) to your product.
That's it.
Regards,
Troy
Edit catalog/template/default/content/Product.tpl
Look for the line : :
Insert this below that line:
- Code: Select all
<script language="JavaScript" type="text/javascript">
<!--
function chgVal(myVal,hiddenTxt) {
mycntl=document.getElementById(hiddenTxt);
mycntl.value=mycntl.value+myVal;
//alert (mycntl.value);
}
//--> </script>
Look for this code:
- Code: Select all
<?php foreach ($options as $option) { ?>
<tr>
<td><?php echo $option['name']; ?>:</td>
<td><select name="option[<?php echo $option['option_id']; ?>]"> <?php foreach ($option['value'] as $option_value) { ?>
<option value="<?php echo $option_value['product_to_option_id']; ?>"><?php echo $option_value['name']; ?>
<?php if ($option_value['price']) { ?>
<?php echo $option_value['prefix']; ?><?php echo $option_value['price']; ?>
<?php } ?>
</option>
<?php } ?>
</select></td>
</tr>
<?php } ?>
Replace that code with:
- Code: Select all
//===tv start
<?php foreach ($options as $option) {
if ( strpos($option['name'],'[input:') === false ) { ?>
<tr>
<td><?php echo $option['name']; ?>:</td>
<td><select name="option[<?php echo $option['option_id']; ?>]">
<?php foreach ($option['value'] as $option_value) { ?>
<option value="<?php echo $option_value['product_to_option_id']; ?>"><?php echo $option_value['name']; ?>
<?php if ($option_value['price']) { ?>
<?php echo $option_value['prefix']; ?><?php echo $option_value['price']; ?>
<?php } ?>
</option>
<?php } ?>
</select></td>
</tr>
<?php
} else {
?>
<tr><td>
<?php $tv_input=str_replace('[input:','', $option['name']);
$tv_input=str_replace(']','', $tv_input);
$tv_input=str_replace('[',' :</td><td> <input ', $tv_input);
$tv_input.=' :</td><td> <input ';
foreach ($option['value'] as $option_value) {
$tv_params=str_replace('[',' ',$option_value['name']);
$tv_params=str_replace(':','="',$tv_params);
$tv_params=str_replace(']','" ',$tv_params);
$tv_input.=$tv_params;
}
//
$tv_input.='name="temp['.$option['option_id'].']" onBlur="chgVal(this.value,\'option'.$option['option_id'].'\')" >';
$tv_input.='<input type="hidden" name="option['.$option['option_id'].']" id="option'.$option['option_id'].'"';
$tv_input.=' value="'.$option_value['product_to_option_id'].'~"> </td></tr>';
echo $tv_input;
} //=== end if
} //=== end options ?>
//===tv end
Step 2
Edit the file library/cart/cart.php
Look for this line of code:
- Code: Select all
$option = $this->database->getRow("select o.name as name, ov.name as `value`, p2o.price, p2o.prefix from product_to_option p2o left join `option` o on p2o.option_id = o.option_id left join option_value ov on p2o.option_value_id = ov.option_value_id where p2o.product_to_option_id = '" . $tv_ptoid . "' and product_id = '" . (int)$product_id . "' and o.language_id = '" . (int)$this->language->getId() . "' and ov.language_id = '" . (int)$this->language->getId() . "'");
Insert this above that line of code:
- Code: Select all
//===tv
$tv_ptoid=strval($product_to_option_id);
$tv_pos=strpos($tv_ptoid,'~');
if ($tv_pos === false) {
} else {
$tv_optVal=substr($tv_ptoid,$tv_pos+1);
$product_to_option_id=substr($tv_ptoid,0,$tv_pos);
}
//===tv end
And insert this below that line of code:
- Code: Select all
//===tv
if ($tv_pos !== false ){
$option['name']=str_replace('input:','',$option['name'])
$option['name']=str_replace(']','',$option['name']);
$option['value']=$tv_optVal;
}
$option['name'].=': ';
//===tv end
Step 3
To create a textbox for your product:
1) Create an option with the format [input:optionName]
example: [input:Monogram Initials]
2) Create option values with the format [attribute:attributeValue].
*It is mandatory that you create an option value with the name [type:text].
You can add other textbox attributes, for example,
a. [size:20]
b. [maxlenght:30]
3) Add the option value(s) to your product.
That's it.
Regards,
Troy
==========================================================================
Veluzar Studios
Web Design and Development
Veluzar Studios
Web Design and Development
- troyveluz
- Posts: 20
- Joined: Wed Jul 16, 2008 5:45 pm
- Location: Frisco, TX USA
Re: Adding Textbox Option To Your Products
any chance of a screenshot to show us??
cheers mate!
cheers mate!

visit my mobile phone store! ---> http://m-shop.guardhost.net
- anmp3r
- Posts: 21
- Joined: Sun Apr 20, 2008 2:48 pm
Re: Adding Textbox Option To Your Products
You can find the demo at my test opencart site.
Here is the url to product 1 with a textbox and other options:
http://www.veluzarwebdesign.com/octplt/ ... oduct_id=1
Here is the url to product 2 with two texboxes:
http://www.veluzarwebdesign.com/octplt/ ... oduct_id=2
Regards,
Troy
Here is the url to product 1 with a textbox and other options:
http://www.veluzarwebdesign.com/octplt/ ... oduct_id=1
Here is the url to product 2 with two texboxes:
http://www.veluzarwebdesign.com/octplt/ ... oduct_id=2
Regards,
Troy
==========================================================================
Veluzar Studios
Web Design and Development
Veluzar Studios
Web Design and Development
- troyveluz
- Posts: 20
- Joined: Wed Jul 16, 2008 5:45 pm
- Location: Frisco, TX USA
Re: Adding Textbox Option To Your Products
I am not quite sure if this is what I am looking for or not but I would like
to have a textbox where my clients can type in.
Place: Each product page. In the available option section. (I want: not just only a drop down but also a text box)
Is this possible with this code? And if so, I didn't get it where you said
""Create an option with the format [input:optionName]""
Do this in Admin?
Thank you,
to have a textbox where my clients can type in.
Place: Each product page. In the available option section. (I want: not just only a drop down but also a text box)
Is this possible with this code? And if so, I didn't get it where you said
""Create an option with the format [input:optionName]""
Do this in Admin?
Thank you,
- alice
- Posts: 16
- Joined: Thu Jun 19, 2008 9:29 am
Re: Adding Textbox Option To Your Products
Troy:
I was looking for this functionality and your post is great. Thanks. For other non-php folks, I didn't get it work exactly as posted - showed a blank page at first. I realized after looking carefully that a line was missing a semicolon. The last code section in step 2 showed the following:
It should have been:
Only other thing I noticed was there were php comments outside of php tags in the product.tpl code section
To make it all clean you'll want to either wrap these in php tags or just delete them.
Troy - you're the man. Thanks for this valuable post.
Regards,
Kevin G.
I was looking for this functionality and your post is great. Thanks. For other non-php folks, I didn't get it work exactly as posted - showed a blank page at first. I realized after looking carefully that a line was missing a semicolon. The last code section in step 2 showed the following:
- Code: Select all
//===tv
if ($tv_pos !== false ){
$option['name']=str_replace('input:','',$option['name'])
$option['name']=str_replace(']','',$option['name']);
$option['value']=$tv_optVal;
}
$option['name'].=': ';
//===tv end
It should have been:
- Code: Select all
//===tv
if ($tv_pos !== false ){
$option['name']=str_replace('input:','',$option['name']); <-----the missing semicolon
$option['name']=str_replace(']','',$option['name']);
$option['value']=$tv_optVal;
}
$option['name'].=': ';
//===tv end
Only other thing I noticed was there were php comments outside of php tags in the product.tpl code section
- Code: Select all
//===tv
...
//===tv end
To make it all clean you'll want to either wrap these in php tags or just delete them.
Troy - you're the man. Thanks for this valuable post.
Regards,
Kevin G.
-

kgrace - Posts: 15
- Joined: Wed Dec 31, 2008 5:57 am
Re: Adding Textbox Option To Your Products
I started to ask is there a way to make options required - then I realized they are required once assigned by default. So -
Anyone know an easy way to make sure these text fields actually have data. I assume you'd have to use something driven on the "onClick" or "onFocus" add to cart button event using javascript..
maybe someone might have a better idea?? Maybe checking the option value in the php code before to see if it's null?? how hard would that be? any other ideas?
My problem - have text field options using the information in this topic - but like to make some of them required fields before you can add the product to cart.
Many thanks,
Kevin
Anyone know an easy way to make sure these text fields actually have data. I assume you'd have to use something driven on the "onClick" or "onFocus" add to cart button event using javascript..
maybe someone might have a better idea?? Maybe checking the option value in the php code before to see if it's null?? how hard would that be? any other ideas?
My problem - have text field options using the information in this topic - but like to make some of them required fields before you can add the product to cart.
Many thanks,
Kevin
-

kgrace - Posts: 15
- Joined: Wed Dec 31, 2008 5:57 am
Re: Adding Textbox Option To Your Products
I have tried to get this to work and keep throwing errors, it seems it is conflicting with another product option (the upload file product option), would anyone be willing to custom code a feature like this for some paypal cash? PM me if interested and let me know your price.
- cparkner01
- Posts: 3
- Joined: Fri Feb 06, 2009 8:57 pm
7 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 3 guests













