How to get the number of items in a user's cart from commerce? I've been looking into the drupal 8 commerce module, I want to take the variable $count that is generated within 


  ../commerce/modules/cart/src/plugin/block/CartBlock.php


and place it into a custom block in my own theme. I see that the build() function returns the count variable through #count which can then be accessed via twig. Just for reference Drupal commerce currently uses the template:


  ../commerce/modules/cart/templates/commerce-cart-block.html.twig


to output the variables and markup:

&lt;div{{ attributes}}&gt;
  &lt;div class="cart-block--summary"&gt;
    &lt;a class="cart-block--link__expand" href="{{ url }}"&gt;
      &lt;span class="cart-block--summary__icon"&gt;{{ icon }}&lt;/span&gt;
      &lt;span class="cart-block--summary__count"&gt;{{ count_text }}&lt;/span&gt;
    &lt;/a&gt;
  &lt;/div&gt;
  {% if content %}
  &lt;div class="cart-block--contents"&gt;
    &lt;div class="cart-block--contents__inner"&gt;
      &lt;div class="cart-block--contents__items"&gt;
        {{ content }}
      &lt;/div&gt;
      &lt;div class="cart-block--contents__links"&gt;
        {{ links }}
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  {% endif %}
&lt;/div&gt;


My question is, how I can I make that variable global, are there any implications to just making it global, like what if the user updates the basket?

How do I go about achieving this? Does anyone have a possible solution?
you will have to use hook_preprocess_hook to achieve this by passing the variable to the template. 

function template_preprocess_TEMPLATE_NAME(&amp;$variables) {
    $variables['YOUR_VAR_NAME'] = $YOUR_VAR;
}


Then you can access your var in the template like so:

{{ YOUR_VAR_NAME }}