Vote up!
Vote down!

How do I insert "Add to cart" button in my custom module?

Hello Experts,

I am new to Drupal Commerce and I am loving it. I have a unique requirement where my product is a Card Design, hence it needs a lots of customization, I have done the customizations part in my custom module. Now I would like to display an "Add to Cart" button in my custom module, clicking which that product and (X) number of quantity should be added to the Drupal Commerce Cart.

To make a note here is what I am looking for.

1- Add to cart button my my custom modules to template page where I know the product id, and upon clicking it, it should add the product to the cart. I also want to pass the number of quantity from the module it self.

2- Then it should take the user to the cart page.

Thanks in advance.

Asked by: dharam1987
on June 22, 2012


having another question here, using drupal_goto can I add a product to cart and it will redirect to the shopping cart page as well. Please advice. !

- dharam1987 on June 25, 2012

I was using drupal_goto('checkout/' . $order_id) to bypass the cart and proceed directly to checkout. The $order_id was returned when the order was created. In drupalcommerce, it seem that each user can own multiple carts, and each cart is linked to an order_id.
So you will need to redirect to the correct cart. There is also some kind of "default cart". Anyone can confirm this, maybe elaborate further?

- binarycubes on June 26, 2012

2 Answers

Vote up!
Vote down!

commerce_cart_product_add_by_id('product_id','quantity'); calling this function in my module has done the trick.

However as far as performance is concerned I have a question here, looks like this function is available to me when ever I called it, I did not load it earlier, does this mean every time any page we open Drupal loads all the api functions ?

Answer by: dharam1987
Posted: Jun 25, 2012