Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
What's the best way to create orders and line items programmatically? I've looked for some API functions to call to accomplish this, but haven't seen any. Perhaps it's just a matter of creating the proper database entities using the database API? Or is ther a more appropriate way? I intend to do this through the Services module, so there won't be any UI involved.
I will likely also want to add service calls to define products in this way later on, in case this is something to factor in.
-JM
Comments
Comment #1
TheWizz CreditAttribution: TheWizz commentedHmm, answering my own question here, I guess. I now found the commerce_order_new and commerce_line_item_new functions, which I seem to have missed for some reason earlier. Presumably those would do what I want... After some spelunking, I also found the method to add fields to Orders. At first, I expected to find it under "Content types", but eventually found it under Store Configuration, Order Settings, Manage Fields. Somewhat confusing at first, but I guess there's a good reason for keeping this "content type" off to the side in the Store menu.
-JM
Comment #2
pcambraYou should take a look to the tests (commerce_base.test) creates orders and products for testing purposes.
Comment #3
redben CreditAttribution: redben commentedOr the commerce_cart_product_add() on commerce cart module
http://drupalcode.org/project/commerce.git/blob/HEAD:/modules/cart/comme...
Comment #4
TheWizz CreditAttribution: TheWizz commentedThanks for your suggestions. I got things partially working. I first create the order using
The goal of all this is to consolidate orders collected through another system, hence I set the order to already be "complete" to begin with. BTW, should that be "complete" or "completed"? I'm somwhat confused by the wording used in the array returned by commerce_order_commerce_order_state_info(), which I assume is what defines the valid states.
I then move on to creating the line items, as follows:
I need to create line items based on what's received from the other system, hence I'm not creating them based on commerce product entities, but need to provide all details verbatim. I set the name and quantity as shown above, then do the following to add the price to the line item:
Finally I save the complete order using commerce_order_save($order). This basically seems to work. However, when subsequently viewing the order in Commerce, each line item's name is enclosed in parenthesis, and there's no total calculated. I suspect there's some action involved in calculating totals which doesn't happen due to the way I add the line items. Please let me know if there's a better way to accomplish the above, which also performs the Total calculations, and other details that I may be overlooking. For instance, calling commerce_price_field_data_auto_creation() directly feels wrong. I'm sure there's a more "kosher" method for creating a price field that I'm supposed to use here, but I'm quite new to Drupal and haven't fully understood all the new "fields" stuff yet. Any pointers are most welcome!
-JM
Comment #5
TheWizz CreditAttribution: TheWizz commentedComment #6
redben CreditAttribution: redben commentedFirst, if you don't use commerce product for the imported orders you should not be using line items of type product, since these need to reference product on the database. So you might need to create a line item type that does not reference a "commerce product".
For the price calculation, i have no clue !
Comment #7
TheWizz CreditAttribution: TheWizz commentedThanks, redben. Do I need to somehow declare another type of line item, or can I just give it a different name in the 'type' => 'product' entry? Any name? And I think I've figured out why the price calculation doesnät work. I have only the line item's total price specified – I think I need to fill it with its constituent "price components" as well (base price, VAT, etc), which serve as the basis of the order total price calculation, as far as I can tell.
Comment #8
pcambraSome notes:
Comment #9
TheWizz CreditAttribution: TheWizz commentedThanks, pcambra and redben. I've made some progress, and seem to have most things working now, except for the price components. I need to build these from scratch for the line item (since it's not directly based on a commerce product). I've been struggling here for a couple of days. It's a rather complex multi-level structure. What's the best way for adding this info to the line item, given a known base price amount and tax amount?
-JM
PS: Also, any thought on my question #7 above?
Comment #10
redben CreditAttribution: redben commentedIf the question you refer to is how to create line item types, I suggest you have a look at product_reference module and see how it defines product line item type.
Comment #11
TheWizz CreditAttribution: TheWizz commentedThanks for the hint, redben. I believe I managed to add a line item type of my own. I also eventually got the, somewhat complex, price component stuff working, including the various kinds of VAT we have to deal with here.
-JM
Comment #13
tahiticlic CreditAttribution: tahiticlic commented@TheWizz : can you provide your module code please? I've to do the same thing and just looking at product_reference module I don't succeed... Thanks!
Comment #14
TheWizz CreditAttribution: TheWizz commentedHere's my code. It essentially imports orders with line items from another system. The trickiest part was getting the elaborate price structure, with its various components, to work. Perhaps there's a more elegant way, or an API function, that can perform this, rather than hard-coding the logic down to a fairly low level? If anyone knows a better way, please let me know.
-JM
Comment #15
tahiticlic CreditAttribution: tahiticlic commentedThanks, I've done quite the same.
Comment #16
hoZt CreditAttribution: hoZt commentedI have been working with the code in #14 and still can not get it to create my items to come in correctly. None of my field tables like field_data_commerce_line are getting populated so when I view the order it is blank. @TheWizz do you think that you can post the remaining couple of lines before and after this to make your example work?
I have looked hard and can not find any example how to do this. commerce_base.test looks like it creates a cart and uses that for the order. I need to be able to modify the price as you are doing.
It would be good if this could be made a recipe as I am sure others are going to want to do something similar.
Thanks!
Comment #17
TheWizz CreditAttribution: TheWizz commentedPerhaps you're not declaring your line item type (i.e., "myLineItemType" in the snippet above)?
Comment #18
hoZt CreditAttribution: hoZt commentedI did have the correct line type (product) in my case, but still could not get it to work.
With the new RC1 release the cart object has changed so you can now modify the price. Here is a simple example that is working. I just needed to have control of the price when adding items to an order so my case is not as complicated as your VAT example. I am open to suggestions on how to do this better.
Comment #19
aneek CreditAttribution: aneek commentedHello hoZt,
I followed this code snippet and started to work on a custom Module that extends the APIs of DC. Now, the problem I am facing is I have a product of price $10.
I am modifying its price as you mentioned
Now in the cart I am getting the item added as $10. How can I change this one to $100 ? But it changes the quantity. Not the price.
Note that this is a custom module that uses hooks and APIs from DC.
Any help.?
Comment #20
kenorb CreditAttribution: kenorb commentedSee also:
- Commerce Drush module,
- Programmatically creating an order in Drupal Commerce for anonymous users redirecting to payment page
Comment #21
mogtofu33 CreditAttribution: mogtofu33 commentedI found this subject usefull, code by TheWizz is a good start, but
commerce_line_item_save
return a value, so to save an new line_item you need to simply save line item:And you need to set a corresponding line item type from admin/commerce/config/line-items (product, fee, order_discount if module enabled...)
Comment #22
raul_drupal_dev CreditAttribution: raul_drupal_dev as a volunteer commentedWell I think this way is pretty cool! ^^
reference
Comment #23
sumit-k CreditAttribution: sumit-k commentedThanks , #22 worked for apply tax on newly created line item.