Drupal Commerce - Custom Add to Cart Link

Drupal Commerce is a whole pile of awesome. Once you wrap your head around the whole product vs. product display issue, that is… but thats a topic for another blog post. I will say that the separation does make sense, even if it does cause some confusion

The thing to keep in mind about DC (I can refer to it as DC because I am cool… and now you are too), is that its designed from the ground up to be an e-commerce platform instead of an actual shopping cart solution (like UC). In other words - DC provides the tools, API, and basic structure for you to build your own custom e-commerce solution. It will do all the basic stuff out of the box, but its greatest strength is in the ability to easy add your own modifications.

Adding products to the cart with a link

On a recent project, I had the need for an easy way to add products to the cart without relying on the “Add to cart” form. There are many ways to do this, but I decided that the best way was to create a menu hook item (and callback function) to let us add items to the cart with just a link!

The process is simple - we use hook_menu() to create a path that will call a function. Then we use the function to add the product to the cart. First - the hook_menu():

<?php
/**
* Implements hook_menu().
*/
function MYMODULE_menu() {
 
$items['product/add/%/%'] = array (
   
'page callback' => 'MYMODULE_product_add',
   
'page arguments' => array(2,3),
   
'access arguments' => array('access content'),
   
'type' => MENU_CALLBACK,
  ); 
  return
$items;

?>

The hook adds a path with two wildcards (which become variables later in the function). The first spot is the for the product id, and the second is the quantity. For example, the url “mysite.com/product/add/5555/2” will add the 2 of the product with id 5555 to the cart. When the user visits this path, the product id and quantity are sent to the MYMODULE_product_add function… which we need to add now:

<?php
/**
* Custom function to add product(s) to a cart by product id
*/
function MYMODULE_product_add($pid,$quantity=1) {
    if (
$product = commerce_product_load($pid) ) {
        global
$user;
       
$uid = $user->uid;
       
$line_item = commerce_product_line_item_new($product, $quantity);
       
$line_item->field_ticket_type['und'][]['value'] = 0;
       
$line_item = commerce_cart_product_add($uid, $line_item, FALSE);
       
drupal_goto('checkout');
    }   
}
?>

We start by trying to load the product object from the id. If it does, then we can continue… if not, then there is a problem with the product id and we should just quit while we’re ahead. Now we grab the user id for whoever just clicked on the link… its going into their cart after all. Then we create a new line item object, give it a zero value (don’t worry - DC will take care of the calculations later), and add it to the cart. Finally, we send the user to checkout. You may want to send them to the cart instead.

Taking it further

There you have it - nice and simple! Now we can add links to other pages, blog posts, ads, etc. and have those links directly add products to the cart. You can even use these links offsite! So - you can link directly to a product or service and have that go straight to checkout. Maybe send a link for work to be done to a client and have them immediately ready to checkout and pay.

You could also create your own forms and modify the function to pass other information to the line item. For example:

<?php
/**
* Custom function to add product(s) to a cart by product id, submitted by a form and looking for an affiliate cookie
*/
function MYMODULE_product_form_add($pid,$quantity=1) {
      
$other = $_POST["other_info"];
      
$affiliate = $_COOKIE['mysite_affiliate_id'];
    if (
$product = commerce_product_load($pid) ) {
        global
$user;
       
$uid = $user->uid;
       
       
$line_item = commerce_product_line_item_new($product, $quantity);
       
$line_item->field_ticket_type['und'][]['value'] = 0;
       
$line_item->field_other['und'][]['value'] = $other;
       
$line_item->field_affiliate['und'][]['uid'] = $affiliate;
       
$line_item = commerce_cart_product_add($uid, $line_item, FALSE);
       
       
drupal_goto('checkout');
    }   
}
?>

For more details, check out the API info for commerce_product_line_item_new and commerce_cart_product_add. I’ve also attached a sample module below with the code above. Untested, so use it with caution. Happy commercing!

AttachmentSize
Sample module containing this code852 bytes