Write a functions.php code for WooCommerce (WordPress)

Job ID: 31952771

Budget: $10 – $30 USD

We need a piece of code written that will do the following:

When a product from Category A AND Category B are both added to cart, allow user to purchase another product from category A of equal or lesser value for free.

We just want the code inserted into functions.php, we don't want to install a plugin.
Related categories: PHP WordPress Software Architecture MySQL WooCommerce