I'm trying to create a popup notification on a route that shows the current cart value (subtotal) and notifies users to add $X.XX to receive free shipping.

I have extended a route to add new functionality but I'm unsure whether a hook or service would be best/appropriate. I can't seem to find any documentation on cart value--I can see some functions for retrieving quantity of items and seemingly could calculate the price by retrieving the prices for each individual item but research isn't bringing up much.

I see information regarding Order Totals but one would seemingly have to finish the order process in order to retrieve that data. I'm trying to grab cart value when a user goes to the /cart page.

I'm not asking for anything specific, just a pointer in the right direction. Thank you.

  • you can grab cart quantity & value in a view, display the view in your popup, i sugest you to clone the cart view, disable the original one, then modify the cloned one.
    – pinueve
    Commented Mar 20, 2018 at 20:47
  • Thanks for that comment, I will look into that--did not think to start with that. Thank you!! Edit: nevermind I'm pretty sure you can render a view from code so I don't think that should be an issue.
    – cchoe1
    Commented Mar 20, 2018 at 20:48

1 Answer 1


If you know the store id and order type then

$store_id = 1;
$order_type = 'default';
$cart_manager = \Drupal::service('commerce_cart.cart_manager');
$cart_provider = \Drupal::service('commerce_cart.cart_provider');
$entity_manager = \Drupal::entityManager();
$store = $entity_manager->getStorage('commerce_store')->load($store_id); 
$cart = $cart_provider->getCart($order_type, $store);
  • Why is it important to know the store id? Orders aren't separated by store ID so it seems pointless to require that information...
    – cchoe1
    Commented Mar 21, 2018 at 13:07
  • Every cart/order has a store ID @cchoe1, see drupal.stackexchange.com/questions/255431/…
    – Clive
    Commented Mar 21, 2018 at 13:20

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.