20

Ryan has some great code that you can programmatically create an order

<?php
global $user;
$product_id = 1;
// Create the new order in checkout; you might also check first to
// see if your user already has an order to use instead of a new one.
$order = commerce_order_new($user->uid, 'checkout_checkout');

// Save the order to get its ID.
commerce_order_save($order);

// Load whatever product represents the item the customer will be
// paying for and create a line item for it.
$product = commerce_product_load($product_id);
$line_item = commerce_product_line_item_new($product, 1, $order->order_id);

// Save the line item to get its ID.
commerce_line_item_save($line_item);

// Add the line item to the order using fago's rockin' wrapper.
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$order_wrapper->commerce_line_items[] = $line_item;

// Save the order again to update its line item reference field.
commerce_order_save($order);

// Redirect to the order's checkout form. Obviously, if this were a
// form submit handler, you'd just set $form_state['redirect'].
drupal_goto('checkout/' . $order->order_id);
?>

http://www.drupalcommerce.org/questions/3259/it-possible-drupal-commerce-work-without-cart-module

I have a site where I want to take anonymous donations so I have two problems.

  1. If a user is not logged into the site they get an access denied message
  2. The checkout process asks for name, address etc.

What I want to do is have a page where you confirm the amount then get taken to the payment page. In this case I am using PayPal WPS so redirecting there would be great.

Any advice you could give would be appreciated.

4
  • Great, you question prevent me to ask qustion and solve my problem charmingly :)
    – Yuseferi
    Commented Nov 26, 2013 at 8:43
  • @zhilevan thanks for commenting I got this working so just need to remind myself of the answer. I'll add that as well
    – user13134
    Commented Nov 26, 2013 at 8:45
  • I implement this code in another project, but when nor root user run it , return page not found !!!
    – Yuseferi
    Commented Mar 5, 2014 at 11:53
  • The requested page "/nashrtest/checkout/12" could not be found.
    – Yuseferi
    Commented Mar 5, 2014 at 12:44

4 Answers 4

12

You may try to test a new module called Commerce Drush which has the following syntax:

drush commerce-order-add 1
drush --user=admin commerce-order-add MY_SKU123

Manual solution

For creating an order programatically in Commerce, you can use the following code (it works with drush as well, e.g. drush -vd -u "$1" scr order_code-7.php). Please note that commerce_payment_example module is required.

<?php

  if (!function_exists('drush_print')) {
    function drush_print ($text) {
      print $text . "\n";
    }
  }

  $is_cli = php_sapi_name() === 'cli';

  global $user;

  // Add the product to the cart
  $product_id = 5;
  $quantity = 1;

  if ($is_cli) {
    drush_print('Creating new order for ' . $quantity . ' item(s) of product ' . $product_id . '...');
  }

  // Create the new order in checkout; you might also check first to
  // see if your user already has an order to use instead of a new one.
  $order = commerce_order_new($user->uid, 'checkout_checkout');

  // Save the order to get its ID.
  commerce_order_save($order);

  if ($is_cli) {
    drush_print('Order created. Commerce order id is now ' . $order->order_id);
    drush_print('Searching product ' . $product_id . ' in a Commerce system...');
  }

  // Load whatever product represents the item the customer will be
  // paying for and create a line item for it.
  $product = commerce_product_load((int)$product_id);

  if((empty($product->product_id)) || (!$product->status)){
    if ($is_cli) {
      drush_print('  Cannot match given product id with a Commerce product id.');
    }

    drupal_set_message(t('Invalid product id'));
    drupal_goto(); // frontpage
    return FALSE;
  }

  if ($is_cli) {
    drush_print('  Found a Commerce product ' . $product->product_id . '.');
  }

  // Create new line item based on selected product
  $line_item = commerce_product_line_item_new($product, 1, $order->order_id);

  if ($is_cli) {
    drush_print('  Added product to the cart.');
  }

  // Save the line item to get its ID.
  commerce_line_item_save($line_item);

  // Add the line item to the order using fago's rockin' wrapper.
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  $order_wrapper->commerce_line_items[] = $line_item;

  if ($is_cli) {
    drush_print('Saving order...');
  }

  // Save the order again to update its line item reference field.
  commerce_order_save($order);

  // Redirect to the order's checkout form. Obviously, if this were a
  // form submit handler, you'd just set $form_state['redirect'].

  if ($is_cli) {
    drush_print('Checking out the order...');
  }

  commerce_checkout_complete($order);

  if ($is_cli) {
    drush_print('Marking order as fully paid...');
  }

  $payment_method = commerce_payment_method_instance_load('commerce_payment_example|commerce_payment_commerce_payment_example');

  if (!$payment_method) {
    if ($is_cli) {
      drush_print("  No example payment method found, we can't mark order as fully paid. Please enable commerce_payment_example module to use this feature.");
    }
  }
  else {
    if ($is_cli) {
      drush_print("  Creating example transaction...");
    }

    // Creating new transaction via commerce_payment_example module.
    $charge      = $order->commerce_order_total['und'][0];

    $transaction = commerce_payment_transaction_new('commerce_payment_example', $order->order_id);
    $transaction->instance_id = $payment_method['instance_id'];
    $transaction->amount = $charge['amount'];
    $transaction->currency_code = $charge['currency_code'];
    $transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
    $transaction->message = 'Name: @name';
    $transaction->message_variables = array('@name' => 'Example payment');

    if ($is_cli) {
      drush_print("  Notifying Commerce about new transaction...");
    }

    commerce_payment_transaction_save($transaction);

    commerce_payment_commerce_payment_transaction_insert($transaction);
  }

  if ($is_cli) {
    drush_print("Marking order as completed...");
  }

  commerce_order_status_update($order, 'completed');

  if ($is_cli) {
    drush_print("\nDone.");
  }

