Automatically Add a WooCommerce Product to Cart Based on Category

by | Jan 10, 2018 | WordPress Tweaks

A client of ours was using WooCommerce Subscriptions to create several subscriptions, each with an initial down payment using the built-in subscription sign-up fee. However, he requested that certain subscriptions also include an additional one-time registration fee on top of the down payment. This created a problem. WooCommerce does have an API and plugins for adding fees to orders. However, with a subscription product, these fees are also added to each recurring order of the subscription. What the client really wanted was the ability to have two one-time sign-up fees on a single subscription, one for the initial down payment and one for the registration fee. Unfortunately, this is not currently possible with WooCommerce Subscriptions.

After thinking it over, we came up with a good solution. We could create a simple product called “Registration Fee” and then automatically add this product to the shopping cart whenever a subscription was added to the cart. Using Direct Checkout, we could then take the purchaser directly to the payment page.

With this idea in mind, we set out to create a simple plugin that would add a specified WooCommerce product to the Shopping Cart whenever a product in a specified category was added to the cart. It would need two settings in the WordPress admin area under WooCommerce > Products. First, there would be a select dropdown to choose a product that would automatically be added to the shopping cart. Second, there would be a select dropdown to choose a category that would trigger adding that product to the cart. So whenever a product from the trigger category would be added to the shopping cart, the specified product would also be added to the cart.

We have posted the resulting plugin on GitHub. Feel free to take it for a spin, fork it if you like, and leave a comment with any feedback or ideas for improvements!