Discussions

Get shipment services object (flat rate) from order object

Hello
I have an order object with line items, shipping profiles and so on. I want to get current shipment service object (SSO) from my order object. I'm talking about flat rate price for order shipment.

I can get SSO from $order->commerce_line_items, but I have to run through all line items until I find SSO.

So, my question: what is the proper way for getting attached line_item shipment service?

Thanks in advance!

Update:
Found a way to direct mysql query into database, just like this:
select * from {commerce_line_item} where order_id = 1 AND type = 'shipping';
But i think there must be a better way

P.S. Sorry for my great English.

Posted: May 25, 2013

Comments

Cyclodex on November 29, 2013

I tried something similar, and got to the following solution, perhaps it will help or inspire you :)

// Be sure there are line_items
if (!empty($order->commerce_line_items["und"])){
  $line_items = array();
  // Gather together the IDs of the line items
  foreach($order->commerce_line_items["und"] AS $line_item){
    $line_items[] = $line_item["line_item_id"];
  }

  // load_multiple allows us to use $conditions so we only load the entity of type "shipping"
  $shipping_line_item = commerce_line_item_load_multiple($line_items, array('type' => "shipping"), FALSE);
}

So you will have only the shipping entities, and could filter probably even more. Check out the function from commerce_line_item.module!
commerce_line_item_load_multiple()
Cheers