Jump to content


Corporate Sponsors


Latest News: (loading..)

* * * * * 1 votes

Tip: Radio buttons for Product Options


31 replies to this topic

#1 Moufasa2

  • Community Member
  • 5 posts
  • Real Name:Barney Chastain

Posted 05 October 2005, 20:54

The purpose of this code is to give you the ability to display product attributes & product options as radio buttons instead of as a drop-down menu. This is free to anyone, but please keep my information in the comments. Good luck!

1. Go to /catalog/product_info.php. Search and replace this code:

<td class="main"><?php echo tep_draw_pull_down_menu('id[' . $products_options_name['products_options_id'] . ']', $products_options_array, $selected_attribute); ?></td>

with this code:

<td class="main"><?php echo tep_draw_radio_menu('id[' . $products_options_name['products_options_id'] . ']', $products_options_array, $selected_attribute); ?></td>


2. Go to /catalog/inclues/functions/html_output.php and add the following code anywhere outside of other functions. Your done.

/*
FUNCTION "tep_draw_radio_menu" for use with osCommerce

Resides: /catalog/includes/functions/html_output.php
Called from: /catalog/product_info.php
Function modified from function "tep_draw_pull_down_menu"
Purpose: create radio buttons for product options instead of drop-down
10/04/05 - Barney Chastain - www.ashrava.com

*/


function tep_draw_radio_menu($name, $values, $default = '', $parameters = '', $required = false)
{
$field = '<label>';
if (tep_not_null($parameters)) $field .= ' ' . $parameters;
if (empty($default) && isset($GLOBALS[$name])) $default = stripslashes($GLOBALS[$name]);

for ($i=0, $n=sizeof($values); $i<$n; $i++) {
$field .= '<input type="radio" value="' . tep_output_string($values[$i]['id']) . '"';
$field .= ' name="' . tep_output_string($name) . '"';
if (tep_not_null($parameters)) $field .= ' ' . $parameters;

if ($default == $values[$i]['id']) {
$field .= ' SELECTED';
}
$field .= '>' . tep_output_string($values[$i]['text'], array('"' => '&quot;', '\'' => ''', '<' => '&lt;', '>' => '&gt;')) . '<br>';
}
$field .= '</label>';
if ($required == true) $field .= TEXT_FIELD_REQUIRED;
return $field;
}

Edited by Moufasa2, 05 October 2005, 20:56.


#2 OSCnewbie

  • Community Member
  • 39 posts
  • Real Name:Pamela
  • Location:Sunny Arizona

Posted 07 October 2005, 05:43

Moufasa2,

Thank you for your hard work!

Did you know...

There is a contribution that will accomplish this, as well as checkboxes, text boxes and textarea fields. It's called "Product Attributes - Option Type Features." You can download it here:
http://www.oscommerce.com/community/contributions,160

I think it works great.

BUT, would you happen to know how to get product option fields to become REQUIRED FIELDS when added as attributes to a product? (I've been searching and asking about this all day long! )????

Thanks,

--OSCnewbie

#3 wheeloftime

  • Community Member
  • 2,338 posts
  • Real Name:Howard
  • Gender:Male
  • Location:Universe

Posted 07 October 2005, 07:18

Thanks for this ! If you don't need the whole option set features this is a nice replacement for the pull down menu.
I do have some problems with it though:
This part gave me an error
'\'' => '''
until I changed it to
'\'' => '''

Now the display is right but only the first option is selectable ?!
Hope you have a fix for this.

#4 wheeloftime

  • Community Member
  • 2,338 posts
  • Real Name:Howard
  • Gender:Male
  • Location:Universe

Posted 07 October 2005, 08:47

View Postwheeloftime, on Oct 7 2005, 09:18 AM, said:

Thanks for this ! If you don't need the whole option set features this is a nice replacement for the pull down menu.
I do have some problems with it though:
This part gave me an error
'\'' => '''
until I changed it to
'\'' => '''

Now the display is right but only the first option is selectable ?!
Hope you have a fix for this.

Okay, found the problem with the selection. Change the function to
/*
FUNCTION "tep_draw_radio_menu" for use with osCommerce

Resides: /catalog/includes/functions/html_output.php
Called from: /catalog/product_info.php
Function modified from function "tep_draw_pull_down_menu"
Purpose: create radio buttons for product options instead of drop-down
10/04/05 - Barney Chastain - www.ashrava.com

*/


function tep_draw_radio_menu($name, $values, $default = '', $parameters = '', $required = false)
{
$field = '';
if (tep_not_null($parameters)) $field .= ' ' . $parameters;
if (empty($default) && isset($GLOBALS[$name])) $default = stripslashes($GLOBALS[$name]);

for ($i=0, $n=sizeof($values); $i<$n; $i++) {
$field .= '<input type="radio" value="' . tep_output_string($values[$i]['id']) . '"';
$field .= ' name="' . tep_output_string($name) . '"';
if (tep_not_null($parameters)) $field .= ' ' . $parameters;

	if ($default == $values[$i]['id']) {
	$field .= ' checked';
	}
	$field .= '>' . tep_output_string($values[$i]['text'], array('"' => '&quot;' ,
											'\'' => ''',
											'<' => '&lt;',
											'>' => '&gt;')) .
											'<br>';
}
if ($required == true) $field .= TEXT_FIELD_REQUIRED;
return $field;
}

