Cart Links API

Contrib type: 
Module
Status: 
approved

Downloads


Compatibility: 
Ubercart 1.x
This module is now in Ubercart core, so the download has been removed. 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. 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.
ActionDescriptionArgument
iSets the ID of the cart link.A custom text ID for the link.
eEmpties the customer's cart.None.
mDisplays a preset message to the customer.A message ID.
pAdds 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.
Related threads: 
amitaibu@drupal.org's picture
Offline
Bug Finder
Joined: 09/08/2007
Juice: 239
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

fiasst's picture
Offline
Joined: 10/15/2007
Juice: 28
Bloomin fantastic Thanks!

Bloomin fantastic Smiling Thanks!

torgosPizza's picture
Offline
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.
Joined: 08/14/2007
Juice: 4111
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.

--
Help directly fund development: Donate via PayPal!

torgosPizza's picture
Offline
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.
Joined: 08/14/2007
Juice: 4111
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.

--
Help directly fund development: Donate via PayPal!

mandclu's picture
Offline
Bug Finder
Joined: 11/19/2007
Juice: 78
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.

jdblank's picture
Offline
Bug Finder
Joined: 12/11/2007
Juice: 127
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.

TR
TR's picture
Offline
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3294
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?

<tr>.
Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15459
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.

torgosPizza's picture
Offline
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.
Joined: 08/14/2007
Juice: 4111
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:

