0

In Drupal Commerce Kickstart, I used custom PHP page in Drupal root folder. As per requirements, I chose custom PHP Code to show products in different logic pattern. I am getting product id value for each product in that page. Now, I need "Add to cart" button which should generate order number and redirect it to checkout page directly.

How do I generate an order using the "Add to Cart" button in a custom PHP page?

1 Answer 1

1

Though I prefer the Commerce Add To Cart Filter module to adding a "Add to cart" button anywhere in my content by mentioning the product ID with the suggested method, since you need PHP code, I am copying my old code which I don't use anymore; maybe it can help you.

function MYMODULE_commerce_menu() {
  $items = array();
  $items['mysite/trial-buy'] = array(
    'title' => 'Trial Plan',
    'page callback' => 'MYMODULE_add_single_product',
    'access arguments' => array('view published content'),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function MYMODULE_add_single_product(){
  global $user;
  $product_id = 59;//This is my product ID, place your's here.
  $order = commerce_order_new($user->uid, 'checkout_checkout'); 
  commerce_order_save($order);
  $product = commerce_product_load($product_id);
  $line_item = commerce_product_line_item_new($product, 1, $order->order_id);
  commerce_line_item_save($line_item);
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  $order_wrapper->commerce_line_items[] = $line_item;
  commerce_order_save($order);  
  drupal_goto('checkout/' . $order->order_id);//Forward to checkout. You can have your method here.
}

This is the code I came out with the help of Commerce guys documentation when I was working with some custom need like yours.

5
  • I think above code is related to creating a module and using it, but I have no knowledge in module development. I kept my php page in drupal root folder (Ex: localhost/commerce/test.php) and used normal PHP coding with existing database connection. Everything I need from that page only. Please suggest any other possible solutions.
    – J Sinha
    Aug 11, 2013 at 17:00
  • @JSinha Can't help in your scenario..Sorry :(
    – RajeevK
    Aug 11, 2013 at 17:05
  • ok. Thanks for u r contribution and fast reply. I have one small doubt, In drupal 7 in themes folder page.tpl.php file will exist.. but in drupal commerce kickstart, there is no page.tpl php page. What is the common page for this drupal commerce kickstart? then I'll try to use the code there and try to link with modules.
    – J Sinha
    Aug 11, 2013 at 17:17
  • @JSinha You will find that in profile/theme I hope as it's distribution. But my code wont work in tpl file in there..
    – RajeevK
    Aug 11, 2013 at 17:22
  • ok. I'll check.
    – J Sinha
    Aug 11, 2013 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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