The <label> tag isn't needed and if you use it anyway the </label> must be like '<br></label>'.
The $field .= ' SELECTED '; doesn't work for radio buttons and should be $field .= ' checked ';

To have the first radio button automatically selected you have to change
		if (isset($cart->contents[$HTTP_GET_VARS['products_id']]['attributes'][$products_options_name['products_options_id']])) {
		  $selected_attribute = $cart->contents[$HTTP_GET_VARS['products_id']]['attributes'][$products_options_name['products_options_id']];
		} else {
		  $selected_attribute = false;
		}
in
		if (isset($cart->contents[$HTTP_GET_VARS['products_id']]['attributes'][$products_options_name['products_options_id']])) {
		  $selected_attribute = $cart->contents[$HTTP_GET_VARS['products_id']]['attributes'][$products_options_name['products_options_id']];
		} else {
		  $selected_attribute = 1;
		}
This has no further impact for when you still use the tep_draw_pull_down_menu so this seems better anyway to always have the first option selected by default.

Edited by wheeloftime, 07 October 2005, 08:48.


#5 wheeloftime

  • Community Member
  • 2,338 posts
  • Real Name:Howard
  • Gender:Male
  • Location:Universe

Posted 07 October 2005, 08:57

Damn. only one edit allowed...
I just noticed that the change for '\'' => ''' is not shown as it should (probably why it was already okay in the first post).
It should be
'\'' => '& # 0 3 9 ;' without the spaces in between

#6 Moufasa2

  • Community Member
  • 5 posts
  • Real Name:Barney Chastain

Posted 07 October 2005, 14:08

Thank you for comments on this, especially you wheeloftime!

#7 Mickey Monk

  • Community Member
  • 10 posts
  • Real Name:Mickey Monk

Posted 08 October 2005, 23:55

Hi

When I try to add this I get the following error message.

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' in /home/sites/cartridge-king.co.uk/public_html/shop/includes/functions/html_output.php on line 327

Any help would be much appreciated.

#8 wheeloftime

  • Community Member
  • 2,338 posts
  • Real Name:Howard
  • Gender:Male
  • Location:Universe

Posted 09 October 2005, 04:43

View PostMickey Monk, on Oct 9 2005, 01:55 AM, said:

Hi

When I try to add this I get the following error message.

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' in /home/sites/cartridge-king.co.uk/public_html/shop/includes/functions/html_output.php on line 327

Any help would be much appreciated.

Mickey,

Paste your line 327, with the lines of code above and below, here so we can have a look

#9 Mickey Monk

  • Community Member
  • 10 posts
  • Real Name:Mickey Monk

Posted 09 October 2005, 12:18

I moved the bit of code to make it easier.

This is the error.

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' in /home/sites/cartridge-king.co.uk/public_html/shop/includes/functions/html_output.php on line 41


if ($default == $values[$i]['id']) {
$field .= ' checked';
}
$field .= '>' . tep_output_string($values[$i]['text'], array('"' => '&quot;' ,
'\'' => ''',
'<' => '&lt;', (LINE 41)
'>' => '&gt;')) .
'<br>';
}
if ($required == true) $field .= TEXT_FIELD_REQUIRED;
return $field;
}

#10 wheeloftime

  • Community Member
  • 2,338 posts
  • Real Name:Howard
  • Gender:Male
  • Location:Universe

