Ajax Attribute Calculations

Contrib type: 
Module
Status: 
Complete and working
Moderation: 
Limited approval
Latest version: 
2.0

Downloads


Compatibility: 
Ubercart 1.x

This module will automatically update the sell_price and display_price fields contained in the add_to_cart_form when attributes are adjusted. By default it also updates individual attribute price adjustments so that they remain relative to the updated price, but this can be turned off in the attribute admin settings.

Available also for d6 Ubercart, at http://drupal.org/node/296878


snicers's picture
Offline
Uber DonorInternationalizationizer
Joined: 09/20/2007
Juice: 192
works fine

Hi, thank you for that module. It is working fine. No problems yet. oh.... one small thing: The module deletes the "price:" lable and shows only the value.

Lyle's picture
Offline
AdministratoreLiTe!
Joined: 08/07/2007
Juice: 6855
Re: works fine

I noticed that too, and I thought you might find a way to pass the returned HTML from the right theme functions back to the AJAX call. The problem is that there would be two different values to deal with instead of one.

cYu
cYu's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 852
Re: Re: works fine

Yeah, I see what you mean. I can append Price: easily enough, but maybe I can address what Lyle is talking about as well. Lyle, I'm not sure I follow exactly what you mean. Can you give me some examples so that I can play around with it?

cYu
cYu's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 852
Re: Re: works fine

Lyle, I think I can extract the original price from the current innerHTML and then plug in the new price that is returned, surrounded by whatever other HTML was there. Would that address the concern you had?

Lyle's picture
Offline
AdministratoreLiTe!
Joined: 08/07/2007
Juice: 6855
Re: Re: Re: works fine

If it works, it sounds good to me.

cYu
cYu's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 852
Re: Re: Re: Re: works fine

Latest version should adjust the sell_price in a way which preserves any other text around it.

snicers's picture
Offline
Uber DonorInternationalizationizer
Joined: 09/20/2007
Juice: 192
1.1 not working

Hi, sorry the 1.1 version is not working for me on alpha8, the price will not adjust.. module is active.

cYu
cYu's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 852
Re: 1.1 not working

I found some rogue code in there that was linked to something else I was testing. I'll get that fixed up and upload a new version. Thanks for the feedback.

cYu
cYu's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 852
Re: 1.1 not working

1.2 is up there now and should work. Let me know if you have any more problems.

TimK's picture
Offline
Joined: 08/18/2007
Juice: 147
V 1.2

Thanks for your work on this, cYu.

For some reason version 1.2 is not updating the price fields for me when an attribute with a price adjustment is selected. I am using UC alpha 8. The AAC module is enabled, but there is no change in the store behavior when it is.

Any ideas?

Thanks.

cYu
cYu's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 852
Re: V 1.2

TimK, are you getting any Javascript errors? If you use Firebug with Firefox, can you look to see if there are Ajax requests being made, and what they are returning?

TimK's picture
Offline
Joined: 08/18/2007
Juice: 147
Thanks for the reply, cYu. I

Thanks for the reply, cYu.

I am getting the error: "currentdisplayHTML has no properties".

Here is the bit of code that gets highlighted in Firebug: $("#node-55 .display_price").html(currentdisplayHTML.replace(replacePrice_uc_product_add_to_cart_form_55, html_result));

I'm not a coder, so I hope this helps.

Thanks.

cYu
cYu's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 852
TimK, Try v1.3 when you get

TimK,
Try v1.3 when you get a chance and let me know if it is working for you now. Thanks.

TimK's picture
Offline
Joined: 08/18/2007
Juice: 147
Re: TimK, Try v1.3 when you get

The module is working fine now. I updated to the latest version, and since I am using the Blue Marine theme, I change the first line of node.tpl.php to:

<div id="node-<?php print $node->nid; ?>" class="node<?php if ($sticky) { print " sticky"; } ?><?php if (!$status) { print " node-unpublished"; } ?>">

Thanks cYu.

detour's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 10/02/2007
Juice: 150
Updating cost field

cYu, Thanks for the contribution.

I'm modified uc_aac to update the cost as well as the sell price and show the sum in the display price. I'm using this on a site where the cost field on products and attributes is used as a conditional fee.

In case it would help anyone, I've attached the patch for this.

AttachmentSize
uc_aac-cost.diff 3.69 KB
cYu
cYu's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 852
Re: Updating cost field

