I want to write some code as an reaction to a product being added to the cart.

To my surprise it seems it's only possible to react if a user tries to add a product to cart, and not if it actually succeeded.

In my use case, I want to add some tracking on the site when users add an item to the cart, so it makes a big difference if the product is actually added, or only is attempted to be added.

Maybe it's just me that overlooked something, but going through the uc_cart_add_item function, I couldn't see how this would be possible. I figured this would be a pretty standard request, to act on products being added, so maybe I'm missing something?

CommentFileSizeAuthor
#2 hook_uc_cart_modified.patch1.47 KBalanmackenzie

Comments

longwave’s picture

If you want to propose the name of the hook, and maybe even add a patch to uc_cart that invokes it, that would be helpful in getting this added.

alanmackenzie’s picture

StatusFileSize
new1.47 KB

I've attached a patch for the 6.x-2 dev branch that adds hook_uc_cart_modified($op, $cart_item_id, $qty, $data).

Explanation of Parameters:

Parameter Value
$op insert, delete or update.
$cart_item_id The id of the cart item that is being acted on.
$qty The quantity of items that are being acted on. This is always 0 for the delete operation.
$data The data array, including attributes and model number adjustments.

Here is an example implementation of hook_uc_cart_modified():

function test_uc_cart_modified($op, $cart_item_id, $qty, $data) {

  // Find the cart item that matches the id passed to us.
  $item;
  $contents = uc_cart_get_contents();
  foreach( $contents as $content ) {
    if ( $content->cart_item_id == $cart_item_id )
      $item = $content;
  }

  // The name of the product we're going to print out.
  $name = $item->title;

  // Print a different message depending on what action was taken.
  switch ( $op ) {
    case 'delete':
      drupal_set_message("You deleted $name from your cart.");
      break;
    case 'insert':
      drupal_set_message("You added $name to your cart.");
      break;
    case 'update':
      drupal_set_message("You updated $name in your cart.");
      break;
  }
}

Feedback welcome, particularly on the name of the hook and whether the $cart_item_id parameter should just be a full $item object.

I'm also willing to write a D7 version of the patch just let me know and I'll do it.

alanmackenzie’s picture

Status: Active » Needs review

status update.

Status: Needs review » Needs work

The last submitted patch, hook_uc_cart_modified.patch, failed testing.

tr’s picture

Version: 6.x-2.4 » 7.x-3.x-dev

Feature requests should be made against HEAD.

I'd greatly prefer to address this in the current version of Ubercart first (7.x-3.x).

longwave’s picture

Version: 7.x-3.x-dev » 6.x-2.x-dev

This will be fixed in 7.x-3.x by #1492626: "Cart Items" triggers dont work

longwave’s picture

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

No new features are being added to 6.x-2.x, as per #1903564: [policy] Maintenance phase for 6.x-2.x branch