JQuery HELP!

Posts: 59
Joined: 08/08/2007
Early adopter... addicted to alphas.Getting busy with the Ubercode.

Ok, you guys are definitely back up just in time. I've worked all night on this problem and it is killing me. I'm sure someone here will be able to help.

I'm trying to add an onchange event to a product attribute. Then on that onchange event, I want to get the selected value and display an image based on that. Basically one of my attributes is a list of fonts. I have a bunch of images of those fonts and when they select a font, I want it to show up.

There has to be something outside of my control, because I can't even get the value. The id of the select is #edit-attributes-3, so just to test, I'm just trying to get the value and do an alert. All I get is null. I've even tried it with the quantity, and I get null. I'm calling the function when the document is ready, so the values should be available. Am I missing something? I'm pulling my hair out here...

Cheers,
Steph

Posts: 92
Joined: 08/07/2007
Bug FinderGetting busy with the Ubercode.

Is your development environment live, or are you running a local copy. I'd be happy to look into this some because I have an interest in doing the same thing.

Posts: 59
Joined: 08/08/2007
Early adopter... addicted to alphas.Getting busy with the Ubercode.

I actually figured it out, and I feel stupid.

With the calls you are supposed to put a function:

$(document).ready(function(){font_set_onchange('/cards4kids/','themes/cards4kids')});

I was doing this:

$(document).ready(font_set_onchange('/cards4kids/','themes/cards4kids'));

For some reason it was sort of working, so I didn't try to fix it, but after a day or so away from it, I went back, and noticed that maybe that was wrong.

I can post how I ended up getting it to work if you like. Basically I just created a new template for the type of product I wanted this to work for, and added some drupal_add_js lines to it to include my javascript file.

I add an change event to the attributes dropdown, and in that function I get the value of the dropdown, and I've created a folder of gif images with the different fonts and named them the same as their id. So if the value I get back is 4, I've named the image for that font 4.gif. Then I just include an image after the select box that is $(dropdownvalue).gif.

I don't know if that made any sense, but I will have the website up soon and can point you to that, or I can post the actual code here if you like.

Cheers,
Steph

Posts: 34
Joined: 08/07/2007

i think this could be used to update the price instead of changing a picture. please post your solution. thanks!

Posts: 59
Joined: 08/08/2007
Early adopter... addicted to alphas.Getting busy with the Ubercode.

Actually, I do that too. I change the price based on the quantity entered. Somehow I just have to get that to work on checkout as well. I will post both updating the font and updating the price based on quantity tonight. Though the solution isn't a general solution. Eventually I can maybe start adding these ideas into a module or something but for this website I've just added in these hacks to make it work for me. I'm happy to share them so other people can add them in, but you probably have to do a bit of hacking to get it to work for you.

Anyway, I'll post them tonight when I get home.

Posts: 4686
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

That's great news! I look forward to seeing it in action... and maybe using it for our sites in the future, too. Sticking out tongue

Thanks for taking this on.

Posts: 59
Joined: 08/08/2007
Early adopter... addicted to alphas.Getting busy with the Ubercode.

Ok, so I'll go through my steps, at least so far. I copied the node.tpl file into one called node-personalised_cards.tpl.php because my class for my product was personalised_cards. Then at the top of that I've added these lines:

<?php drupal_add_js(path_to_theme()."/personalised.js"); ?>
<?php
drupal_add_js
("if (Drupal.jsEnabled) {  $(document).ready(function(){font_set_onchange('".base_path()."','".path_to_theme()."')}); }",'inline');
?>

Basically that adds my javascript file and then adds my function call to when the page loads. Then I have the attached javascript file. I'm not pasting it in because part of it is long. In that I have both the image change, and the price change when the quantity changes. The quantity one is the longest, so I'll just copy and paste there the parts that is just for the image change for the font:

function font_set_onchange(base_path, path_to_theme){
$("#edit-attributes-3").change(function(){font_image(base_path, path_to_theme)});
font_image(base_path, path_to_theme);
quantity_set_onkeyup();  // not needed for just the font change
colour_set_onchange();   // not needed for just the font change
}

function font_image(base_path, path_to_theme){
var font_value = $("#edit-attributes-3 option:selected").val();
$('#fontimage').remove();
var path = base_path+path_to_theme+'/images/font_images/'+font_value+'.gif'
$("#edit-attributes-3").after("&nbsp;&nbsp;&nbsp;<img src=\"" + path + "\" id=\"fontimage\"/>");
$("#edit-attributes-3").parent().attr("width","250px");  // this is because of a strange behavior where the div doesn't seem to adjust to the size with the image and moves the next line down a little way for a long image
}

Anyway, have a look through that, and if there are any questions, let me know. The quantity stuff is pretty neat, because I had to put it on a timeout so that if someone is entering in several keystrokes, it only picks up once they pause. I also plan to put in that once it gets above a certain amount, they can't add it to the cart. For this site, once they want above 5000 cards, it is a special order, so they'll have to call up. We don't expect it to happen that often...haha.

Cheers,
Steph

AttachmentSize
personalised.js_.txt2.74 KB
Posts: 116
Joined: 08/08/2007
Brain Stormer

looks great (i haven't tried it yet though).

pointer, i'd make sure that if you have a check on 5000+ cards that you do it in backend code though versus js because anyone can disable js to get around it.

Posts: 59
Joined: 08/08/2007
Early adopter... addicted to alphas.Getting busy with the Ubercode.

Yes, most definitely. At the point where I posted that stuff, it actually only worked before adding to the cart. Now I want to make it actually work instead of just the javascript showy part.

With the crash, I'm having a hard time trying to find out exactly what hook, etc I would have to add into a temporary module to make this work.

I've figured out the hook_cart_item, so I change the price of the item based on the quantity so now it works all throughout the checkout process. Now I just have to use the hook_add_to_cart to make sure things don't get added when there are over 5000. I can't quite find documentation on that hook, but hopefully I'll find enough to be able to get it to work.

Oh, and of course making the whole thing generic is actually the difficult part. I am definitely cheating big time.

Posts: 17
Joined: 08/07/2007

Could you please post the code?

I was trying to achieve update price based on attributes, but can't figure out the hook-stuff etc.

Thanks for your help.

Posts: 17
Joined: 08/07/2007

I tried to make my own "personalised.js" and a function price_set_onchange - but unfortunaly it doesn't work.

I'm having some troubles:

I show all products in a /taxonimy/term list but the scipt above (as I see it) is only handling one node/product - since the change(function() will work on all $("#edit-attributes-1") on the list/page.

And using $('div.display_price').empty(); $('div.display_price').append(price); gives me the same problem.

I guess somehow we'll have to make them hold the node-id as well? (how?)

In my case - even using the above on a single node doesn't work! It updates the price when the document has loaded but not when changing the attribute.
I'm sure I must have left something important out??

Any help is very helpfull and will be gladly received. Thanks. Smiling

Posts: 17
Joined: 08/07/2007

Has anyone done progress on this?

You might want to see this tread as well... http://www.ubercart.org/forum/support/45/update_price_when_attributes_ar...

aspx-shop-owners are making fun on this, please help... we need this behaviour. Smiling

O.K. can anyone tell me what hooks and divs to use?

Any help... I'm trying - but as you can see in my comment above this, I'm having problems.