Posted 09 October 2005, 12:51

View PostMickey Monk, on Oct 9 2005, 02:18 PM, said:

I moved the bit of code to make it easier.

This is the error.

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' in /home/sites/cartridge-king.co.uk/public_html/shop/includes/functions/html_output.php on line 41
if ($default == $values[$i]['id']) {
$field .= ' checked';
}
$field .= '>' . tep_output_string($values[$i]['text'], array('"' => '&quot;' ,
'\'' => ''',
'<' => '&lt;', (LINE 41)
'>' => '&gt;')) .
'<br>';
}
if ($required == true) $field .= TEXT_FIELD_REQUIRED;
return $field;
}

I don't see any error so quickly in this part ! How did you find the line 41 ? I ask because this whole part

$field .= '>' . tep_output_string($values[$i]['text'], array('"' => '&quot;' ,
'\'' => ''',
'<' => '&lt;', (LINE 41)
'>' => '&gt;')) .
'<br>';

is one line. Can you paste this whole functions as you have put it in html_output.php

Edited by wheeloftime, 09 October 2005, 12:53.


#11 Mickey Monk

  • Community Member
  • 10 posts
  • Real Name:Mickey Monk

Posted 09 October 2005, 13:11

Thanks....Wheeloftime....I have sorted it now

Edited by Mickey Monk, 09 October 2005, 13:15.


#12 Mickey Monk

  • Community Member
  • 10 posts
  • Real Name:Mickey Monk

Posted 09 October 2005, 13:26

Just one more thing...

I can't seem to get the first radio button to be highlighted.

This is the code in html_output.php
/*
FUNCTION "tep_draw_radio_menu" for use with osCommerce

Resides: /catalog/includes/functions/html_output.php
Called from: /catalog/product_info.php
Function modified from function "tep_draw_pull_down_menu"
Purpose: create radio buttons for product options instead of drop-down
10/04/05 - Barney Chastain - www.ashrava.com

*/


function tep_draw_radio_menu($name, $values, $default = '', $parameters = '', $required = false) 
{
$field = '';
if (tep_not_null($parameters)) $field .= ' ' . $parameters;
if (empty($default) && isset($GLOBALS[$name])) $default = stripslashes($GLOBALS[$name]);

for ($i=0, $n=sizeof($values); $i<$n; $i++) {
$field .= '<input type="radio" value="' . tep_output_string($values[$i]['id']) . '"';
$field .= ' name="' . tep_output_string($name) . '"';
if (tep_not_null($parameters)) $field .= ' ' . $parameters;

if ($default == $values[$i]['id']) {
$field .= ' checked';
}
$field .= '>' . tep_output_string($values[$i]['text'], array('"' => '"', '\'' =>''', '<' => '<', '>' => '>')) . '<br>';
}
if ($required == true) $field .= TEXT_FIELD_REQUIRED;
return $field;
}

and in product_info.php
  if (isset($cart->contents[$HTTP_GET_VARS['products_id']]['attributes'][$products_options_name['products_options_id']])) {
		  $selected_attribute = $cart->contents[$HTTP_GET_VARS['products_id']]['attributes'][$products_options_name['products_options_id']];
		} else {
		  $selected_attribute = 1;
		}
?>
			<tr>
			  <td class="main"><?php echo $products_options_name['products_options_name'] . ':'; ?></td>
			  <td class="main"><?php echo tep_draw_radio_menu('id[' . $products_options_name['products_options_id'] . ']', $products_options_array, $selected_attribute); ?></td>
			</tr>


#13 wheeloftime

  • Community Member
  • 2,338 posts
  • Real Name:Howard
  • Gender:Male
  • Location:Universe

Posted 09 October 2005, 14:29

View PostMickey Monk, on Oct 9 2005, 03:26 PM, said:

Just one more thing...

I can't seem to get the first radio button to be highlighted.

This is the code in html_output.php
/*
FUNCTION "tep_draw_radio_menu" for use with osCommerce

Resides: /catalog/includes/functions/html_output.php
Called from: /catalog/product_info.php
Function modified from function "tep_draw_pull_down_menu"
Purpose: create radio buttons for product options instead of drop-down
10/04/05 - Barney Chastain - www.ashrava.com

*/
function tep_draw_radio_menu($name, $values, $default = '', $parameters = '', $required = false) 
{
$field = '';
if (tep_not_null($parameters)) $field .= ' ' . $parameters;
if (empty($default) && isset($GLOBALS[$name])) $default = stripslashes($GLOBALS[$name]);

for ($i=0, $n=sizeof($values); $i<$n; $i++) {
$field .= '<input type="radio" value="' . tep_output_string($values[$i]['id']) . '"';
$field .= ' name="' . tep_output_string($name) . '"';
if (tep_not_null($parameters)) $field .= ' ' . $parameters;

if ($default == $values[$i]['id']) {
$field .= ' checked';
}
$field .= '>' . tep_output_string($values[$i]['text'], array('"' => '"', '\'' =>''', '<' => '<', '>' => '>')) . '<br>';
}
if ($required == true) $field .= TEXT_FIELD_REQUIRED;
return $field;
}