Note: As suggested in the comment, if you got error about payment method is unknown while saving the order, make sure you've specified it, e.g.

$order->data['payment_method'] = 'commerce_payment_example|commerce_payment_commerce_payment_‌​example';
commerce_order_save($order); 
4
  • 2
    The Commerce Drush module sounds like an awesome tool. Commented Sep 4, 2014 at 14:28
  • Regarding the manual solution part, there's a problem with the order email notification. The payment method is "unknown" I'm not sure why, I already tested by using the example payment method and is "unknown"
    – mana
    Commented Oct 27, 2017 at 2:37
  • @fkaufusi You'll have to raise the new question then to check what's going on.
    – kenorb
    Commented Oct 27, 2017 at 9:04
  • I now found a solution for the "unknown" payment method on order email. I need to add the payment method to the order before save the order. This will allow the token system to pickup the payment method and use on the order email. $order->data['payment_method'] = 'commerce_payment_example|commerce_payment_commerce_payment_example'; commerce_order_save($order);
    – mana
    Commented Oct 29, 2017 at 23:06
5

This modified script works also for the anonymous users:

<?php
global $user;

$product_id = 2;
// Create the new order in checkout; you might also check first to
// see if your user already has an order to use instead of a new one.
$order = commerce_order_new($user->uid, 'checkout_checkout');
// Save the order to get its ID.
commerce_order_save($order);

// Link anonymous user session to the cart
if (!$user->uid) {
    commerce_cart_order_session_save($order->order_id);
}

// Load whatever product represents the item the customer will be
// paying for and create a line item for it.
$product = commerce_product_load($product_id);
$line_item = commerce_product_line_item_new($product, 1, $order->order_id);

// Save the line item to get its ID.
commerce_line_item_save($line_item);

// Add the line item to the order using fago's rockin' wrapper.
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$order_wrapper->commerce_line_items[] = $line_item;

// Save the order again to update its line item reference field.
commerce_order_save($order);

// Redirect to the order's checkout form. Obviously, if this were a
// form submit handler, you'd just set $form_state['redirect'].
drupal_goto('checkout/' . $order->order_id);
1

You need to add commerce_cart_order_session_save() so anonymous user have their session to link the browser with cart content.

0

1. If a user is not logged into the site they get an access denied message

I got something working but I highly doubt it is best practice.

In the end I cheated. On my form where you put your details including email address I create a user account on the fly and then log the user in. If an email address is all ready in use I log the user in. (I make sure you are not using the admin email address).

As my site only has the donation form page when you hit that page it makes sure you are logged out (if you are not admin). On a successful transaction it logs you out. I have turned off order history / put redirects in place so you can only go on the pages I know about when logged in. No personal details are stored and can't see past donations

In my situation I am happy with how this works. It is not ideal and will only work in a few cases.

2. The checkout process asks for name, address etc.

I went to

/admin/commerce/config/checkout

And disabled

  • Account information
  • Billing information

Your Answer

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

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