title: Uncaught Error: Call to a member function get_cart() on null
body: <p>On Woocommerce, I have an error message on my function but i don't unserstand why it happens.</p>
  
  <p>Uncaught Error: Call to a member function get_cart() on null in ...
  The function checks id a category product is in the cart.</p>
  
  <p>The error is displayed in the order detail in backend</p>
  
  <p>It's used in functions.php , executed on checkout page, 
  called by these hooks: 
  woocommerce_before_order_notes
  woocommerce_checkout_process
  woocommerce_checkout_update_order_meta
  woocommerce_admin_order_data_after_billing_address
  woocommerce_email_order_meta_keys</p>
  
  <p><a href="https://i.stack.imgur.com/v7Atp.png" rel="nofollow noreferrer">https://i.stack.imgur.com/v7Atp.png</a></p>
  
  <pre><code>function is_in_the_cart() {
  
      $found=false;
  
  // Find if product is in the cart price &lt;=40
      foreach( WC()-&gt;cart-&gt;get_cart() as $cart_item_key =&gt; $values )
   {
          $cart_product = $values['data'];
          $price = accessProtected($values['data'], 'changes')['price'];
          if ($price &gt;=40 &amp;&amp; $cart_product-&gt;id=='969') {$found=true;}
    }
  
  
  if ( $found ) {
          return true;
      } else {
          return false;
      }
  }
  
  
  
       add_action('woocommerce_checkout_process', 'wps_select_checkout_field_process');
       function wps_select_checkout_field_process() {
           if (is_in_the_cart())
      {
          global $woocommerce;
  
          // Check if set, if its not set add an error.
          if ($_POST['dvd'] == "blank")
           wc_add_notice( '&lt;strong&gt;Merci de séléctionner un DVD&lt;/strong&gt;', 'error' );
      }
       }
  
       //* Update the order meta with field value
       add_action('woocommerce_checkout_update_order_meta', 'wps_select_checkout_field_update_order_meta');
       function wps_select_checkout_field_update_order_meta( $order_id ) {
           if (is_in_the_cart())
      {
         if ($_POST['dvd']) update_post_meta( $order_id, 'dvd', esc_attr($_POST['dvd']));
      }
       }
  
  
       add_action( 'woocommerce_admin_order_data_after_billing_address', 'wps_select_checkout_field_display_admin_order_meta', 10, 1 );
      function wps_select_checkout_field_display_admin_order_meta($order){
           if (is_in_the_cart())
      {
          echo '&lt;p&gt;&lt;strong&gt;DVD: &lt;/strong&gt; ' . get_post_meta( $order-&gt;id, 'dvd', true ) . '&lt;/p&gt;';
      }
      }
  
      //* Add selection field value to emails
      add_filter('woocommerce_email_order_meta_keys', 'wps_select_order_meta_keys');
      function wps_select_order_meta_keys( $keys ) {
           if (is_in_the_cart())
      {
          $keys['Dvd:'] = 'dvd';
          return $keys;
          }
      }
  
  
  </code></pre>