MemberMouse: Creating a Coupon Programatically

Coming from a WooCommerce background, creating a coupon programatically in MemberMouse is completely different than what I’m used to. In WooCommerce a Coupon is a Post Type of shop_coupon and all you do is insert a new POST with a few required fields and then set it’s different parameters using the update_post_meta() function.

The MemberMouse Way

Creating a Coupon in MemberMouse is a bit different because of it’s Object Orineted Programming style of handling everything. You first need to instantiate a class and then call different functions which build up the Coupon before it’s inserted and the data is commited to the database once and for all.

Where Are Coupon Codes Stored?

Coupon Codes are stored in the mm_coupons table of your database. If you take a quick look at it, it has about 16 fields. MemberMouse has a function for adding or altering the data that goes into each of these fields, for example the coupon_type column which expects a value of ‘percentage‘, ‘dollar‘ or ‘free‘ if you want to give out something for free.

Loading The WordPress and MemberMouse Environment

Let’s take a look at how the process looks for creating a coupon programmatically. The first thing we must do to have access to the MemberMouse PHP programming interface is to load the WordPress and MemberMouse environment by requiring 3 files.

From this point on, we have access to a bunch of MemberMouse classes which allow us to do almost anything with MemberMouse.

Creating a Coupon Programatically

MemberMouse provides us with a bunch of classes which we can use to interact with it. Since we are working with coupons, we are going to be exploring the MM_Coupon class and see how we can use to create a coupon programatically.

From this point on we can call any of the MM_Coupon() methods on $coupon, like in the following example where are are going to add a few of the fields required for the discount code to work properly.

Coupon Subscription Options

Building on the previous example, we can also set the Susbcription Options for the discount to only apply the discount to the first charge of the subscription or all of them.

Conclusion

Comming from a WooCommerce background on creating discount codes and seeing the way MemberMouse creates them was a bit weird at first. But, as you can see from this example, MemberMouse does provide a very simple API to create Coupon Codes programatically.

MemberMouse: Get a List of Core Page Types

Very useful piece of code to get a list of Core Page Types MemberMouse gives you access to.

The Result

  • Member Home
  • Save the Sale
  • Error
  • Login
  • Forgot Password
  • Checkout
  • Confirmation
  • My Account
  • Logout
  • Redeem Gift
  • Reset Password

MemberMouse: Identifying Core Page Types

The more I use MemberMouse the more I come to like it. Below are some very useful snippets of codes for detecing various Core Page Types like the Checkout page, Save The Sale page or even the Confirmation page.

MemberMouse: Checkout Page

The checkout core page is where a user is directed when they click on a purchase link. There is only one default checkout core page on your site which is used by all users regardless of their access rights.

MemberMouse: Redeem Gift Page

The Redeem Gift core page is where a user is directed when a gift membership or bundle has been purchased for them. It functions almost identically to the Checkout core page, except that no billing information is required, as the membership or bundle they are signing up for has already been paid for.

MemberMouse: Member’s Home Page

The member home core page is where a member is directed after they log in.

MemberMouse: Save The Sale Page

The save-the-sale core page is where a user is directed when they’ve clicked on a cancellation link.

MemberMouse: Login Page

The login core page is where a user is directed when they want to log into your site. There is only one login core page on your site which is used by all users regardless of their access rights.

MemberMouse: My Account Page

The my account core page is where a user is directed when they want to view and manage account information such as general account information, billing and shipping information, activate subscriptions and their order history.

MemberMouse: Error Page

The error core page is used to display informative messaging to members when they attempt to interact with pages they do not have access to.

MemberMouse: Confirmation Page

The confirmation core page is where a user is directed after they have made a purchase.