1

So I have created a product in Commerce: a £15 one-off donation.

I don't need a product page for this (I have a custom block with links for different donation amounts). When you click a link, I want to use the product ID, and push the product straight to checkout.

How would I go about this?

3

The way to programatically add a product to the cart and send a user to checkout would be to use the function:

function commerce_cart_product_add_by_id**($product_id, 
                        $quantity = 1, $combine = TRUE, $uid = NULL)

and

function drupal_goto**($path = '', array $options = array(),
       $http_response_code = 302) 

to redirect the user to checkout.

Here is information on the functions:

  • This is great through template.php, but how would I go about achieving this through a simple anchor link? Can I somehow call the functions on a condition? – Scott Lawrence Sep 28 '12 at 5:37
  • 1
    One way to do this is to create a custom module and create a new page via hook_menu. Have the links reference the product id (For instance: yoururl.com/yourproductaddingpage?id=22)and use arguments through hook_menu to relay the product id to the function itself. When the function receives the product the product is added to the cart and the drupal_goto function sends the user to checkout. – generalconsensus Sep 28 '12 at 6:20
  • 1
    These functions work thanks. Further to this, because I'm handling one-off donations the cart is redundant (and causes confusion!), is there a way to force one product through checkout without using the cart? – Scott Lawrence Sep 28 '12 at 11:37
  • 1
    I did this on my last site, I just used a hook to disable it for users that didn't have certain permissions. Disabling the cart than would allow you to do your redirect directly to checkout and the user would never see that there was a cart. You would also want to remove the drupal_set_message that tells the user that the product was added to cart. The hook: function hook_menu_alter(&$items){ $items['cart']['access arguments'] = array('administer checkout'); $items['cart/my']['access arguments'] = array('administer checkout'); //unset($items); } Unsure where the message is – generalconsensus Sep 28 '12 at 18:27
0

This question popped updated because of being edited and I thought I would share how to do to this via module. I wrote a custom module to do just this because we have a lot of direct marketing and I often need the ability to throw someone directly into a checkout process via URL. This module takes a URL constructed of the SKU and the quantity and puts the customer into a checkour process. The URL is http://www.example.com/product/add/SKU/QUANTITY. As an example SKU 101 at a quantity of 2 would be:

http://www.example.com/product/add/101/2

Here is the module: https://github.com/taz77/drupal-add-to-cart-url

  • This module also allows the passing of tracking query strings for Google Analytics and the Affiliate NG module. It preserves any query sting basically. – Brady Aug 27 '15 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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