Thanks for posting that detour. Took a quick look and it looks good for what you described. Glad you were able to work with the existing code instead of having to write from scratch.

jrust's picture
Offline
Joined: 12/19/2007
Juice: 122
Fixing it for IE 6/7

It wasn't working in Internet Explorer because IE is more finnicky than FF about putting commas between methods in an object. IE insists that the last method of an object not have a comma while FF allows it either way. All that to say the javascript can be fixed for IE by removing the comma after the error method. The error method should look like:

      error: function() {
        //alert("Failed to update this result.");
      }

Thanks for the great module!

cYu
cYu's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 852
Re: Fixing it for IE 6/7

Thank you jrust. I uploaded your suggested fix in v1.31.

Mog
Mog's picture
Offline
Internationalizationizer
Joined: 08/16/2007
Juice: 160
thx for this module.

thx for this module.

just a sugestion :
It would be better to use <script type="text/javascript"> than <script> on line 67 of .module to get a W3C valid code Eye-wink

cYu
cYu's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 852
Re: thx for this module.

Ah....yes indeed Mog, thanks. I'll change that and upload it as v1.32.

detour's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 10/02/2007
Juice: 150
Patch for new feature and fix

I'm attaching a patch to implement a new feature as well as fix a critical issue.

First, this adds dynamic updating of the price differentials shown for each attribute option. Currently, these remain relative to the product's original sell price. With this patch, the price differentials are rewritten to be consistent with the updated sell price. You can see a demonstration of this at:
http://sundaysenergy.org/catalog/hosting/0.1.1-hosting-basics-base-package

Second, I've fixed the method used for looking up the sell price of a product. Currently, it does not fetch the price from the product node's latest revision. I have changed the query statement to obtain this specifically.

AttachmentSize
uc_aac-attrtext.diff 4.44 KB
cYu
cYu's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 852
Re: Patch for new feature and fix

Thanks Detour. I'll get that fix in for the revisions. I'm not sure I would always want the functionality that your new feature adds, but maybe I could add that as a configuration option. What sort of verbiage do you think best describes that action?

cYu
cYu's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 852
detour, Was there a reason

detour,
Was there a reason to use

AND vid=(SELECT vid FROM {node_revisions} WHERE nid=%d ORDER BY timestamp DESC LIMIT 1)

instead of

AND vid=(SELECT vid FROM {node} WHERE nid=%d)

I'm a little iffy about how revisions work in general, but if the latter statement works fine I would use that instead.

torgosPizza's picture
Offline
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.
Joined: 08/14/2007
Juice: 4111
Re: detour, Was there a reason

FWIW, I almost never use revisions on my nodes, granted it gives you an "updated" status but I have never explicitly checked the "create new revision" checkbox.

--
Help directly fund development: Donate via PayPal!

detour's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 10/02/2007
Juice: 150
cYu,

cYu,

No good reason. Eye-wink Your revision to the code looks good.

cYu
cYu's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 852
Re: Patch for new feature and fix

detour, I played around with your patch a little more and I really like the automatic attribute price differential adjustments. I think it makes more sense than what was there before and should be the default behavior. One thing I noticed though is that it ignores the setting in /admin/store/settings/attributes to display only the total price or no adjustment at all, so I'll put in a check for that and then apply your changes. Thanks for the patch.

rew
rew's picture
Offline
Joined: 01/19/2008
Juice: 4
Sort order of attribute options used in drop down field

Hi there. Attributes shown in the drop down field on the product page are sorted by name instead of their ordering field specified in Home › Administer › Store administration › Products › Manage attributes › Attributes. I did not found the line of code where I could change this, my only guess is here: (uc_aac.module, line 50)

function _uc_aac_alter_form($field_id, $nid) {
# ...
$total_attributes = db_num_rows(db_query("SELECT * FROM {uc_product_attributes} WHERE nid = %d ORDER BY ordering", $nid));
# ...
}

Tough this seems right, as the SQL statement reads "ORDER BY ordering".

Thanks for any help in sorting this right, and thanks for the module.

cYu
cYu's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 852
Re: Sort order of attribute options used in drop down field

Good catch rew, I'll take a look at that today and see if I can figure out what is going on.

cYu
cYu's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 852
Re: Re: Sort order of attribute options used in drop down field

uc_aac.module line 200 should be...

$res = db_query('SELECT DISTINCT upo.oid, uao.name, upo.cost, upo.price FROM {uc_attribute_options} AS uao JOIN {uc_product_options} AS upo ON uao.oid = upo.oid WHERE uao.aid = %d AND upo.nid = %d ORDER BY upo.ordering', $aid, $nid);