and in product_info.php
  if (isset($cart->contents[$HTTP_GET_VARS['products_id']]['attributes'][$products_options_name['products_options_id']])) {
		  $selected_attribute = $cart->contents[$HTTP_GET_VARS['products_id']]['attributes'][$products_options_name['products_options_id']];
		} else {
		  $selected_attribute = 1;
		}
?>
			<tr>
			  <td class="main"><?php echo $products_options_name['products_options_name'] . ':'; ?></td>
			  <td class="main"><?php echo tep_draw_radio_menu('id[' . $products_options_name['products_options_id'] . ']', $products_options_array, $selected_attribute); ?></td>
			</tr>

It's the same I have so that is strange ?! How many option sets have you with your test product ?
I have only one and just tried with a second and only the first option of the first set is selected by default.

#14 Mickey Monk

  • Community Member
  • 10 posts
  • Real Name:Mickey Monk

Posted 09 October 2005, 14:56

Only 1 option set with 3 options.

Also how do I sort the options into the order I want?

#15 wheeloftime

  • Community Member
  • 2,338 posts
  • Real Name:Howard
  • Gender:Male
  • Location:Universe

Posted 09 October 2005, 15:17

View PostMickey Monk, on Oct 9 2005, 04:56 PM, said:

Only 1 option set with 3 options.

Also how do I sort the options into the order I want?

For sorting attributes you'll have to look within the contribution section. I know there are several but as I don't use these I have never really looked into them.

I don't have an explanation why your first option isn't selected with the same setup as mine but maybe you have some attribute contrib installed which I haven't.

#16 Moufasa2

  • Community Member
  • 5 posts
  • Real Name:Barney Chastain

Posted 21 December 2005, 20:25

I just found a way to make it easier. The only change on product_info.php is the function name. Add the following to html_output.php and your "attributes" will show up on the cart and invoice:

