Ajax Attribute Calculations

Contrib type: 
Module
Status: 
Complete and working
Moderation: 
Limited approval
Latest version: 
2.0
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


Joined: 09/20/2007
Juice: 156

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.

Joined: 08/07/2007
Juice: 6673

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
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 756

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
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 756

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?

Joined: 08/07/2007
Juice: 6673

If it works, it sounds good to me.

cYu
cYu's picture
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 756

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

Joined: 09/20/2007
Juice: 156

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
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 756

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
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 756

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

Joined: 08/18/2007
Juice: 132

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
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 756

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?

Joined: 08/18/2007
Juice: 132

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
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 756

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

Joined: 08/18/2007
Juice: 132

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.

Joined: 10/02/2007
Juice: 126

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
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 756

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.

Joined: 12/19/2007
Juice: 110

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
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 756

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

Mog
Mog's picture
Internationalizationizer
Joined: 08/16/2007
Juice: 94

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
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 756

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

Joined: 10/02/2007
Juice: 126

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
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 756

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
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 756

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.

Joined: 08/14/2007
Juice: 3867

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!

Joined: 10/02/2007
Juice: 126

cYu,

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

cYu
cYu's picture
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 756

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
Joined: 01/19/2008
Juice: 4

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
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 756

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
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 756

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
Joined: 01/19/2008
Juice: 4

Thanks! Smiling

Joined: 08/07/2007
Juice: 87

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
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 756

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?

Joined: 08/07/2007
Juice: 87

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

Joined: 01/09/2008
Juice: 152

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
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 756

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; ?>"
Joined: 01/09/2008
Juice: 152

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
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 756

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.

Joined: 09/18/2007
Juice: 17

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
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 756

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.

Joined: 09/18/2007
Juice: 17

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
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 756

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?

Joined: 03/07/2008
Juice: 4

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

Joined: 03/17/2008
Juice: 6

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
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 756

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.

Joined: 03/17/2008
Juice: 6

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.

Joined: 03/17/2008
Juice: 6

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
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 756

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.

Joined: 03/13/2008
Juice: 50

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
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 756

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.

Joined: 03/13/2008
Juice: 50

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.