The Cart Links API is a module that enables store owners to create specially crafted links that add products to customer shopping carts and redirect customers to any page on the site. A store owner might enable this module to provide a link in an e-mail or on any node in a site that functions as a "Buy it now" link for any product on the site. There is also a reports page that lets administrators track the effectiveness of cart links by seeing how many times they've been clicked.
This module has already been committed to the Bazaar repository but is being posted now so folks can use it until the next release comes out. At that time, I'll remove this contrib from the site.
Use this link to test the module on the Livetest:
Buy 2 Tin Can Telephones Now!
There are a few simple rules used when crafting links. These are taken from the module's settings page:
Cart links are simple to form using a few actions and arguments with the following rules:
- The cart link should be /cart/add/cart_link_content.
- Chain together as many actions as you want with dashes.
- Do not put any spaces or use dashes in any action arguments.
- Use the table below to learn about actions and their arguments.
- Arguments come directly after their action letters.
- Specify the redirection by adding ?destination=url where url is the page to go to.
| Action | Description | Argument |
|---|---|---|
| i | Sets the ID of the cart link. | A custom text ID for the link. |
| e | Empties the customer's cart. | None. |
| m | Displays a preset message to the customer. | A message ID. |
| p | Adds a product to the cart. | A product string using the rules below... |
- You must at least specify a node ID immediately after the 'p'.
- Add additional specifications separated by underscores.
- Specify the quantity with q followed by the number to add.
- Specify attributes/options using a#o#, replacing # with the ID of the attribute and option.
- Turn off the add to cart message with m0.
Example: /cart/add/e-p1_q5-imonday_special?destination=cart
This will empty the cart, add 5 of product 1 to the cart, track clicks with the ID "monday_special", and redirect the user to the cart.
| Attachment | Size |
|---|---|
| uc_cart_links.tar.gz | 3.72 KB |


Hi Ryan, Thanks, seems a
Hi Ryan,
Thanks, seems a really good functionality!
While going over the description, it sounds as something that could have used the 'cclinks' from the workflow-ng module. Are you familiar with that?
Cheers,
Amitai
Bloomin fantastic Thanks!
Bloomin fantastic
Thanks!
Adding multiple when navigating Back
Ryan, the Cart Links API works wonderfully, except for this one little issue I've found. We have banner ads and stuff that allow a user to add a product from our homepage. The problem I've found, and this isn't a huge issue (and only happens rarely), if someone hits the back button, even if it's quickly - quick enough to trigger the action - they are sometimes adding another product to their cart. While I'd normally say that "buying two for the price of two" is great, it becomes an issue when we have to start refunding credit cards. Well, minor inconvenience, at least.
I wonder if there's a patch or maybe the addition of a flag, "oneonly" or something to that effect, that makes sure there can be only 1 of a particular item in a cart added by the link? (They could then update the qty in their cart if they wanted). I'm not sure if this is more relegated to a product attribute or to the cart links system.
Suppose I could write a module to take care of this too but I was wondering if you had any thoughts.
Re: Adding multiple when navigating Back
An update, I figured that it's not the "going back" method since everything is taken care of in the Drupal backend, but rather when people double-click a Cart Link, it will add 2 products to their cart. Not a huge issue but I'm sure there's an easy way to disable the button using some JS when it's clicked once. Found a couple solutions but none seems to be 100% so I'll just leave it for now.
destination not working for me, fix
This is a great addition to ubercart, but I wasn't able to get the destination to work, it always sent me to the top of the site. For some reason, the $_REQUEST['destination'] was not being set properly, maybe something to do with clean URLs.
Anyway, I was able to fix it by changing the start of uc_cart_links_process to be:
function uc_cart_links_process($arg1) {
$messages = array();
$start = strpos($arg1,'destination=') + 12;
if ($start) {
$dest = substr($arg1,$start);
}
else {
$dest = $arg1;
}
// Fail if the link is restricted.
$data = variable_get('uc_cart_links_restrictions', '');
if (!empty($data)) {
$restrictions = explode("\n", variable_get('uc_cart_links_restrictions', ''));
if (!empty($restrictions) && !in_array($arg1, $restrictions)) {
$url = variable_get('uc_cart_links_invalid_page', '');
if (empty($url)) {
$url = '<front>';
}
unset($_REQUEST['destination']);
drupal_goto($url);
}
}
if (!empty($dest) && empty ($_REQUEST['destination'])) {
$_REQUEST['destination'] = $dest;
}
Maybe there's some overkill in there, but it worked for me.
Adding price
Is it possible to send in the product price with this API? We have a situation where we want to create a product on demand with a unique price and want the person to be able to check out and pay.
Re: Adding price
In order for the cart link to work, the product has to already exist. Besides, it would be a Really Bad Idea to use the price encoded in a URL as the purchase price - what would stop someone from typing in the link with a different price and getting it for free?
Re: Re: Adding price
Agreed w/ TR on the possibilities for manipulation... I responded to your post, though, with a possible alternative. You can use hook_cart_item() to alter the price based on an invoice number.
Gotcha for adding to cart double-click issue
I mentioned this earlier in the thread and thought I'd post my solution I just recently implemented - it seems to work well. The main problem is when someone accidentally "double-clicks" a Cart Link, UC will add 2 of that item to the cart. In our store, that's almost never what the person wants. So to avoid this, add this little snippet to your implementation of hook_add_to_cart($nid, $qty, $data):
<?php
$items = uc_cart_get_contents();
foreach($items as $cartitem) {
if ($cartitem->qty > 0 && $cartitem->nid==$nid) {
$result[] = array(
'success' => FALSE,
'message' => t('Sorry, you can only add 1x '.$cartitem->title.' to your cart at a time. Contact us if you need help.'),
);
}
}
return $result;
}
?>
In my cart I also use eregi() to only limit this to a certain SKU - for example:
<?phpif (eregi('SKU-Pattern-Here', $cartitem->model) && $cartitem->qty > 0 && $cartitem->nid==$nid) {
?>
There's probably a better way to do this, a carriage-return delimited list in Settings someplace, (or better yet a multiple select list) that would return an array which you could then check against. Only SKUs in the array would be checked when this hook is called.
Anyways, hopefully someone else can find use for this.
Gotcha for adding to cart double-click issue
Sorry for double post .. UC.org was having some database issues =/
destination as a separate argument with drupal_goto
My bad, I didn't realize at the time that the real problem was that drupal_goto url encodes the destination string, which essentially breaks it. As it turns out, the real fix is to remember that if you're setting the destination and setting the redirect programmatically using drupal_goto, you should supply the destination string separately as the second argument. In my case, this looked like:
drupal_goto('cart/add/'. implode($cart_commands, '-'), 'destination=cart/checkout');Re: destination not working for me, fix
thank you! so very much! i was wondering how the hell to make cart links work without cleanURLs enabled. this worked like a charm!
and it saved me a lot of trouble.
thankies!