0

Im trying to get the shipping method and the shipping service values after i place an order on a Drupal 8 commerce website so i can use it inside an email im sending the website's owner (not the receipt for the client). I know Swift email would be an option but i have to continue with what i have. So far the owner of the website does receive an email but i need to add more information to it like the shipping method and shipping service.

I've got as far as finding out that $order->shipments->entity will give me, among other values, these 2 fields shipping_method and shipping_service.

Shipment {#4521 ▼
  #values: array:19 [▼
    "shipment_id" => array:1 [▶]
    "type" => array:1 [▶]
    "uuid" => array:1 [▶]
    "order_id" => array:1 [▶]
    "package_type" => array:1 [▶]
    "shipping_method" => array:1 [▼
      "x-default" => "2"
    ]
    "shipping_service" => array:1 [▼
      "x-default" => "PRIORITY_OVERNIGHT"
    ]

Where do I find the values of the shipping method value whose ID is 2 and the shipping service value whose ID is PRIORITY_OVERNIGHT?
Is there an easier way to get the shipping method and service from a placed order?

1 Answer 1

5

Since shipping_method is an entity reference field, you can call $order->shipments->entity->shipping_method->entity, to load the shipping method entity.

The shipping service is not an entity, but just a text field, so you can get the value by using $order->shipments->entity->shipping_service->value.

Hope it helps!

4
  • Thank you @Jdrupal...
    – Nathan
    Jan 3, 2020 at 7:50
  • Hey.... @Jdrupal, how did you know that shipping_method was an entity and shipping_service was not? usually, in devel, if i would see a target_id, i could tell that's an entity but were you able to tell that shipping_method was an entity based on the content of my post, did you look it up or did you just know it?
    – Nathan
    Jan 3, 2020 at 15:46
  • 1
    Hi @Nathan. I have worked with the shipping module before and therefore I knew that shipping_method is an entity :)
    – user72672
    Jan 3, 2020 at 16:17
  • i see... well, thanks for the help!
    – Nathan
    Jan 3, 2020 at 16:18

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.