Forums / Setting Up Categories, Products, Attributes / Getting the default attribute radio button checked

Getting the default attribute radio button checked

Locked

Started by john_luciani on 27 Nov 2007, 6:50 PM · Views: 12,954

Results 1 to 14 of 14
This thread is locked. New replies are disabled.
27 Nov 2007, 6:50 PM
#1
john_luciani avatar

john_luciani

New Zenner

Join Date:
Nov 2007
Location:
Massachusetts
Posts:
2
Plugin Contributions:
0

Getting the default attribute radio button checked

I set up attributes for a product and wanted to have the radio button associated with
the default choice be checked when the page opens.

I set the "Default Attribute to be Marked Selected:" as yes for my default attribute
but none of the buttons were checked when the page opened. If I press the "preview attributes" button the proper button is checked.

Chaning the call of zen_draw_radio_field in the file html_output.php seems to have corrected
this problem (and hopefully not created new problems ;) Changing the $checked parameter
from false to true gives me a checked="checked" parameter in the <input> field for the
radiobutton associated with the default choice.

Is this a known bug? Since I am not familiar with the code I am wondering if I broke
something else.

(* jcl *)


https://www.luciani.org

27 Nov 2007, 7:14 PM
#2
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,874
Plugin Contributions:
8

Re: Getting the default attribute radio button checked

It's certainly not a known bug. I've never heard of the default selector not functioning before.

I would be wary of changing a parameter in a common function unless you know exactly what all the repercussions will be.

29 Feb 2008, 11:23 PM
#3
blackbrick avatar

blackbrick

New Zenner

Join Date:
Feb 2008
Posts:
1
Plugin Contributions:
0

Re: Getting the default attribute radio button checked

Hey John,

It's been a little while since you posted this problem so you may have already found another solution to your problem. Here goes my stab at it anyways...

I also ran into the same problem that you reported and have hunted down what I think is the fix :smile:

In <your zen cart path>/includes/modules/attributes.php, you will find the following block of code (in around line 200):

                  if ($_SESSION['cart']->contents[$prod_id]['attributes'][$products_options_names->fields['products_options_id']] == $products_options->fields['products_options_values_id']) {
                    $selected_attribute = $_SESSION['cart']->contents[$prod_id]['attributes'][$products_options_names->fields['products_options_id']];
                  } else {
                    //$selected_attribute = false;
                    // JEFF - fix for default attribute:
                    $selected_attribute = ($products_options->fields['attributes_default']=='1' ? true : ($products_options->RecordCount() == 1 ? true : false));
                  }

It looks like it has something to do with choosing the same attribute as previously selected if something is in the cart already. When I was playing, I added an item without any attributes to the cart. With attributes tied to price and with no attributes chosen, it added an item with 0 cost. Anyhoo, since my item wasn't found with an attribute in the cart, it did not allow a default radio button to be selected.

