Skip to content
Advertisement

WooCommerce: Automatically change the user role after a certain quantity of completed orders or a minimum amount in one order

I want to change automaticly user role to Premium after

  • 3 completed orders
  • Spending minimum 500 cash in one order

I found on internet this two code snipets :

JavaScript

And this one to access order history:

Source: Checking if customer has already bought something in WooCommerce

JavaScript

How I can tweak them to make it work ? Thank you in advance ! 🙂

Advertisement

Answer

JavaScript

So we get

  • The conditions can be adjusted via the settings
  • A flag is used in the usermeta table, so that the whole code is not unnecessarily rerun if one is already a premium member
  • woocommerce_order_status_completed hook is used – this hook changes a field value after a Woocommerce order is completed
  • Explanation via comments added in the code
JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement