WooCommerce Actions and Filters to Manipulate the Cart

Just to be clear, this is the code as it appears in my theme functions file:

    /**
 * Checks the cart to verify whether or not a product from a Category is in the cart
 * @param $category Accepts the Product Category Name, ID, Slug or array of them
 * @return bool
 */
function qualifies_basedon_product_category( $category ) {
    foreach( WC()->cart->cart_contents as $key => $product_in_cart ) {
        if( has_term( $category, 'fee', get_id_from_product( $product_in_cart, false ) ) ) {
            return true;
        }
    }
    // Return false in case anything fails
    return false;
}
 
/**
 * Adds a specific product to the cart
 * @param $product_id Product to be added to the cart
 */
function add_incentive_to_cart( $product_id ) {
    // Check the cart for this product
    $cart_id = WC()->cart->generate_cart_id( $product_id );
    $prod_in_cart = WC()->cart->find_product_in_cart( $cart_id );
    // Add the product only if it's not in the cart already
    if( ! $prod_in_cart ) {
        WC()->cart->add_to_cart( $product_id );
    }
}
 


add_action( 'woocommerce_check_cart_items', 'qualifies_for_incentive' );
function qualifies_for_incentive() {
    // Incentive product we are giving away
    $incentive_product_id = 506;
 
    if( qualifies_basedon_product_category( 'fee' ) ) {
        add_incentive_to_cart( $incentive_product_id );
    } 
}