I'm trying to implement a shopping cart feature that requires retrieving the data (total value of cart) from a Commerce order. The problem is that this is only possible with a hook. But how am I supposed to pass this data from the hook to a service/controller? I can't find any documentation on where the return value is sent from a hook and if it exists, it's certainly not placed in a very easy to access place since I've been searching for a few days now.

Trying to implement this hook:

function commerce_cart_order_load($uid = 0) {
  // Retrieve the order ID for the specified user's current shopping cart.
  $order_id = commerce_cart_order_id($uid);

  // If a valid cart order ID exists for the user, return it now.
  if (!empty($order_id)) {
    return commerce_order_load($order_id);

  return FALSE;

This should give me an assoc array (or some other data object) of the different elements of an order. Not sure how I can pass this around or access it anywhere.

  • Passing to a service is easy, just literally pass it to the relevant method, e.g. \Drupal::service('my_service')->foo($bar);, where $bar is initialised in the hook. Passing to a controller from a hook doesn't make sense, a controller's job is to prepare the page, it doesn't directly invoke any hooks. If it did, you'd be responsible for passing the data along anyway, so by definition you'd already have it. Maybe if you edit the question and describe exactly what data you need available in the controller, someone will be able to tell you how to get access to it
    – Clive
    Mar 21, 2018 at 13:16
  • I'm trying to extend a contrib module--commerce_cart. I want to load the Total Price of an order (a cart is just a special type of Order). I've successfully done it with the Views module but I have to do some arithmetic before showing it ("Add $X.XX to get free shipping!"). Your example is showing an alternate method of dependency injection, no? I'm trying to get the order details from this hook and display it with a drupal_set_message. I know how to inject services into a controller but that doesn't help me with getting the data from a hook
    – cchoe1
    Mar 21, 2018 at 13:22
  • Like I said, it doesn't make sense to pass data from a hook to a controller, you can't do that. If you need to get the current cart, the linked post I mentioned on your other question a few minutes ago has examples (drupal.stackexchange.com/questions/255431/…). Dependency injection is irrelevant, or at least tangential, to the problem you're trying to solve; don't get hung up on that
    – Clive
    Mar 21, 2018 at 13:30

1 Answer 1


For those who may have the same troubles as I do...

I am trying to set a message in Drupal to show on /cart the main cart page. The data is seemingly already there but it was a bit trickier to actually pull the data into code compared to the Views module just simply inserting a field.

So I can't say I fully understand how this is working. But my site uses a multi-store model so it's necessary to specify store ID. Here is my code:

This isCartNotificationController.php (I changed the route behavior of /cart by setting a default _controller method in RouteSubscriber.php and copy-pasting the CartController.php class from commerce_cart/src/Controllers/CartController.php and making edits. Now the behavior of the route at /cart is different.

$store = \Drupal::service('commerce_store.current_store')->getStore();
$cart = \Drupal::service('commerce_cart.cart_provider')->getCart('default', $store);
$price = $cart->get('total_price')->getValue()[0]['number'];
drupal_set_message($price); //shows $19.98

So it's a rough implementation but I can successfully access the cart value using this. I'll probably make a template or something instead since a drupal_set_message is a little bland.

Lots of Google searches will bring up hooks and extending certain hooks but it doesn't seem like you can practically use those within a service/controller.

In other words, I misunderstood what a hook was doing.

  • You can use services in procedural hooks just like you have there, but you can't go in the other direction (passing data in). \Drupal::service() will instantiate and return whatever object you requested. Hooks are more or less listening for when the system calls for them - in your case, hook_entity_load (hook_order_load) gets called when the entity is loaded: api.drupal.org/api/drupal/…
    – Kevin
    Mar 21, 2018 at 14:12
  • Ah, that triggers memories of reading something about hooks where they were likened to 'event listeners'. So now that makes more sense, thanks for the input, @Kevin
    – cchoe1
    Mar 21, 2018 at 14:21
  • Yeah, I cannot find the link(s), but I think its commonly referred to as observer/visitor pattern.
    – Kevin
    Mar 21, 2018 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.