title: Woocommerce get count of products by category type across all shop pages
body: <p>I am trying to check how many products of a particular product category the client has added to their basket at each iteration (ie added 1 or several, or removed 1 or several). I want to pass this to the Front End and store in localStorage so I can set cookies with it. And I need to be able to check this whenever it changes.</p>
  <p>Initially I tried to do this mapping to a click function for the add to cart button. This won't work as you can click but the add to cart might be unsuccessful (not enough stock, price too low, as I am trying to implement a pay what you want component).</p>
  <p>I've found various ways to try and list all product categories, or retrieve the current item, but cannot find exactly how to do what I want.</p>
  <p>Inside my cart object I first tried:</p>
  <pre class="lang-php prettyprint-override"><code>
  echo $product-&gt;get_categories( ', ', '&lt;span class=&quot;posted_in hidden&quot;&gt;' . _n( '', '', sizeof( get_the_terms( $post-&gt;ID, 'product_cat' ) ), 'woocommerce' ), '&lt;/span&gt;' );
  
  </code></pre>
  <p>But this works only seems to work on product pages.</p>
  <p>Then this which looked promising:</p>
  <pre class="lang-php prettyprint-override"><code>
  wp_list_categories( array('taxonomy' =&gt; 'product_cat', 'title_li'  =&gt; '') );
  
  </code></pre>
  <p>This returns all product categories</p>
  <p>And this:</p>
  <pre class="lang-php prettyprint-override"><code>
  echo wc_get_product_category_list( $cart_item['product_id'] );
  
  </code></pre>
  <p>This seems to retrieve the first (or only one) product category, and it does not return anything  on the cart page.</p>
  <p>Basically I am looking for</p>
  <p>product category: <code>product_a</code>, count: <code>2</code></p>
  <p>Or something very similar to this. My goal is to limit the number of products a user can purchase in one transaction but only for one product category. IE they can purchase unlimited products if not in the restricted category.</p>
  <p>Hope this makes sense.</p>
  <p><strong>UPDATE</strong></p>
  <p>I found <a href="https://www.skyverge.com/blog/checking-woocommerce-cart-contains-product-category/" rel="nofollow noreferrer">this</a> which is <em>almost</em> working</p>
  <pre><code>&lt;?php
      // holds checks for all products in cart to see if they're in our category
      $category_checks = array();
  
      // check each cart item for our category
      foreach ( WC()-&gt;cart-&gt;get_cart() as $cart_item_key =&gt; $cart_item ) {
  
          $product = $cart_item['data'];
          $product_in_cat = false;
  
          // replace 'membership' with your category's slug
          if ( has_term( 'instant-print', 'product_cat', $product-&gt;id ) ) {
              $product_in_cat = true;
          }
  
          array_push( $category_checks, $product_in_cat );
      }
  
      // if all items are in this category, do something
      if ( ! in_array( false, $category_checks, true ) ) {
          echo 'items in category' . count($category_checks);
      } else {
          echo 'No items in category' . count($category_checks);
      }
  ?&gt;
  </code></pre>
  <p>The only issue is it seems the length of the array is for all products in the cart rather than the ones that match my product category 'instant-print'.</p>