Ajax Add to Cart is a Drupal Commerce module.

About the module:
AJAX Add to Cart module ajaxifies the add to cart operation. The updated cart will be displayed without page refresh and a popup message will be shown after you add an item to cart. This module provides two blocks, first the shopping cart block that provides details of the product currently present in the cart, and the other is a shopping cart teaser that provides the total number of items
and the total amount including tax. The module also provides other customizations like showing the product image in shopping cart. You can further customize the look of the blocks using its template files.

The sandbox project of Ajax Add to Cart module can be found here:

Git repository link:
git clone --recursive --branch master subhojit777@git.drupal.org:sandbox/subhojit777/1844396.git ajax_add_to_cart

This a Drupal 7 module for Drupal Commerce.

I have ported Ajax Pic Preview module from Drupal 6 to Drupal 7
It is still in sandbox mode, soon it will be released as a full project.


Status:Needs review» Needs work


please get a review bonus first. Then try to fix issues raised by automated review tools: http://ventral.org/pareview/httpgitdrupalorgsandboxsubhojit7771844396git

Hi, great module! Here're some tips:

.info file
1. Maybe you need to add a dependencies[] of commerce module.

.module file
1. For hook_init(), I dont' think it's necessary to load system file. Drupal will load them for us. Also, js and css should not be added inside this hook, unless there're specifc conditions. Otherwise, they will be loaded for all pages(and they should be put in .info file). I think this is not what you want.

2.'page callback' => 'remove_commerce_line_item','delivery callback' => 'ajax_deliver', and there're many other functions, they should have a prefix of module name to avoid name collision.


['remove-product/ajax/%'] = array(
'delivery callback' => 'ajax_deliver',
  ) +

This part looks wierd, or not the conventional way of hook menu.

4. For hook_theme(), 'template' => 'templates/custom-shopping-cart', you should only put a file name, instead of including the path. You may want to use 'path' to override default path. But I think it's safe to use 'template' => custom-shopping-cart', since these templates are located in this module's directory.

.tpl file.
1. Function variable_get is used in tpl file. Maybe we could add these variables in prepcrocess function and make tpl more clean.

.install file
1. Is it really necessary to set so many variables? Like this variable, variable_set('remove_cart', 'link'); it's just a regular string 'link', and you set it as a variable in database. They're not changed anywhere, unless this module is uninstalled. I think it's safe to use these strings directly. Or, you could use php 'define' keyword in .module file to keep these configuration settings.
2.Also, these variables should have a module name prefix, otherwise it's quite easy to get name collision.

.admin.inc file
1. If a page callback only returns a drupal_get_form, I think we could use drupal_get_form as the page callback directly.

Anyway, a nice module!

Here is a little addition to the comment of @smiletrl in #2. In the .info you should probably add these dependencies:

dependencies[] = commerce
dependencies[] = commerce_cart

Also in the .info file you should remove the version, it's automatically added.
Here is an automated review with some more things: http://ventral.org/pareview/httpgitdrupalorgsandboxsubhojit7771844396git

By the way, nice project!

Issue tags:+PAReview: Commerce

Adding PAReview: Commerce tag

There seems to already be a commerce cart ajax module, though yours seems to add a little more functionality.

Thank you smiletrl for the code review and the suggestions. I will make sure that the module follows your suggestions. But there are something which cannot be followed, like the variable_set in .install file. This module has admin settings form and it requires some default configuration settings, due to which I have considered so many variables in .install file, although I will change the name of variable as you suggested. Thanks again :)

Thank you idflood, I will soon add those dependencies in .info file. Thanks :)

@2pha dc_cart_ajax module ajaxifies the removal of items from cart, but this module will ajaxify the addition of items to cart. If you see in this perspective this module this module is quite different from dc_cart_ajax.

Code review of this module as per ventral.org and DrupalCS has been done.

Ahh, yes, dc_cart_ajax is only for removal. I made my own module to ajaxify adding to the cart by adding a couple of functions to dc_cart_ajax (I probably should have submitted a patch).

What I really don't want to see is many different modules providing ajax functionality to commerce and the functionality of each overlapping. As I know I am going to be using them in the future. What also worries me is if they add quite specific functionality that can not be easily removed.

Can the popup thing after adding a product be disabled in your module?
Also, I see your module creates an alternative block to display cart items. I think doing it the way that dc_cart_ajax did it might be better. It uses a custom js command to update the default commerce cart block.

Just my 2 cents :)

I might have missed the patch while I was searching for a module that ajaxifies the addition of items to cart.

Yes sure, the popup thing can be disabled. Read the module's README and there are instructions to customize the appearance of the blocks.

By the way the dc_cart_ajax module changes the view that is available in /cart page of a commerce website. Removing any item form there will obviously affect the default commerce cart block, but the thing is there are many commerce websites where you will see a commerce cart block visible on all pages, and also they are ajaxified. See this website http://www.foodpanda.com, here you will see that addition of food items are ajaxified and the cart block appears on almost all pages(although there are few pages in that website), and like that there are many other websites implementing that functionality. Also you will see that items can be removed from the cart itself, I am planning to implement this functionality in this module's next release.

Anyways, here is my opinion that why I want to contribute this module. Any code review error or feature that can improve the functionality of this module are welcome :)

Status:Needs work» Needs review

Code review done as per ventral.org and DrupalCS.

Some suggestions of smiletrl have also been implemented.

