title: How to add a category programmatically to a WooCommerce product?
body: <p>In <code>WooCommerce</code>, if an item is sold out, I would like to add a category &quot;sold out&quot; to it, so that I can show these items in a separate section.</p>
  <p>I created the &quot;sold out&quot; category in the <code>WooCommerce</code>/<code>WordPress</code> Admin panel.</p>
  <p>This is what I have in my <code>functions.php</code>:</p>
  <pre><code>add_action( 'woocommerce_before_shop_loop_item_title', 'mytheme_display_sold_out_loop_woocommerce' );
   
  function mytheme_display_sold_out_loop_woocommerce() {
      global $product;
   
      if ( !$product-&gt;is_in_stock() ) {
         // add category &quot;sold out&quot;   
      }
  }
  </code></pre>
  <p>Now, how can I automatically add the category &quot;sold out&quot; to any sold out <code>WooCommcerce</code> product?</p>