Experiment creating virtual coupons for WooCommerce. This is a proof of concept that will be developed to verify a coupon code with an external source.
A few weeks ago I got a call from a guy I spoke with at WordCamp Dublin. I suppressed my shyness to talk to someone, Mark Davenport, during a lunch break. He rang me to ask me about using external coupons with WooCommerce.
Mark had a client that ran a Pigsback/Groupon promotional campaign. He explained that Pigsback/Groupon provided him with a number of promotional codes that customers would receive when they paid for the promotion at the Pigsback/Groupon website. Customers then had to go to his client’s site to use the code to receive the actual product. He wondered how best to handle this in a robust manner that did not impact customers.
He reviewed numerous coupon plugins. While they were great at generating coupon codes but none could handle verifying the already created codes. Knowing how extensible WooCommerce is, I knew that there must be a way to allow the use of coupons created outside of WooCommerce.
Begin the search
I started out at the front end and looked at the code that is run when the ‘Apply Coupon‘ button is clicked on the cart page but didn’t get very far.
I soon found a post to Create WooCommerce coupons programmatically. It uses the ‘ woocommerce_get_shop_coupon_data‘ filter that “allows custom coupon objects to be created on the fly” (according to the comment in the WooCommerce code). It was a great starting point.
This code in the post created a coupon in the database if it didn’t exist. I didn’t want this part so I commented it out.
I noticed that the function was being called 8 times when the coupon was applied and 4 times when it was removed. While this isn’t a problem for the current code, it would have a terrible performance impact if the filter function did a database query or, worse still, called an external API. The code I am using is a proof of concept but production code will have to take this into account.
In a StackOverflow thread a user was doing something similar to Mark and asked why the filter code was being called 3 times on each page load. One of the replies mentioned the ‘woocommerce_applied_coupon‘ and ‘woocommerce_removed_coupon‘ actions. While these actions are called too late to validate a coupon, they will allow for a coupon usage counter to be incremented or decremented.
Furthermore, the code below does not limit the products or categories that it can be applied to. These fields must be set before use in production.
The first thing that I will do it convert the code into a class. This will allow for a static variable to store whether the coupon code is valid after initialising it once. Mark aims to store the codes provided by Pigsback/Groupon in a custom database table (with a column to indicate whether the code has been used) and this table would be queried. It will be much more efficient to do one database query and store the result for the next 7 times the function is called.
The ‘woocommerce_applied_coupon‘ and ‘woocommerce_removed_coupon‘ actions will be used to mark the code as used and available respectively. I will likely use a INT (or TINYINT) field and increment and decrement the value.