0

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.

2
  • 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
    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
    Mar 20, 2018 at 20:48

1 Answer 1

0

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);
$cart->get('total_price')->getValue()[0]['number'];
2
  • 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
    Mar 21, 2018 at 13:07
  • Every cart/order has a store ID @cchoe1, see drupal.stackexchange.com/questions/255431/…
    – Clive
    Mar 21, 2018 at 13:20

Your Answer

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

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