8 replies [Last post]
dolittle's picture
Offline
Joined: 01/30/2008
Juice: 111
Was this information Helpful?

Hi,

Is there a support for ajax in ubercart?

Can I add products to the cart without page reload?

If there isn't it could be very usefull and shouldn't be too hard to implement Smiling

Thanks

dolittle's picture
Offline
Joined: 01/30/2008
Juice: 111
Re: Add to cart using ajax

bump

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15459
Go for it. We're on a

Go for it. Eye-wink We're on a feature freeze in core, but this can be done with a custom module and hook_form_alter().

dolittle's picture
Offline
Joined: 01/30/2008
Juice: 111
Re: Go for it. We're on a

How can I do it with hook_form_alter()?

What I had in mind is two pages left and right using the panel module.

The right page will be the cart and the left the catalog.

User will be able to browse the catalog with ajax and to add to cart using ajax.

The cart in the right page will be updated when the user add products.

I'll appericiate if you cuold give me a direction.

Thanks

philk@drupal.org's picture
Offline
Joined: 05/10/2008
Juice: 8
ajax add to cart would be great

hi doolittle...any progress with this? I too would like to have an ajax add to cart, but, I don't know how to create the form alter module Ryan mentioned.

philk@drupal.org's picture
Offline
Joined: 05/10/2008
Juice: 8
bumpity bump

bump

waldo's picture
Offline
Joined: 07/09/2008
Juice: 2
Re: Add to cart using ajax

Doesn't the uc_aac module do this or get you close? http://www.ubercart.org/contrib/2010

Well I guess technically, there is still a full page reload from what I can see. I've got my test site setup with uc_aac and no redirect set for the Cart settings and that automatically updates the cart when the add to cart button is pressed. This still does a page reload, but stays on the same page.

From what I can see without really digging into the code (I'm still familiarizing myself with Drupal and Ubercart) is that either Ubercart's behavior needs to be changed so it doesn't do a page reload when adding to the cart and just let AJAX and JavaScript do the work or, and this is where I don't know what can and can't be done, a module needs to be created that overrides the core default behavior so that the redirect doesn't reload the page, or it just doesn't redirect at all.

Personally, from looking at the Cart settings, the way I read it is if you set the redirect to <none> it should do just that, no redirect, thus no page reload. Maybe add another option of <same> where it does do a redirect and reload to the same page so that the <none> setting does what it insinuates "Enter <none> for no redirect."

Ryan, if you're still following this thread, could you post an example or a link to an example of using hook_form_alter()? I've only just started digging into the Developer's guide, but I'm no programmer, just a hack, tech geek.

I guess I just don't understand. If I or someone creates a new module, would they be overwriting some of the behavior of the uc_cart module? Would they need to specify new functions for uc_cart_form_alter and uc_cart_add_item? Well after doing a bit more digging, I think I'm understanding a little more.

A new module that would actually be pretty small and just and pretty much have a "uc_cart_ajax_form_alter" function that would be watching for "add to cart" and not do a redirect?

I think I've got more reading of the code, documentation and some testing to do....

torgosPizza's picture
Offline
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.
Joined: 08/14/2007
Juice: 4111
Re: Re: Add to cart using ajax

Here's how I imagine it'd get done.

- Create a custom module, call it UC_ajax_cart or something.
- In uc_ajax_cart.module you'll need a menu path that could be /cart/ajax/add - to which the vars get posted. (Using a GET would probably result in a redirect, especially if you are using a link.)
- Basically AJAX is just posting to another location. So your add to cart button would need to be a form (something that can POST vars) and your callback for /cart/ajax/add/.. will be a function that goes something like..

<?php
function uc_ajax_cart_add() {
 
$nid = $_POST['nid']; $qty = $_POST['qty']; // Make sure these vars are posting from the form.
 
uc_cart_add_item($nid, $qty); // Not sure if $data is required?
  /** Turns out there is also a $check_redirect var..
  uc_cart_add_item($nid, $qty = 1, $data = NULL, $cid = NULL, $msg = TRUE, $check_redirect = TRUE)
   - So you could potentially override the redirect that way. **/ 
}
?>

I think this would work, but it's untested. You would also need to add a drupal_add_js() bit of code that would basically be your AJAX callback - which would utilize jQuery to update the cart block. (The cart block might also need to be ajaxified for this reason - another part of the custom module.)

That's just my scattershot thoughts on the thing, anyway.

--
Help directly fund development: Donate via PayPal!

netslave's picture
Offline
Joined: 05/22/2008
Juice: 187
Ajax Cart

I create a module for an ajax cart. Please try and give feedback.

http://www.ubercart.org/contrib/6148