title: Add fee to woocommerce based on product category
body: <p>So I have a store with a category that uses the "virtual" product type in order to avoid shipping. However, there is a service fee that needs to be added to each order that includes a product of that category. </p>
  
  <p>Through various searches I have found different ways to globally add a fee, or to add a fee based on quantity, and have tried to use that to target via category, but cant seem to get it right. Any help would be appreciated.</p>
  
  <pre><code>function woo_add_cart_fee() {
  global $woocommerce;
  //Getting Cart Contents. 
  $cart = $woocommerce-&gt;cart-&gt;get_cart();
  //find product category
  foreach ( $woocommerce-&gt;cart-&gt;get_cart() as $cart_item_key =&gt; $values ) {
      $_product = $values['data'];
          $terms = get_the_terms( $_product-&gt;id, 'product_cat' );
      }
    if ($_categoryid == 23){
        $excost = 6;
    } 
    elseif ($_categoryid == 14){
        $excost = 0;
    }     
  
  $woocommerce-&gt;cart-&gt;add_fee('Service Charge', $excost, $taxable = false, $tax_class = '');
  }   
  add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
  </code></pre>
  
  <hr>
  
  <p>Okay, so because this was my first post, I can't answer my own question for awhile. I finally got this to work with the following snippet, but feel free to improve if you find issues:</p>
  
  <pre><code>// Add Service Fee to Category
  function woo_add_cart_fee() {
  
  $category_ID = '23';
  global $woocommerce;
  
  foreach ($woocommerce-&gt;cart-&gt;cart_contents as $key =&gt; $values ) {
      // Get the terms, i.e. category list using the ID of the product
  $terms = get_the_terms( $values['product_id'], 'product_cat' );
      // Because a product can have multiple categories, we need to iterate through the list of the products category for a match
      foreach ($terms as $term) {
          // 23 is the ID of the category for which we want to remove the payment gateway
          if($term-&gt;term_id == $category_ID){
           $excost = 6;
           }
           }
          $woocommerce-&gt;cart-&gt;add_fee('Service Charge', $excost, $taxable = false, $tax_class = '');
  }
  }
  add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
  </code></pre>
  
  <p>You would obviously change the category ID and the cost to whatever you wanted.</p>