Zen Cart Logo
Forums / General Questions / Option Value URL parameter?

Option Value URL parameter?

Views: 2,799

Results 1 to 14 of 14
8 Aug 2014, 12:21 PM
#1
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Option Value URL parameter?

Hi All,

Is there a url parameter built into zencart where by if you add it to the product page url it can pre select the option?

for example if option values were

1 - small
2 - medium
3 - large

is there a param I can add to a product page so that medium is pre-selected?

eg:

site.com/index.php?main_page=product_info&products_id=44&optionselected=2

Cheers

Phil

8 Aug 2014, 1:34 PM
#2
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Option Value URL parameter?

philip937:

Hi All,

Is there a url parameter built into zencart where by if you add it to the product page url it can pre select the option?

for example if option values were

1 - small
2 - medium
3 - large

is there a param I can add to a product page so that medium is pre-selected?

eg:

site.com/index.php?main_page=product_info&products_id=44&optionselected=2

Cheers

Phil

Not by design. Usually the default attribute is identified in the attributes controller on a per product basis. The recommended setup is to add a read only, default attribute that is not one of the product attributes to prevent the accidental selection of the attribute and instead require the customer to purposefully select one of the attributes.

Could you identify the reason for needing a url version of assigning such a default, perhaps some assistance could be provided as a result?

8 Aug 2014, 1:46 PM
#3
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: Option Value URL parameter?

I wish to provide a link that is specific to the size required, as per the requirement detailed above.

As there is nothing built in I will add some custom code to use php GET from the parameter and set the "selected" attribute to the drop down box where the selected attribute is relevant.

8 Aug 2014, 1:59 PM
#4
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: Option Value URL parameter?

So im thinking, if the ouput is liek this:

<select name="id[3]" id="attrib-3">
  <option value="2">Choose Size..</option>
  <option value="5">Medium</option>
  <option value="6">Large</option>
</select>

