8

Is it possible to empty the cart with drupal commerce by visiting a url?

/cart/empty

Trying to create a test case for selenium

5 Answers 5

13

The code would look something like this:

function MYMODULE_menu() {
  $items['cart/empty'] = array(
    'title' => 'Empty Cart',
    'access arguments' => array('access content'), // or whatever permission you want
    'page callback' => 'MYMODULE_empty_cart',
    'type' => MENU_CALLBACK
  );

  return $items;
}

function MYMODULE_empty_cart() {
  global $user;
  // Load the order and empty the cart
  $order = commerce_cart_order_load($user->uid);
  commerce_cart_order_empty($order);

  // As this page won't display anything you need to redirect somewhere
  drupal_goto('some/page');
}
0
1

You could define a URL with a callback function in which you just programmatically clear the cart's content.

1
  • 1
    Could you provide what that looks like in a module?
    – chrisjlee
    Commented Apr 4, 2012 at 15:55
0

I know this is an old thread, but there is now a module for this see Commerce Empty Cart Paths. This module allows site administrators to define multiple paths, and when a user visits a non-declared page, their shopping cart is emptied.

0

As suggested, try Commerce Empty Cart Paths module.

It allows site administrators to define multiple paths, and when a user visits a non-declared page, their shopping cart is emptied.

Usage:

  1. Download and enable the module.
  2. Go to admin/commerce/config/empty-cart-paths and enter any additional paths. checkout, checkout/, cart and cart/ are declared by default.

Or combert's sandbox: Commerce Clear Cart module which simply adds a page that when visited will empty the current users Commerce cart. It also adds a link to that page to the Commerce cart view.

0

I hope this helps someone having the same issue... I installed Path Rules https://www.drupal.org/project/pathrules. From there create a custom rule... 1. Event ->Drupal is initializing 2. check path cart/empty 3. Actions: Remove all products from an order 4. goto Parameter: URL: [site:url]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.