<?php
if (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.

--
Help directly fund development: Donate via PayPal!

torgosPizza's picture
Offline
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.
Joined: 08/14/2007
Juice: 4111
Gotcha for adding to cart double-click issue

Sorry for double post .. UC.org was having some database issues =/

--
Help directly fund development: Donate via PayPal!

mandclu's picture
Offline
Bug Finder
Joined: 11/19/2007
Juice: 78
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');
Tanjerine's picture
Offline
Bug FinderInternationalizationizerNot KulvikThe other woman.
Joined: 08/31/2007
Juice: 235
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! Smiling and it saved me a lot of trouble. Smiling thankies!

primo411's picture
Offline
Joined: 07/22/2008
Juice: 23
Delete item link

Hi, I'm trying to make a column with cliquable delete icons instead of the checkboxes in the cart. I use the cart links API in a "tricky" way: in fact, I generate for each item line a link that empties the cart, and then adds all the other products with the same quantities as before. It works fine, but I can't find how to disable the "Product added to cart message", even by using "m0" as said on the help page. For example, I tried:
cart/add/e-p13_q11-p4_q17-p11_q1-p6_q12-m0?destination=cart => doesn't work
cart/add/e-p13_q11-m0-p4_q17-m0-p11_q1-m0-p6_q12-m0?destination=cart => doesn't work

a_c_m@drupal.org's picture
Offline
Bug Finder
Joined: 01/02/2008
Juice: 288
Re: Delete item link

This thread feeds my point on http://www.ubercart.org/node/6050 if you agree please say so in the comments so it get some attention.

Documentation needs to be clearer. I think
cart/add/e-p13_q11_m0-p4_q17_m0-p11_q1_m0-p6_q12_m0?destination=cart should do what you need.

primo411's picture
Offline
Joined: 07/22/2008
Juice: 23
Re: Re: Delete item link

That was it, thank you. So underscore refer to the actions on one product.

japerry@drupal.org's picture
Offline
Bug FinderGetting busy with the Ubercode.Not Kulvik
Joined: 08/08/2007
Juice: 248
Adding a SKU

I have a client who has a different way of adding products to the cart, by SKU in the old ecommerce system. It looks like the easiest way to convert the site is by allowing the cart_links api add to cart VIA a SKU.

Here is some of my code to allow this, all takes place in uc_cart_links.module
First, add case s: after case p:, near link 190

// Add a product to the cart.
case 'p':
case 's':

Then create the specific case for SKUs....

//add the following after case 'm' in uc_cart_links
case 's':
  $sku = substr($part, 1);
  if($prod_info = uc_cart_links_by_sku($sku)) {
    if(!is_array($prod_info)) {
      $p['nid'] = $prod_info;
    } else {
      $p['attributes'] = $prod_info['attributes'];
      $p['nid'] = $prod_info['nid'];
    }
    break;               
  } else {
    break;
  }

Then create a function called uc_cart_links_by_sku($sku):

function uc_cart_links_by_sku($sku) {
  //check to see if its a product or attribute combination
  $prod = db_query("SELECT nid FROM {uc_products} WHERE model ='%s'",$sku);
  $attributes = db_query("SELECT nid,combination FROM {uc_product_adjustments} WHERE model ='%s'",$sku);
  if(db_result($prod)) {
    return db_result($prod);
  } else if($atr = db_fetch_array($attributes)) {
    return array('nid' => $atr['nid'], 'attributes' => unserialize($atr['combination']));
  } else {
    return false;
  }
}

And there you go, you can now do quotes like this: /cart/add/sMYSKU043HERE_q1?destination=cart

aasarava@drupal.org's picture
Offline
Joined: 04/29/2009
Juice: 15
Hiding the 'add to cart' message

I just wanted to highlight the point made by a_c_m about hiding the "add to cart" message, just to make it more clear for anyone having trouble.

To hide the message, use _m0 (underscore m zero) after the product action, not -m0 (dash m zero).

For example: /cart/add/e-p1_q1_m0

Foaman's picture
Offline
Joined: 10/16/2009
Juice: 33
How do I pass the URL into the SKU

I would like to be able to pass in the URL of the node on which the "add to cart" button is located. I would also like to add different options per node for shoppers to add to their carts. So..., for the shopper: "Add option 1 from url to cart," "Add option 2 from url to cart." For the merchant: "Shopper 1 has added option 1 from x url." "Shopper 1 has purchased option 1 from x url."

jww2nc's picture
Offline
Joined: 08/17/2009
Juice: 2
Coupons?

Is there a way to include a coupon code (see uc_coupon module) to the cart-link so that the cart will auto-process the coupon instead of the user having re-type it in?

kafka93's picture
Offline
Joined: 02/19/2010
Juice: 5
Problems with add-to-cart not respecting pricing options

Hopefully someone's still reviewing this topic... Eye-wink -- we're running into problems with the API where the product is added to the cart correctly, and shows the right attribute option (in our case, "license type"), but does not respect the specific price for that option and instead just uses the default price for the product. Anyone else run into this?

rl9022's picture
Offline
Joined: 12/07/2009
Juice: 20
Re: Cart Links API

I have three of buttons on a page that use the Cart Link.

is there a way to track which button is being clicked? I'm not sure if there is a way to make a custom attribute or something like that. (I use Google Analytics - not sure if there is a GA friendly way to track this)

burtsbees's picture
Offline
Joined: 03/23/2010
Juice: 29
Re: Re: Adding multiple when navigating Back

I am having this double click issue as well. Did you find out a simple solution?

bertomart's picture
Offline
Joined: 08/09/2008
Juice: 41
A couple things

So I'm using this module and I wanted to be clear on something. The "Product ID" is the SKU? or is it the Node ID?
I can't seem to get this thing to work. I have a url like this: /cart/add/p16 (16 is the node ID). I took off the redirection, I just want to see if this would be added to the cart, but its not being added.
Any suggestions would be most welcome.

Thank you all.

tnichols's picture
Offline
Joined: 05/05/2010
Juice: 7
Node Checkout link, and Destination

I'm having two problems that hopefully someone can help with:

1. My link is not redirecting to the destination in the URL, it goes to the front page (<front>) instead
The relative url I'm producing is:

  cart/add/p213_a2o3-iannual_membership_upgrade-mUpgrade_to_Annual_Membership?destination=cart
 

2. The product I'm linking to is a uc_node_checkout product. I need to be able to pass the Node ID of the page that is being managed in the link as well. Is there any way to do it with this plugin?

Thanks....

Tim

tnichols's picture
Offline
Joined: 05/05/2010
Juice: 7
add a 'd' data option to cart links?

Nevermind the redirect issue. I was having an old age moment and trying to pass the query in the destination to l().

I'm still looking for a way to send the node_checkout_nid to the cart. One method might be to add a 'd/v' option to cart links. Anything passed with it would be appended to the product data. The dataname and value would need to be encoded to reduce conflict with parsing of the url.
In my case i want to add key->value pair of
'node_checkout_nid'=>213
to the products data array. So I envision passing the following in the URL
_dnode%5fcheckout%5fnid_v213

Any thoughts? Does this open up security issues that do not already exist in the module?

Thanks!

lipinponmala's picture
Offline
Joined: 08/11/2010
Juice: 59
Hi I have almost same type of

Hi
I have almost same type of issue...when we add the products through the cart links the default SKU is selected (the alternate SKU not getting affected). i made a form to accept inputs and create cart links. i am using version 2.4 of ubercart .. please help me to solve this.

csdesignco's picture
Offline
Joined: 02/10/2010
Juice: 230
Re: Hi I have almost same type of

The top of this page says "Compatibility: Ubercart 1.x". Should this be revised to include 2.x? I've been looking for the API page for cart links in Ubercart 2 and kept ignoring this page.

TR
TR's picture
Offline
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3294
Re: Re: Hi I have almost same type of

As it says at the top "This module is now in Ubercart core". And has been for at least three years. This thread describes the contributed cart links module that existed before it was moved into core. It is now of historic interest only.

<tr>.
garydev's picture
Offline
Joined: 03/02/2011
Juice: 24
Cart Links to Pass URL to Ubercart

Greetings -
I have a Flash front end and an Ubercart Cart. The Front end functions as a product designer. I am passing variables from user's design (5 layers) to textfield attributes. These layers report to the textfields in the cart on checkout to inform manufacturing of the "bill of goods" for the design (text, font, image, position, etc).

When user selects "Purchase" in Flash front end Flash records a jpg of the design and saves it in a folder on the server.

I am trying to pass that URL into a textfield attribute. The problem is that the "// is being striped from the URL being passed and only http: is making it. I've tried to encode the ascii equivalent &#47 for "/" but it still breaks it.

Anyone have any guidance for how to pass that url into the textfield attribute?

Thanks

csdesignco's picture
Offline
Joined: 02/10/2010
Juice: 230
It is now of historic

It is now of historic interest only.

So where would one find current documentation on the Cart Links API that is in Ubercart core?

garydev: I have used the uc_attribute_files module with success: http://www.ubercart.org/forum/development/17782/handling_print_shop_cust...
although what you're currently trying to do should be achievable with an attribute of type "textfield" and a custom module to play with the data.

TR
TR's picture
Offline
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3294
Re: Cart Links to Pass URL to Ubercart

Embedding a URL inside another URL will be problematic - I think I would try base64 encoding your URL before putting it into the textfield, that will guarantee it can be passed unscathed. You can then decode it on the back end, maybe in the template you're using for your bill of goods. Or earlier. If you need to see it in the cart you can decode it in hook_uc_cart_item(). You might also want to use a relative URL rather than an absolute.

<tr>.
TR
TR's picture
Offline
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3294
Quote: So where would one
Quote:

So where would one find current documentation on the Cart Links API that is in Ubercart core?

admin/store/help/cart-links or admin/store/help/cart_links if you're using Ubercart 2.x or earlier verions of Ubercart 3.x. The documentation is essentially the same as at the top of this page. There's not much to document - cart links are very simple.

<tr>.
garydev's picture
Offline
Joined: 03/02/2011
Juice: 24
Little More Info

Hi TR -
Thanks very much for your response. I am able to encode the URL in base64 as suggested. Now I'm hung up on the decoding portion. I apologize is this is obvious but where would I Implement the hook_uc_cart_item?

I presume it is in uc_cart.module but a confirmation would be helpful

Do I simply add

<?php
$str
= 'somebase64 stiring text==';
echo
base64_decode($str);
?>

Thanks in advance?

garydev's picture
Offline
Joined: 03/02/2011
Juice: 24
Re: Little More Info

Bump de bump.

TR
TR's picture
Offline
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3294
Re: Re: Little More Info

Hook functions are a basic Drupal concept. You'll have to write your own little module to implement a hook function. This is explained in every single Drupal book, and in a lot of documentation on drupal.org. I wrote a post a long time ago showing how to write a tiny module for a different hook function - the details for hook_uc_cart_item() will be different, but the principle will be the same: http://www.ubercart.org/forum/support/2191/removing_sku_product_edit_pag...

<tr>.
royerd's picture
Offline
Joined: 01/22/2008
Juice: 295
other actions

Is it possible to have other actions for the cart link? For example, I'd like the cart link to also choose one of the payment methods (if the coupon is a 100% off, then choose "Free order" payment method).

This may involve some programming--but where would I start with this? Can someone suggest something for me?

Thanks much,

Dan

satter9's picture
Offline
Joined: 07/29/2011
Juice: 7
Cart Links are re-directing to "Shopping Cart" page

Hello,
I am having trouble with the cart links. Once I set up a cart link such as cart/add/p1555_q1?destination=cart/checkout then it re-directs the user to the Shopping Cart page and says the cart is empty. Is there any reason why this might be happening?

garydev's picture
Offline
Joined: 03/02/2011
Juice: 24
Cart will not update qty or remove items

Hello,
I have an issue and I am a bit stumped. I have a site http://www.americraft.us which is built upon Drupal 6.14 and Ubercart 2.2. I know there are updates which need to be applied but I have working applications that have been in development for some time and I don't want to risk breaking. I'll update once I have the application completed.

A. The features a custom Flash application that allows for the (WYSIWYG) customization of blank products. The customized product is passed to Ubercart utilizing Cart Links module.

B. In addition, there are products available in the Ubercart products catalog (non-flash app, standard Ubercart products) which utilize Textfield Attributes to make products customized. This branch utilizes Cart Links to pass the text field attribute contents to the Cart.

C. Finally, there are some products within the Ubercaert products catalog which do not have textfield attributes.

When adding any product from "C" to the Cart - the cart updates qty and removes products from the cart as the cart should when operating normally.

When adding any product from "A" or "B" to the Cart the cart will not update qty or remove items.

I have applied the patch which is supposed to address this issue for textfield attributes but it had no impact on the problem. Any advice on where I should look next?

Thank you in advance.

echoleaf's picture
Offline
Joined: 08/03/2008
Juice: 258
Re: Cart Links API

I wouldn't say cart links are easy, since I've followed the instructions and they don't work. Something simple like

cart/add/p1623?destination=cart

goes to a 'Page not found.' 1623 is the NID, but changing it to the SKU (1) makes no difference.

I'm using UC 2 - can someone point out what I'm doing wrong? Even removing the destination does not add the product to the cart.

TR
TR's picture
Offline
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3294
Re: Re: Cart Links API

Did you enable the cart links module?

<tr>.
moonshdw8's picture
Offline
Joined: 08/24/2012
Juice: 17
No redirect

I've been using cart links and it's pretty great.

I'm in a situation where I created an "Add to Cart" button using Cart links. However, when clicking it, the user gets redirected to the frontpage. The url looks like this "https://mysite.com/cart/add/p38".

I'm wondering if there is a way to keep the user on the current page and not redirect them?

I can't just add the regular "Add to Cart" field like you can with the catalog because I'm using a Search API custom Index View, which is why I was trying to use Cart Links as an alternative.

Thanks for the great module!

drupalfever's picture
Offline
Joined: 05/14/2011
Juice: 57
I just wanted to post an example where all the options are used

The above instructions helped me to understand how to create this link but there were still some questions left that I had to answer by doing some experimentation. Following is a link that is using all the options that you can use.
 

http://www.myUbercartWebsite.com/cart/add/e-p2457_a3o28_a4o27_a1o2_q5-imonday_special-p7753_q1?destination=cart

 
Let me break down the above link so it can be better understood:

  1. http://www.myUbercartWebsite.com -> Obviously, your website address.
  2. cart/add/ -> The path you always need to have in order for this to work.
  3. e -> If this command is present, your shopping cart will be emptied.
  4. "-" -> Dash is the command separator.
  5. p2457_a3o28_a4o27_a1o2_q5 -> The full product command.
    1. p2457 -> The p indicates that the number that will follow will be a Node ID of the product that you want to add to your shopping cart.
    2. "_" -> The underscore character separates the options within a command.
    3. a3o28 -> This is the option that indicates an attribute of the current product. What goes after letter "a" is the Attribute ID and what goes after the letter "o" is the Option ID of the current attribute.
    4. a4o27 -> Is another attribute for the current product.
    5. a1o2 -> Is yet another attribute for the current product.
    6. q5 -> The letter "q" indicates that the number that comes soon after is the quantity of the current product. In this case, 5 of the current product type are going to be added to the shopping cart with the same selected attributes
  6. "-" -> This dash indicates that the product command is over and what goes next in another command.
  7. imonday_special -> The letter "i" indicates that what comes next is a message. The message that comes after is "monday special". the spaces in the message must be replaced with underscore characters.
  8. p7753 -> This is another product command. You can add several different products to your shopping cart all at once with a single link. This product has no attributes and has no quantity indicator. As a result, only one unit of this product will be added.
  9. ?destination=cart -> This should always go at the end of the link. The first part of the command (?destination=) indicates that what will come next will be the path that you want to be redirected to when your Ubercart link is clicked.

I know that my explanation is a little bit too exhaustive but it never hurts to be thorough.

drupalfever's picture
Offline
Joined: 05/14/2011
Juice: 57
Here is your answer:

Hi, moonshdw8!

I know it's been a while but here goes your answer:

$path_array = arg();
$path = implode('/', $path_array);
$link = "https://mysite.com/cart/add/p38??destination=" . $path;