Case Study - Customizing WooCommerce Subscriptions
We are in the final stages of completing a site for a client and our work on that site involves a good deal of customization and integration of WooCommerce Subscriptions. Additionally, there are several other non-subscription customizations that are included in the plugin that we created for that client in order to accomplish these various tasks.
The main customizations are described below.
Import users and subscription details from a .csv file
The imported .csv file was not originated from a WordPress site but rather an old MySql database for a very old non-WordPress site. That database contained the necessary user information and each user's subscriptions to the old non-WordPress site.
In order to do this, we used a very helpful plugin called Import and Export Users and Customers. It is described as "Clean and easy-to-use import and export users and customer plugin, for WordPress and WooCommerce. It includes custom user meta to be included automatically from a CSV file and delimitation auto-detector." I personally also found it 'easy-to-use' but more importantly for our purposes it contains very useful hooks. In particular, the hook we used allowed us to add functions which imported not only the users but also the old subscriptions and then converted those old subscriptions into the new WooCommerce subscriptions. That hook in 'Import and Export Users and Customers' is post_acui_import_single_user
.
Page access and authorization based on subscription
On the site, subscribers are allowed access to pages and functions, like specialized searches, that non-subscribers are not allowed to access. There are a three separate subscriptions and each of those subscriptions has its own pages. Non-subscribers to a subscription are not allowed access to the subscription's pages. Sort of a membership type setup.
The solution was, first, to create a new admin meta box on the WP admin page editor with the three subscription choices. As in: function add_meta_boxes($post_type, $post) {....}
. That meta box enables an admin/editor to designate a page for one or more of the subscriptions. The meta box data is saved as post metadata for the post and is used as described below.
Then, secondly, we need to check each page visitor and see if they have a subscription matching a subscription allowed on that page. To simplify things - if a page is designated as allowing subscribers for subscription A and the user is a subscriber to A then that user can access the page. This was done by creating a user authorization function that collects the user's subscription(s) and checks the page's metadata against the subscription(s).
Check the number of licenses used for each subscription
Another subscription customization was needed to handle what the site defines as licenses. Basically, a license is a subscription attribute that enables and restricts the number of people that can login as the same user and simultaneously access the site for each purchased subscription. In other words, when a customer purchases a subscription that customer can also add, as a subscription attribute, additional licenses. If more people try to login as the same user with more than the purchased license attributes for that subscription, they are not allow to access the subscription pages. That setup is used on the customer's old site and needed to be similarly set up on the new site.
The way this was done on the new site was by first creating another user authorization function that collects the user's licenses for each subscription. Next each access to the site by the same user is recorded and stored along with the user's subscription. A function then checks each page access to see if more than the licensed number of users is accessing pages set up for a particular subscription.