Status:Needs review» Needs work

Hi subhojit777!

The PAReview tool identified a few minor issues:


Manual review:

  • All Drupal Commerce modules are prefixed by Commerce*, please rename your módule.
  • Have a lot of variables, and I think many of them could be grouped into a single array, saving variable requests.
    You could also use default constants, while administrators do not set your settings.
  • I did check for duplicate modules. About the nearest I can find is Commerce Cart Ajax. But it seems that both do exactly the same. The other only focuses on the process of updating the cart, not the process of adding.

Aldo add in info file:
package = Commerce (contrib)

The rest of the code seems fine.


Status:Needs work» Needs review

Code review done as per DrupalCS and ventral.org

Module name changed and code fixes done


in your function dc_ajax_add_cart_get_shopping_cart_teaser in your .module file I would look to initialise the variables at the start and eliminate the else loop:

  $commerce_cart = dc_ajax_add_cart_get_commerce_cart_details();
  $line_items = NULL;
  $quantity = 0;
  $total = NULL;

also since you don't use the var $content again in the function just return the theme

return theme('ajax_shopping_cart...

with the function dc_ajax_add_cart_get_commerce_cart_details

initialise you array at the beginning like so :

$output = array("order"=>NULL,'wrapper'=>NULL);

eliminate the else

look to populate and check the var $order in the if clause:

if ($order = commerce_cart_order_load($user->uid)) {...

The var $wrapper is only used once, so just assign the array to the function:

$output['wrapper'] = entity_metadata_wrapper('commerce_order', $order);

Sorry if I'm being too critical , just working on my review bonus :)

This may not be the most important issue at the moment but i want to report a problem.

I installed these modules:


and this one in the sandbox.


Then i installed your module (great module, congratulations!) . It worked fine when i click on add to cart button.

Then i uninstalled all the modules (including yours) mentioned above. Now i am getting this error when i click to Add to cart buton:


Error messageEntityMetadataWrapperException: Unknown data property commerce_webform_sid. in EntityStructureWrapper->getPropertyInfo() (line 339 of /path/to/my/site/sites/all/modules/entity/includes/entity.wrapper.inc).
The website encountered an unexpected error. Please try again later.

I reenabled your module but now i am getting this error when i click Add to cart button (as a js alert):

An AJAX HTTP error occurred.
HTTP Result Code: 500
Debugging information follows.
Path: /mysite/cano/en/system/ajax
StatusText: Service unavailable (with message)
ResponseText: EntityMetadataWrapperException: Unknown data property commerce_webform_sid. in EntityStructureWrapper->getPropertyInfo() (line 339 of /path/to/my/site/sites/all/modules/entity/includes/entity.wrapper.inc).

I am not sure if it has anything to do with the above modules though, but i assume you already did enabled and disabled a few times and didn't notice such an error.

Judging by the error, your problem seems to be with the commerce_webform module.

Hi Subhojit,

This looks like a great module, I love it. Installed and tested and it works straight away, even on our pretty complex commerce setup.

I've spotted a few minor issues that you should fix, I'd be happy to mark this as RTBC once 1 and 2 are fixed (then for klausi to have the final say). 3 and 4 would be nice to see fixed but not critical imo.

1. gitignore
You need to remove the .gitignore file, you can set ignores for yourself in ~/.gitignore

2. Define namespaces
Defines need to be prefixed with the module name to avoid namespace collisions with other modules. eg
define('DISPLAY_POPUP', 'dc_ajax_add_cart_display_popup');
should be
define('DC_AJAX_ADD_CART_DISPLAY_POPUP', 'dc_ajax_add_cart_display_popup');

3. Variable overwritten
In dc_ajax_add_cart_remove_commerce_line_item()

  // Initially quantity of products in cart will be zero.
  $quantity = 0;

The $quantity variable is overwritten before it is used. This line can be removed.

4. Code clarity
$is_ajax = $ajax === 'ajax';
would be a little clearer as
$is_ajax = ($ajax === 'ajax');
I was reading this as
$is_ajax = $ajax = 'ajax';
at first.

Status:Needs review» Needs work

Setting to needs work.

@muhleder thanks for your feedback. I will try to fix the problems ASAP. Apart from the issues that you all have mentioned, I have noticed another issue, that is when you add a product to cart, the popup does not shows the right product details currently added to cart.

I am trying to fix this problem. I was hoping that there would be some kind of function that returns the product currently added to cart.

Any thoughts? Please share :)

Status:Needs work» Closed (won't fix)

Closing due to lack of activity. Feel free to reopen if you are still working on this application.

I'm a robot and this is an automated message from Project Applications Scraper.

My install seems to be working inconsistently. The only two differences about my product are:

1) I have multiple products I need to show on one page, so there are multiple Add To Cart buttons showing at once
2) I'm using a pricing rule on one of the fields to enter a custom price (which breaks when your module is enabled, works when not enabled)
3) The quantity button is added through a custom View and not through the standard install view (the quantity button breaks as well and doesn't add multiple quantities when your module is enabled)

This 4 minute video shows what's not working (too confusing to explain in text)


Any ideas? Thx!

@reubenlara thank you very much for using this module and finding bugs. There are bugs present in the module but I was not expecting bugs as you described.

Could you please explain the configuration of pricing rule and custom views, I want to replicate the bug.

Assigned:Unassigned» subhojit777
Issue summary:View changes
Status:Closed (won't fix)» Active