0

I'm developing an eCommerce website using Drupal Commerce. I've been asked from my management to allow several users to use the 'create order' page in the admin store section (orders page) to stimulate sales.

The 'create order' page is sufficient by itself, but adding a product (line item) isn't that friendly, so I need to figure out a way to develop a 'browse products' function that'll allow me to add products to the order I'm creating.

Is there an easy way to do so? Maybe creating an all new 'create order' page? If so, what is the best appraoch without changing the drupal core code?

  • Are you using Drupal version 7 or 8? – Andrew Nim Dec 7 '16 at 20:51
  • am using drupal 7 – ram_griever Dec 8 '16 at 10:42
1

Of course you can create custom order form and page in new custom module. I think you understand drupal 7 FORM API, so you have to create new page with form. You can process form values (i mean in form submit funtion) using entity API.

To create an order for current user:

global $user;

$order = commerce_order_new($user->uid, $status = 'pendig', $type = 'commerce_order');
commerce_order_save($order);
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);

To add billing info:

$profile = new stdClass();
$profile->type = 'billing';
$profile->uid = $user->uid;
$profile->status = '1';

$profile->commerce_customer_address['und'][0]['country'] = $your_country;
$profile->commerce_customer_address['und'][0]['locality'] =$your_locality;
$profile->commerce_customer_address['und'][0]['postal_code'] = $your_postal_code;
$profile->commerce_customer_address['und'][0]['thoroughfare'] = $address;
$profile->commerce_customer_address['und'][0]['first_name'] = $first_name;
$profile->commerce_customer_address['und'][0]['last_name'] = $last_name;

commerce_customer_profile_save($profile);

$order_wrapper->commerce_customer_billing = $profile->profile_id;

Then you can add products to this order:

Presume that $products is an array of arrays of products ids and quantity of this product user is buying (EG.

$products = array(
array('product_id' => 22, 'quantity' => 1),
 array('product_id' => 23, 'quantity' => 3),
);

foreach ($products as $item) {
$product = commerce_product_load($item['product_id']);
$line_item = commerce_product_line_item_new($product, $item['quantity'], $order->order_id);
commerce_line_item_save($line_item);
$order_wrapper->commerce_line_items[] = $line_item;

}
//to save line items and billing info in your order
commerce_order_save($order);
  • Thank you for providing the code, i'll try it and get back to you...cheers – ram_griever Dec 10 '16 at 10:01

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.