To fix this, I added the line that will set the default correctly if there is one, or if there is only one radio button option, it will make that the default. (the line following my comment: // JEFF - fix for default attribute:

In your fix when you set the default to be 'true', it changed all items to be selected or to be 'default'. This appears to work by selecting the first item in your list to be the default, however, if you have an alternate default item, it won't ever get set when viewing the page.

I've only started with ZenCart so if anyone else has more insight than me, feel free to add it here!

Regards,
Jeff

29 Feb 2008, 11:36 PM
#4
john_luciani avatar

john_luciani

New Zenner

Join Date:
Nov 2007
Location:
Massachusetts
Posts:
2
Plugin Contributions:
0

Re: Getting the default attribute radio button checked

Thanks for the reply.

It has been a while since I've looked at the code. I will take a look at your fix when I get
back to playing with Zencart.

(* jcl *)

https://www.luciani.org

15 Nov 2008, 6:36 PM
#5
pclaser avatar

pclaser

New Zenner

Join Date:
Nov 2008
Posts:
2
Plugin Contributions:
0

Re: Getting the default attribute radio button checked

Great Job!! This seems to be a bug.. I used your code Jeff and it works like a charm!!

Thanks again!!
:clap:

19 Jan 2009, 7:25 PM
#6
justlost avatar

justlost

New Zenner

Join Date:
Dec 2008
Posts:
11
Plugin Contributions:
0

Re: Getting the default attribute radio button checked

I can attest to this issue. Here is how to reproduce it:

  1. Go to Admin, and add a new item, NO ATTRIBUTES
  2. Go to your front end and add this item to your shopping cart.
  3. Go back to Admin, and add a couple of radio button attributes, and set one to default.
  4. Go back to front end to the products info page, and you will see that there is no default radio button selected.

In <your zen cart path>/includes/modules/attributes.php, you will find the following block of code (in around line 200):

if ($_SESSION['cart']->contents[$prod_id]['attributes'][$products_options_names->fields['products_options_id']] == $products_options->fields['products_options_values_id']) {
$selected_attribute = $_SESSION['cart']->contents[$prod_id]['attributes'][$products_options_names->fields['products_options_id']];
} else {
//$selected_attribute = false;
// JEFF - fix for default attribute:
$selected_attribute = ($products_options->fields['attributes_default']=='1' ? true : ($products_options->RecordCount() == 1 ? true : false));
}

So Yes, I think the problem has something to do with that code, trying to reselect the last picked option and overriding the default radio button.

I have not checked if it does the something on other attribute types.

Not a big bug per say, as long as you don't change attributes in the middle of your customers placing orders.

I noticed it when working on an Ajax attribute gallery, and my script kept complaining there was no default radio button for an attribute gallery, when testing on some new items.

19 Jan 2009, 9:25 PM
#7
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: Getting the default attribute radio button checked

Rule of thumb: If you have not finished everything on a new Product ... set it to products_status = 0 or In Stock NO ... so that this does not happen ...

Thanks for the suggestions ... :smile:

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

21 May 2009, 8:53 PM
#8
susb8383 avatar

susb8383

Zen Follower

Join Date:
Feb 2008
Posts:
129
Plugin Contributions:
0

Re: Getting the default attribute radio button checked

Hi,

I'm having the same problem, but it has nothing to do with the cart.

I have a teststore that nobody is using but me. I've added 3 attributes--two dropdowns and 1 radio buttons.

The two dropdown defaults are working fine.

The radio buttons have no default.

I have nothing in the cart. I tried Jeff's fix from the previous post and it did not fix it.

I'm on 1.3.8a. I'm using the add on improved_attributes_controller_1-1b2
but that's not the problem because I just changed back to the original attributes controller and same problem.

When I change that attribute to dropdown or checkboxes, it works fine. But for some reason radio buttons does not show me a default.

Hmm....

21 May 2009, 10:45 PM
#9
susb8383 avatar

susb8383

Zen Follower

Join Date:
Feb 2008
Posts:
129
Plugin Contributions:
0

Re: Getting the default attribute radio button checked

Ha ha!

Well, my problem was unique to me. Total operator error.

I had edited my product display page so that I had the Add To Cart button at both the top and bottom, and hen I added the product attributes, I also added them to both the top and bottom.

So it was defaulting the radio button correctly--it was doing it in the second set of radio buttons because that's the last one it saw with that id.

I was looking at the top set of radio buttons and that one wasn't getting set.

I guess the moral is, it's ok to have an Add to Cart button in more than one place, but not attributes if you're going to have defaults.

26 Jun 2009, 4:44 AM
#10
ramchandev avatar

ramchandev

New Zenner

Join Date:
Jun 2009
Posts:
9
Plugin Contributions:
0

Re: Getting the default attribute radio button checked

Hey! Guys

I too ran across this problem and found a solution!:D with out making any changes in the code.
http://www.imakewebsites.ca/zen-cart-downloadable-products/

Check this out they have explained it very clear.

-Ram

3 Nov 2009, 3:55 PM
#11
debiwebi avatar

debiwebi

New Zenner

Join Date:
Aug 2009
Location:
Highlands Ranch, Colorado
Posts:
84
Plugin Contributions:
0

Re: Getting the default attribute radio button checked

I'm having this problem with a Zen Cart site I just made:
http://herdoos.com/shop/index.php?main_page=product_info&cPath=8&products_id=91

Even though I have the first radio button set as the default in the attributes controller, its not selected on the page. When someone adds the item to the cart without making a color selection its added to the cart with a price of zero.

How do I fix this? I have another site almost ready to go live, with the same issue.

3 Nov 2009, 4:33 PM
#12
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: Getting the default attribute radio button checked

First, you have set up the Option Name as a Checkbox vs a Radiobutton ...

From what I am looking at, however, you may want the checkbox as I believe you are trying to get the customer to pick 6 colors ...

If that is the case, then you should have checkboxes ...

Now, are each of the colors $9.95 or are the 6 colors $9.95?

If each color is $9.95, for $9.95 * 6 = $57.90, then you should keep the price on the Attributes ...

But if you are trying to do 6 colors for $9.95, then you should:

1 remove the prices from the Attributes

2 edit the Product and mark it as Priced by Attribute: NO

3 set the Product Price to 9.95

But, if you are trying to pick 1 color to make a 6 pack of 1 color for $9.95, then you would want to switch to Radiobutton on the Option Name ... and then do the 3 steps above ...

To force a Default select, then add another Option Value such as:
Which Color?
Default YES
Display Only YES

which forces the customer to make a real choice ...

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

3 Nov 2009, 5:58 PM
#13
debiwebi avatar

debiwebi

New Zenner

Join Date:
Aug 2009
Location:
Highlands Ranch, Colorado
Posts:
84
Plugin Contributions:
0

Re: Getting the default attribute radio button checked

Thanks for your detailed response!

The reason I used checkboxes was so a customer could choose a variety of packs (they come 6 same-color items per pack) rather than just adding one color to their cart via a dropdown or radiobutton. The packs are $9.95 each, so I have to price the attributes so if someone were to pick a red pack, white pack and blue pack they'd get charged for 3 packs.

I tried forcing a default select like you said below (sounds like a great trick!) but unless someone were to uncheck that box when they add their items to the cart it causes an error message in the cart saying an invalid selection was made.

It would be really nice if, when someone didn't choose a color and clicked add to cart, a popup would come up saying "you haven't chosen a color" or something like that.

3 Nov 2009, 6:19 PM
#14
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,874
Plugin Contributions:
8

Re: Getting the default attribute radio button checked

Note that if you don't set the product to maximum 1, a customer might want to order 3 different packs, select 3 colors and enter qty: 3, thus ordering 9 packs. If you do set max 1, a customer who wants 2 red packs and 1 blue pack will not be able to order that combination.

You may be best off with radiobuttons so a customer only adds one color (in whatever quantity they want) at a time. They can then go back and order some of another color. A bit longer, but allows clarity and flexibility in ordering. You don't want to confuse the customer! :)