The default selected is the top (which is defined by the sort order which controls the order they are written to the page.

So if the url has ?optionselected=6

I could use a php GET and use the result to manipulate the select box liek this:

<select name="id[3]" id="attrib-3">
  <option value="2">Choose Size..</option>
  <option value="5">Medium</option>
  <option value="6" selected>Large</option>
</select>

so im guessing it'll be a variable that is set to nothing by default, for example
$selected = "";
and then
if option value == GET optionselected {
$selected = "selected";
}

What d'ya think?

8 Aug 2014, 2:23 PM
#5
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Option Value URL parameter?

philip937:

So im thinking, if the ouput is liek this:

<select name="id[3]" id="attrib-3"> <option value="2">Choose Size..</option> <option value="5">Medium</option> <option value="6">Large</option> </select> ``` > > The default selected is the top (which is defined by the sort order which controls the order they are written to the page. > > So if the url has ?optionselected=6 > > I could use a php GET and use the result to manipulate the select box liek this: > > ```html <select name="id[3]" id="attrib-3"> <option value="2">Choose Size..</option> <option value="5">Medium</option> <option value="6" selected>Large</option> </select> ``` > > so im guessing it'll be a variable that is set to nothing by default, for example > $selected = ""; > and then > if option value == GET optionselected { > $selected = "selected"; > } > > What d'ya think?

Couple of things, would want to consider the situation where the default (selected) is identified as something other than the first item/is an item that is to be selected ahead of the desired item to select.

Also, regarding your check on the get, you want to be sure that the data passed via the get doesn't get abused to do something undesirable, so a couple of things come to mind and may not be all inclusive. Check/verify if the get variable is set, then want to send the variable through the ZC html cleaning function (sorry I have forgotten what it is called) before attempting to validat it's value(s). In the value checks maintain positive control of what has been provided ie, do something for each selection and if not part of that, then basically disregard the value. Don't rely on the data when choosing the third of three options to necessarily mean that since not one of the first 2 that it must be the third option.

Assuming that the link is something to be "passed out" or posted elsewhere, shouldn't be too much of a problem; however, if the link is to be autogenerated by ZC for display to the customer, that would require additional code to handle that.

Regarding the code suggested, I would assign a variable to the value of the get, do. The cleanup on it, then inside the loop of the attributes, yes that sequence appears as if it would work.

8 Aug 2014, 2:50 PM
#6
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Option Value URL parameter?

Thinking about it some more, I believe that unless the sort order equals the option_value_id that the numerical sequence of the values does not equate to the sort order. (Ie, could be just as possible that the numbers are reversed but the resulting size description is the same.

15 Aug 2014, 12:12 PM
#7
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: Option Value URL parameter?

mc12345678:

Thinking about it some more, I believe that unless the sort order equals the option_value_id that the numerical sequence of the values does not equate to the sort order. (Ie, could be just as possible that the numbers are reversed but the resulting size description is the same.

not sure what you mean by this?

from what I can see the sort order on the attributes is only used to organise the order of the output to the page.

the attribute id is unique to each field, therefore if there was some code added inlide to the bit that creates the drop down that does a check on each through the loop:

$selected = "";
if ($option_value_id == GET['optionselected']) {
$selected = "selected";
}

then which ever attribute was the match would have the "selected" added to the HTML ?

15 Aug 2014, 12:24 PM
#8
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Option Value URL parameter?

philip937:

not sure what you mean by this?

from what I can see the sort order on the attributes is only used to organise the order of the output to the page.

the attribute id is unique to each field, therefore if there was some code added inlide to the bit that creates the drop down that does a check on each through the loop:

$selected = "";
if ($option_value_id == GET['optionselected']) {
$selected = "selected";
}

> then which ever attribute was the match would have the "selected" added to the HTML ?

In your previous post, you had this:
> The default selected is the top (which is defined by the sort order which controls the order they are written to the page.

Without further discussion/explanation, I didn't want to imply that just because something appears first in the list that it is necessarily to be identified as the default.  It was not identified that the option that happened to be first in the list also happened to be the default. 

Further, if in the ZC attributes controller a default option is selected, and then the method applied above of passing a "default" selection is used, if the desired default is below in sort order the attribute controller default, the passed in default/selected will be the selected option, or possibly both may end up selected (haven't tested that thought), either way, the result would be incorrect for the desired action.  Prior to assigning the passed in selection, other selections in the list should be "unselected" as a common practice, although the code cycle could be shortened to only unselect those items that appear above the passed in selection assuming that the passed in selection exists in the list.

So, sorry for the confusion, but the statement was made in relation to the above quoted portion of the previous post.
15 Aug 2014, 12:31 PM
#9
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: Option Value URL parameter?

my understanding is that by default the initially selected option isnt selected as such it simply displays the first in the list so in this case:

<select name="id[3]" id="attrib-3">
  <option value="2">Choose Size..</option>
  <option value="5">Medium</option>
  <option value="6">Large</option>
</select>

the option "Choose Size.." is writted to the page first becasuse it has the lowest sort value.

the sort value is irrelevant in what I am trying to achieve becase I plan to utilise the HTML "selected" attribute to make the option I want selected. So in the exaple the order of the list is the same but because Large has the HTML "selected" attribute, when the page loads the large should be pre-selected:

<select name="id[3]" id="attrib-3">
  <option value="2">Choose Size..</option>
  <option value="5">Medium</option>
  <option value="6" selected>Large</option>
</select>

does that sound right or am I completly misunderstanding?

15 Aug 2014, 12:40 PM
#10
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Option Value URL parameter?

philip937:

my understanding is that by default the initially selected option isnt selected as such it simply displays the first in the list so in this case:

<select name="id[3]" id="attrib-3"> <option value="2">Choose Size..</option> <option value="5">Medium</option> <option value="6">Large</option> </select> ``` > > the option "Choose Size.." is writted to the page first becasuse it has the lowest sort value. > > the sort value is irrelevant in what I am trying to achieve becase I plan to utilise the HTML "selected" attribute to make the option I want selected. So in the exaple the order of the list is the same but because Large has the HTML "selected" attribute, when the page loads the large should be pre-selected: > > ```html <select name="id[3]" id="attrib-3"> <option value="2">Choose Size..</option> <option value="5">Medium</option> <option value="6" selected>Large</option> </select> ``` > > does that sound right or am I completly misunderstanding?

For the "response" action that you are seeing you are correct; however, the expectation when setting up a first attribute like "Please select..." Is that it is identified as both read only and default. (Therefore would be selected when seen on screen.). So yes, the way the cart is setup (incorrectly as compared to information available in the FAQ for attributes), assigning the selected "large" as the selection would work until someone comes by later and changes/decides on say that first option to be the default/selected. At that point as said before if action is not taken to unselect that option before selecting the desired default, the desired default will not be selected as expected.

15 Aug 2014, 12:44 PM
#11
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: Option Value URL parameter?

mc12345678:

For the "response" action that you are seeing you are correct; however, the expectation when setting up a first attribute like "Please select..." Is that it is identified as both read only and default. (Therefore would be selected when seen on screen.). So yes, the way the cart is setup (incorrectly as compared to information available in the FAQ for attributes), assigning the selected "large" as the selection would work until someone comes by later and changes/decides on say that first option to be the default/selected. At that point as said before if action is not taken to unselect that option before selecting the desired default, the desired default will not be selected as expected.

I confused matters by including "Please Select.." my appologies, this also irrelevant. imagine the Please Select.. wasnt there. this attribute is a read only attribute and cannot be added to the cart.

Anyway I think I have convinced myself of the solution so I will post it here once I have it working.

15 Aug 2014, 1:00 PM
#12
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Option Value URL parameter?

philip937:

I confused matters by including "Please Select.." my appologies, this also irrelevant. imagine the Please Select.. wasnt there. this attribute is a read only attribute and cannot be added to the cart.

Anyway I think I have convinced myself of the solution so I will post it here once I have it working.

And that's what I was pointing out, that Please select in managing the attributes should be identified as the default also. As a result, the suggested code would have no effect. Ie, under proper management of the store, the addition of a selected option in a bullet style list will have no effect and not work. It works in the current setup, because it is not setup properly and the code suggested thus far will not account for that difference. See the FAQ: http://www.zen-cart.com/content.php?99-how-do-i-add-attributes-to-my-products about midway down.

15 Aug 2014, 1:19 PM
#13
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Option Value URL parameter?

Curious, since in my last review of our discussion, I didn't see an example page provided, under your current setup, what happens when the item is added to the cart without selecting one of the options, this is assuming that I understood the setup the way I have in my mind and that I incorrectly stated "read only" instead of the attribute option of display only.

Does a warning message appear that an attribute must be selected or does the product get added to the cart without any attribute selected?

14 Dec 2014, 4:58 AM
#14
clange avatar

clange

New Zenner

Join Date:
Dec 2014
Location:
Denver, CO
Posts:
13
Plugin Contributions:
0

Re: Option Value URL parameter?

Any luck implementing this?