Ajax Attribute Calculations

Issue tracker: view

Contrib type: 
Module
Status: 
Uploaded for testing
Compatibility: 
Ubercart Beta 1
Latest version: 
2.0

Module has been moved to drupal.org at http://drupal.org/project/uc_aac
I'd prefer to maintain the module from there but will also see any comments left here.

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.

*This module requires product node divs have an identifier in the same form as the garland theme. Trying to use bluemarine theme without adding this to node.tpl.php will cause the module to not work.

id="node-<?php print $node->nid; ?>"

v1.1 - 12/7/2007 9:55am EST
-instead of replacing entire content of sell price and display price divs with the newly calculated price this code will now replace only the old price within those divs with the new price
-commented out the "failed to update this result" alert

v1.2 - 12/11/2007 4:20pm EST
-fixed up rogue jquery code that was in place for a side project that i was working on.

v1.3 - 12/17/2007 10:30am EST
-added check to only try to update display or sell price if those fields exist on the product form

v1.31 - 12/26/2007 3:05pm EST
-removed trailing comma that jrust found was causing IE errors

v1.32 - 1/7/2008 9:45am EST
-added type="text/javascript" to script tag

v1.4 - 1/16/2008 1:00pm EST
-added detour's patch for using correct product revision and re-calculating attribute price adjustments on the fly
-adjusted query for getting the correct node revision
-added in code to preserve config setting for attribute adjustment display (none, total, or adjustment)
-replaced getelementbyid in patch with code which allows multiple products with attributes to display on one page by doing a jquery selector based on node id (*dependent on template as noted above)

v1.41 - 1/21/2008 10:20am EST
-Fixed problem with attribute options not using the ordering field to determine order correctly.

v2.0 - 3/17/2008 11:15am EST
-reworked module to accommodate radio button attributes
-added config option to admin/store/settings/attributes to turn off attribute price re-adjustments
-ran through coder module and updated to proper drupal syntax

Module has been moved to drupal.org at http://drupal.org/project/uc_aac

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.

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.

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?

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?

Re: Re: Re: works fine

If it works, it sounds good to me.

Re: Re: Re: Re: works fine

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

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.

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.

Re: 1.1 not working

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

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.

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?

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.

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.

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.

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.diff3.69 KB

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.

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!

Re: Fixing it for IE 6/7

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

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

Re: thx for this module.

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

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-packa...

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.diff4.44 KB

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?

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.

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.

cYu,

cYu,

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

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.

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.

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.

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

Re: Re: Re: Sort order of attribute options used in drop down fi

Thanks! Smiling

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

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?

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

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.

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; ?>"

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...)

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.

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

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.

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.

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?

input type "radio" not supported

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

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!

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.

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.

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.

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.

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)

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.

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.

Re: Re: Re: Re: Ajax Attribute Calculations

Uberuser, there is some discussion here...
http://www.ubercart.org/forum/support/3057/one_product_multiple_options
and someone taking up a donation pool for these enhancements here...
http://www.ubercart.org/forum/ideas_and_suggestions/3596/donation_pool_a...
and maybe some people who have done it here...
http://www.ubercart.org/forum/development/78/images_attached_product_atr...

Re: Ajax Attribute Calculations

Thanks a bunch!! CYu

Re: Re: Re: AAC & i18n

I think the 2.0 version of this module should address some of the problems people were having.

@mackay - I updated how the url was generated and it might now work for your i18n example...let me know.

@Otto,brheault - radio button support has been added.

Re: Re: Re: Re: AAC & i18n

I am still using 1.41, have not yet updated to 2.0, but it looks like 2.0 has this same issue:

In uc_aac_menu, you have if ($may_cache). If page caching is turned on, price calculations don't seem to work. I changed this line to if (!$may_cache), and all seems to be well with page caching enabled.

Re: Re: Re: Re: Re: AAC & i18n

beejaysea, my understanding of $may_cache is that it caches all of that information in the items array and not the actual return page. Caching what is returned from the callback would cause problems, but in this case you can see that the static items array should work fine if it is cached.

Is your understanding of this different? I'm not too sure what is causing the problem you described, but I was unable to reproduce it by turning on caching and then viewing products as an anon user.

Re: Re: Re: Re: Re: Re: AAC & i18n

Well, yes, that's my understanding of it as well. Strangely enough, it broke again. So, I'm thinking that there's something else going on. I have turned off caching for the time-being, and put the value for the menu item back to if ($may_cache). Ah, off to debug...

Undefined

I get the message "undefined" on all items that don't have any options. Any idea how to make it so that if there are no options for the items it leaves the default value?

Re: Undefined

Heaiser, I'm unable to replicate this problem.

What version of Ubercart/uc_aac are you using?

When you say, 'all items that don't have any options' do you mean Text field attributes or Select/Radio attributes which have no options? If you mean the latter, then they should probably be Text field attributes.

I also don't know what you mean by default value when there are no options. Can you please elaborate?

Unknown column 'display' error

This is the best module and will be part of all my Ubercart installations. However I'm getting an error, and need some help.

user warning: Unknown column 'display' in 'field list' query: SELECT display FROM uc_attributes WHERE aid = 1 in /var/www/vhosts/bestweightlossrecipes.com/httpdocs/includes/database.mysql.inc on line 172.

you can see this by going to www.bestweightlossrecipes.com, click > Catalog > Bread > Bread Pan

Thanks in advance for your help!

Re: Unknown column 'display' error

Run update.php on your site. A display column was added to the attribute tables.

update.php didn't work

Sorry to be a pain. but update.php didn't work. I went through Administration > Modules, and clicked through to update.php in there but I still have my error. any other ideas? something I should do to the database uc_attributes. I don't have a column 'display' in uc_attributes. Just aid, name and ordering.

I really appreciate all your help in getting this all set up. when I get good at this I am going to help write up some how-to's for other people like me who aren't great programmers.

Connie

Had to manually add column

Figured it out myself. I have another site, with a different theme, and when I added the Ajax module everything worked okay. So I compared the databases and saw that uc_attributes, indeed, did not get it's column added when installing the module. So I added it manually in phpmyadmin, and everything seems to be working okay now.

Here's what I added, in case anyone else has this problem.
Field Type Null Default
display tinyint(2) No 1

Thanks

Re: Had to manually add column

If your site was using an older version of Ubercart, that table column would not exist yet. I'm not sure when it was introduced, but I know it should be there if you download and update to Release Candidate 2.