I'll fix that up and upload it as version 1.41

rew
rew's picture
Offline
Joined: 01/19/2008
Juice: 4
Re: Re: Re: Sort order of attribute options used in drop down fi

Thanks! Smiling

sc0tt's picture
Offline
Uber Donor
Joined: 08/07/2007
Juice: 108
Re: Re: Sort order of attribute options used in drop down

I'm using this module with beta2 and it seems to work OK with the one exception. I did get v1.41 and my attributes drop down seems to be ordered incorrectly. My default option appears in the middle of the drop down list of attributes.

Thanks for this great add-on module!
Sc0tt

cYu
cYu's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 852
Re: Re: Re: Sort order of attribute options used in drop down

sc0tt, default option is different than ordering. On the screen where you set the default option you need to use the column on the right, ordering, for setting the order of the options in your attribute drop down. Is this what was confusing you, or does the module appear to be ignoring the ordering field?

sc0tt's picture
Offline
Uber Donor
Joined: 08/07/2007
Juice: 108
Re: Re: Re: Re: Sort order of attribute options used in drop dow

It's working just fine now. Sorry...I don't know what was going on the other night when I saw this problem. I was having several random problems that just didn't make any sense. I spent 4 hours deep in the twilight zone.

All is working as expected. Thanks very much for your help!
Sc0tt

oliver coleman's picture
Offline
Bug Finder
Joined: 01/09/2008
Juice: 212
Using in conjunction with Extra Columns module?

Hi, thanks for a great module Smiling

