Add Item to WooCommerce From Outside Shop

Sometimes I need to setup donations or memberships or some other item that needs to be added to a users cart but NOT be listed in the shop. First thing we should do is make sure to hide these items in the shop. For this to work you will need to set your Product Category. It can be anything you want of course. Then place this code in your functions.php file

Now that these items are hidden on the shop page (you may also find you need to hide them on other pages depending on your setup but just add those pages to the “if” check. Next we need to setup the code to add this to the cart.

Here is the basic code to add this to the cart. I have also setup a check to make sure no other items of the same category are in the cart, and if they are remove them and add this one. Then, if successful, direct the user to the cart page.