Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Tip: Radio buttons for Product Options


Moufasa2

Recommended Posts

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('"' => '"', '\'' => ''', '<' => '<', '>' => '>')) . '<br>';

}

$field .= '</label>';

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

return $field;

}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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('"' => '"' ,
										'\'' => ''',
										'<' => '<',
										'>' => '>')) .
										'<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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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('"' => '"' ,

'\'' => ''',

'<' => '<', (LINE 41)

'>' => '>')) .

'<br>';

}

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

return $field;

}

Link to comment
Share on other sites

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('"' => '"' ,

'\'' => ''',

'<' => '<', (LINE 41)

'>' => '>')) .

'<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('"' => '"' ,

'\'' => ''',

'<' => '<', (LINE 41)

'>' => '>')) .

'<br>';

 

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 2 months later...

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('"' => '"', '\'' => ''', '<' => '<', '>' => '>')) . '</option><br>';
}
$field .= '</label>';

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

return $field;
 }

Link to comment
Share on other sites

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" . "'");

Link to comment
Share on other sites

  • 2 months later...

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;
 }

Link to comment
Share on other sites

  • 4 weeks later...

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

Link to comment
Share on other sites

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('"' => '"', '\'' => ''', '<' => '<', '>' => '>')) . '</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

Link to comment
Share on other sites

I'm having no luck I get the following error Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' in /home/cubeappl/public_html/includes/functions/html_output.php on line 37

 

I checked this line:

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

 

and it is fine am I doing something wrong?

Link to comment
Share on other sites

$field .= '></p></td><td><p class="main">' . tep_output_string($values[$i]['text'], array('"' => '"', '\'' => '& # 0 3 9; ', '<' => '<', '>' => '>')) . '</p></td></tr>';

 

& # 0 3 9 ; without whitespace

Link to comment
Share on other sites

$field .= '></p></td><td><p class="main">' . tep_output_string($values[$i]['text'], array('"' => '"', '\'' => '& # 0 3 9; ', '<' => '<', '>' => '>')) . '</p></td></tr>';

 

& # 0 3 9 ; without whitespace

 

Thank you so much Qihun, It worked, and also thanks to Moufasa2 and others

Link to comment
Share on other sites

  • 2 months later...
this mod works great, but all the options are pre selected with the LAST button, how can I get this to be set so the FIRST radio button is selected and not the last?

 

I've been trying to figure this out as well. Anyone have any ideas?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...