// Output a radio button menu
// Barney Chastain, December 2005
  function tep_radiobutton_menu($name, $values, $default = '', $parameters = '', $required = false) {
	$field = '<label name="' . tep_output_string($name) . '"';

	if (tep_not_null($parameters)) $field .= ' ' . $parameters;

	$field .= '>';

	if (empty($default) && isset($GLOBALS[$name])) $default = stripslashes($GLOBALS[$name]);

	for ($i=0, $n=sizeof($values); $i<$n; $i++) {
	  $field .= '<input type="radio" name="' . tep_output_string($name) .  '" value="' . tep_output_string($values[$i]['id']) . '"';
	  if ($default == $values[$i]['id']) {
		$field .= ' SELECTED';
	  }

	  $field .= '>' . tep_output_string($values[$i]['text'], array('"' => '&quot;', '\'' => ''', '<' => '&lt;', '>' => '&gt;')) . '</option><br>';
	}
	$field .= '</label>';

	if ($required == true) $field .= TEXT_FIELD_REQUIRED;

	return $field;
  }


#17 Moufasa2

  • Community Member
  • 5 posts
  • Real Name:Barney Chastain

Posted 21 December 2005, 21:26

As for sorting the product attributes/options, just replace the "$products_options_query" around line 120 on product_info.php.

Find:

$products_options_query = tep_db_query("select pov.products_options_values_id, pov.products_options_values_name, pa.options_values_price, pa.price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov where pa.products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "' and pa.options_id = '" . (int)$products_options_name['products_options_id'] . "' and pa.options_values_id = pov.products_options_values_id and pov.language_id = '" . (int)$languages_id . "'");

Change to:

$products_options_query = tep_db_query("select pov.products_options_values_id, pov.products_options_values_name, pa.options_values_price, pa.price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov where pa.products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "' and pa.options_id = '" . (int)$products_options_name['products_options_id'] . "' and pa.options_values_id = pov.products_options_values_id and pov.language_id = '" . (int)$languages_id . "ORDER BY pov.products_options_values_name ASC" . "'");


#18 Moufasa2

  • Community Member
  • 5 posts
  • Real Name:Barney Chastain

Posted 17 March 2006, 14:35

I've made some changes to my original code. This forces the last option to be selected and creates valid HTML.


// Output a form pull down menu
// Barney Chastain, Mar 2006
// www.ashrava.com
  function tep_radiobutton_menu($name, $values, $default = '', $parameters = '', $required = false) {
	$field = '<table border="0" cellpadding="2" cellspacing="0"><label name="' . tep_output_string($name) . '"';

	if (tep_not_null($parameters)) $field .= ' ' . $parameters;

	$field .= '>';

	if (empty($default) && isset($GLOBALS[$name])) $default = stripslashes($GLOBALS[$name]);

	for ($i=0, $n=sizeof($values); $i<$n; $i++) {
	  $field .= '<tr><td><p class="main"><input checked type="radio" name="' . tep_output_string($name) .  '" value="' . tep_output_string($values[$i]['id']) . '"';
	  if ($default == $values[$i]['id']) {
		$field .= '   ';
	  }

	  $field .= '></p></td><td><p class="main">' . tep_output_string($values[$i]['text'], array('"' => '"', '\'' => ''', '<' => '<', '>' => '>')) . '</p></td></tr>';
	}
	$field .= '</label></table>';

	if ($required == true) $field .= TEXT_FIELD_REQUIRED;
	
	return $field;
  }


#19 slimim

  • Community Member
  • 28 posts
  • Real Name:Imran

Posted 15 April 2006, 00:30

I have followed the exact requests as above but I am getting the exact error messages that Mickey was getting, Please could you advise what the fix was? i.e. Parse error in html_output, just for the post is it possible to explain the contribution again and what files need to be modified, cheers

#20 Qihun

  • Community Member
  • 561 posts
  • Real Name:miclosh
  • Location:planet Earth

Posted 15 April 2006, 03:46

you have parse error because posting form on this forum replace tags with simbols
in this line
$field .= '></p></td><td><p class="main">' . tep_output_string($values[$i]['text'], array('"' => '&quot;', '\'' => ''', '<' => '&lt;', '>' => '&gt;')) . '</p></td></tr>';



this is function in catalog/includes/html_output.php

taken from last post of Moufasa2 ( thank you very much , great help to me )

function tep_radiobutton_menu($name, $values, $default = '', $parameters = '', $required = false) {
$field = '<table border="0" cellpadding="2" cellspacing="0"><label name="' . tep_output_string($name) . '"';

if (tep_not_null($parameters)) $field .= ' ' . $parameters;

$field .= '>';

if (empty($default) && isset($GLOBALS[$name])) $default = stripslashes($GLOBALS[$name]);

for ($i=0, $n=sizeof($values); $i<$n; $i++) {
$field .= '<tr><td><p class="main"><input checked type="radio" name="' . tep_output_string($name) . '" value="' . tep_output_string($values[$i]['id']) . '"';
if ($default == $values[$i]['id']) {
$field .= ' ';
}

$field .= '></p></td><td><p class="main">' . tep_output_string($values[$i]['text'], array('"' => '"', '\'' => ''', '<' => '<', '>' => '>')) . '</p></td></tr>';
}
$field .= '</label></table>';

if ($required == true) $field .= TEXT_FIELD_REQUIRED;

return $field;
}

it called tep_radiobutton_menu , so , you need to modify catalog/product_info.php

find there
<td class="main"><?php echo tep_draw_pull_down_menu('id[' . $products_options_name['products_options_id'] . ']', $products_options_array, $selected_attribute); ?></td>

and replace with
<td  align="CENTER" class="main"><?php echo tep_radiobutton_menu('id[' . $products_options_name['products_options_id'] . ']', $products_options_array, $selected_attribute); ?></td>

thats all , now instead of pull_down menu you have options listed with radio_buttons next to them