Just wondering how hard it would be to apply this to the uc_product_table as modified by the Extra Columns module (http://www.ubercart.org/contrib/763)? The EC module adds the attributes drop-downs into the product table for each product, you can see an example at http://e-geek.com.au/computer-systems

I had a quick look and fiddle but couldn't see any obvious way to make it happen.

cYu
cYu's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 852
Re: Using in conjunction with Extra Columns module?

Oliver, I'm not certain...but I get the feeling this might only take a minor change to make this work. The AAC module depends on a div surrounding the node having an ID of the format noted in the description of this module. That div exists right now on your page, but needs to have an ID like...

id="node-<?php print $node->nid; ?>"
oliver coleman's picture
Offline
Bug Finder
Joined: 01/09/2008
Juice: 212
Re: Using in conjunction with Extra Columns module?

Hmm, the page with the product table has this div for the page (not for each product in the table)
<div id="node-10" class="node ntype-page">

Would wrapping each row of the product table in a div with that products nid work?

If it would, then I'd have to reimplement the table rendering, it's using the tapir module at the moment, so would require modifying that to allow adding divs for each row (can't have <table><tr><div><td> .. </td></div></tr></table>). Not using tapir (perhaps a view instead) would mean reimplementing the Extra Column module (it would be nice to be able to add product attributes to a view...)

cYu
cYu's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 852
Re: Re: Using in conjunction with Extra Columns module?

Oliver, when you go to an individual product page you'll see something like...

<div class="node ntype-computer" id="node-82">

I don't know enough about that extra column module or how your tables are setup in tapir, but if your page which lists multiple products has a surrounding div with an identifier like that, ajax calculations should work.

mackay's picture
Offline
Internationalizationizer
Joined: 09/18/2007
Juice: 33
AAC & i18n

Hi,
first of all many thx for this module - it really works great!
On my site i use i18n for both Polish and English languages and before I got AAC working I had to change it little bit. Without it (node template of course "patched") I got none JS errors and price changes - like AAC wouldn't be enabled.
anyway I changed uc_aac.module:

[line 83] fakeForm.setAttribute( "action", "'.substr( url("/", null, null, TRUE), 0, -1).'aac" );

to

[line 83] fakeForm.setAttribute( "action", "'.substr( url("/", null, null, TRUE), 0, 0).'aac" );

Maybe someone will need this quick patch. I suppose to patch it right there should be an "if (i18n enabled) ..." function here?

Mackay

cYu
cYu's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 852
Re: AAC & i18n

mackay, that is interesting. Can you explain a little about why this works? I will take a closer look, but at first glance I don't really understand why this fixes the mod for an i18n site.

mackay's picture
Offline
Internationalizationizer
Joined: 09/18/2007
Juice: 33
Re: Re: AAC & i18n

cYu, my site with i18n has an /en and /pl after domain name and subdirectory (www.example.com/drupal_site/en). I have also pathauto which makes aliases for my nodes based on [sku]. So the link to example product is www.example.com/drupal_site/en/0018.
With such config and original AAC enabled I had no JS response after loading the page or changing attrbutes.
After changing parameter in substr from -1 to 0 it helped, but today I noticed that it only works on product pages. After browsing through taxonomy (catalog) where I get numerous products on single page (with option to pick attributes) any change to them won't make any price change. I guess it's because the url changes and it's then www.example.com/drupal_site/en/sub1/sub2/sub3/sub4.
I might be wrong assuming earlier that it could be a bug (sorry for that cYu).
Based on my little JS knowledge I can tell that from my point of view the changed priced is not targeting the right place (because of url aliases?)
I would be very grateful for any help. If I can help please write.

cYu
cYu's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 852
Re: Re: Re: AAC & i18n

mackay, it might be some time before I can get around to setting up this scenario to test out. If you already have somewhere that I can see this in action, can you send me a PM with the url?

Otto's picture
Offline
Joined: 03/07/2008
Juice: 4
input type "radio" not supported

This does not appear to work when instead of the select box I use radio buttons..

brheault's picture
Offline
Joined: 03/17/2008
Juice: 6
Re: Ajax Attribute Calculations

Hi, I'm new here but have been using Ubercart for a few months.

I've been craving the ability to recalculate price based on attributes selections for a long time and just noticed this module. Unfortunately, while I can get it to function, I cannot get it to recalculate accurately. It seems the first time an attribute is changed, the price changes radically, 10 times what it should. After that first one, other attributes change the display price correctly. It doesn't matter which attribute is changed first, it's always wrong, and then it works for subsequent attribute selections. Does any have an idea about what would cause this? I'm using the most recent version of Ubercart with Drupal 5.7. Any help would be greatly appreciated.
Thanks!

cYu
cYu's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 852
Re: Re: Ajax Attribute Calculations

Otto and brheault, I suspicion your problems are related. There have been changes to the attribute system recently (such as addition of radio button option) which were not accounted for when this module was written. I will try to update it this week, and will also take any patches if you guys happen to find the problem before I do. I've never used this module for anything more than testing....so I'm grateful for any feedback I can get from people using it on their sites.

brheault's picture
Offline
Joined: 03/17/2008
Juice: 6
Re: Re: Re: Ajax Attribute Calculations

I noticed that it doesn't work with radio buttons, but it doesn't have any negative effect with them either... The add to cart button still calculates the correct price from the attributes selected. It's only with select boxes do things actually get mucked up.

I wish I could provide more valuable feedback, or patches, unfortunately it's a little beyond my level. I hope this ends up working, and thanks for everything you've done so far.

brheault's picture
Offline
Joined: 03/17/2008
Juice: 6
Only 1.41 causes problems

Hey cYu, I decided to try AAC 1.32 instead of 1.41 and it seems to work great. So if the only difference between the two is the functionality to recalculate attribute prices, then I guess that's the source of problem at least with Ubercart beta 7. Probably nothing you didn't know, but thought I'd mention it if it gives you any ideas.

cYu
cYu's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 852
Re: Only 1.41 causes problems

brheault, thanks for looking into that. I had left 1.32 up because of the functionality differences between the 2 versions, and since you've narrowed the problem to something amongst those differences I should be able to fix that up faster. I think I'll probably add a setting that allows you to pick which of those 2 ways you'd like the module to function while I'm in there digging around.

uberuser's picture
Offline
Joined: 03/13/2008
Juice: 50
Re: Ajax Attribute Calculations

cYu, This module looks very promising for what I am trying to achieve I have one more question. Would this module also update images. For example if I have attribute as color and I want to show my customers how a shirt would look in different colors would it update the images also (of course assuming I have the images uploaded to the files directory)

cYu
cYu's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 852
Re: Re: Ajax Attribute Calculations

uberuser, this has no tie in with images...but that is definitely a good idea. Perhaps not to be added to this, but as a separate contrib or even in core. I know the idea has been tossed around a few times, so somebody might have already done some work in that direction.

uberuser's picture
Offline
Joined: 03/13/2008
Juice: 50
Re: Re: Re: Ajax Attribute Calculations

Thank you cYu, I was searching around and did not find anyone talking about it if you find something please post. What you have is an awesome module I am using it without problems.