WooCommerce Actions and Filters to Manipulate the Cart

Hi there,
I want to say thank you so much for posting this! I feel I’ve been looking everywhere for this explanation as I am trying to add a specific product to a cart if the cart contains items from a certain category.
I have tried pasting the code into my theme’s function.php file, but with no luck so far. I think I am missing some fundamental step…like how to add the parameters for the category required and the product id that will be added to the cart.
I am trying to check the cart for any items from category ID= 21, then if any items in the cart are from that category, add product ID= 506

I think I am missing something here, because I plut the code into the theme-functions file, then when I try to test, I can’t actually see what’s in my cart on the cart or checkout page anymore. The last line of the source code for the page says <div="woocommerce"> then it’s blank with no content or footer or anything.

Any ideas where I am going wrong? I really need to get this working asap, and this is the closest thing I have found that may (hopefully) work!

Thank you so, so much for your time!!

Danielle