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
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 ‘
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.
require_once( ABSPATH . "/wp-load.php" );
require_once( ABSPATH . "/wp-content/plugins/membermouse/includes/mm-constants.php" );
require_once( ABSPATH . "/wp-content/plugins/membermouse/includes/init.php" );
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.
<?php $coupon = new MM_Coupon(); ?>
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.
// Dummy discount code
$coupon_code = '25offsummersale';
// Set the coupon code
$coupon->setCouponCode( $coupon_code );
// Coupon name is usually the same as coupon code
$coupon->setCouponName( $coupon_code );
// Set the coupon type: percentage | dollar | free
$coupon->setCouponType( 'percentage' );
// Amount to take off
$coupon->setCouponValue( '25' );
// Set Quantity to 100 uses
$coupon->setQuantity( 100 );
// Optional Start Date
$coupon->setStartDate( '2015-07-01' );
// Option End Date
$coupon->setEndDate( '2015-07-31' );
// Commit data to the Database
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.
// Set Subscription Options: first | all
$coupon->setRecurringBillingSetting( 'all' );
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.