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!

3 Comments

  1. Elena

    Worked perfect!

    Reply
  2. Rob Rothschild

    This is great. How could it be modified to auto add the product for each product added? In this case, it is a processing fee for a ticket, and we would like the added product for every ticket in the cart.
    Thanks!

    Reply
    • Robert Staddon

      Hey Rob! You’d want to modify the foreach loop in the handle_add_to_cart() function to count the number of matching products in the trigger category that it found in the cart and then add the corresponding number of “Auto add products”.

      If you’d be interested in hiring our team to make this modification for you, feel free to reach out to us through our Contact page.

      Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Notify me of followup comments via e-mail. You can also subscribe without commenting.