I create the order with some line items in my module, and need to add shipping service to it. But I can't find any example how to do this, so I try with my hands.
I use function
commerce_shipping_service_rate_order('shipping_machine_name1', $order);
This function creates $order->shipping_rates array with 'shipping_machine_name1' item and correct commerce_unit_price and other fields.
After this I do the commerce_order_save($order);
Order is correctly saved, but I didn't see the shipping service in line items. What I do wrong?
Maybe I need to do additional steps for correctly attach shipping to order?
Comments
Comment #1
googletorp commentedIf you don't mind to get your hands dirty, see
commerce_shipping_line_item_add_form
and
commerce_shipping_line_item_add_form_submit
That will show you how to create a shipping line item, then all you need to do after it has been saved is to add it to the order.
Comment #2
murzI look at this functions, but don't understand what I must do. Main function, as I understand, is:
commerce_shipping_line_item_populate($line_item, $service, $unit_price);But where is place when line_item is attached to order, I don't understand.
Maybe you can write the simple example?
I have an $order object and know shipping item machine name $shipping_service. I want to add this shipping service as line item into order. Shipping service is flat_rate, so it have static price. Can you write code example that do this?
Comment #4
Tapendra Singh commentedSteps:
1) Create a service by using Flat Rate Module.
2) By using this code whichever shipping service is applcable will come with order object.
3) User service machine name and then call yourfunction_name($order, $service_name = NULL);
Comment #5
hiramanpatil@TapendraSingh
I am using this function and called it in hook_order_presave() but the page show error. This gives below error message in error logs.
DatabaseTransactionOutOfOrderException: in DatabaseConnection->rollback() (line 1084 of /var/www/html/drupal-project/includes/database/database.inc).Thanks,
Comment #6
finex commentedSame problem here: the presave() is inside a db transition, so it can't be used to change data on the order lines. We have to call the code before or after the presave().
EDIT: solved using hook_commerce_cart_order_refresh().