Skip to content

Instantly share code, notes, and snippets.

@andrewlimaza
Created March 14, 2019 08:21
Show Gist options
  • Save andrewlimaza/be46df94544dd909fc4ae893778521ed to your computer and use it in GitHub Desktop.
Save andrewlimaza/be46df94544dd909fc4ae893778521ed to your computer and use it in GitHub Desktop.
Generate a discount code for Paid Memberships Pro
<?php
/**
* Generate a discount code for Paid Memberships Pro. Call this function whenever you'd like to generate a new discount code.
* Adjust the code accordingly for discount uses and level settings for the discount code.
* Add this code to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*
* www.paidmembershipspro.com
*/
function my_generate_pmpro_discount_code() {
global $wpdb;
$discount_code = array(
"code" => pmpro_getDiscountCode(),
"starts" => '2019-01-01',
"expires" => '2020-12-31',
"uses" => 5
);
// create the discount code first.
$wpdb->insert(
$wpdb->pmpro_discount_codes,
array(
'code' => $discount_code['code'],
'starts' => $discount_code['starts'],
'expires' => $discount_code['expires'],
'uses' => $discount_code['uses']
),
array(
'%s',
'%s',
'%s',
'%d'
)
);
// Setup level data for the discount code.
$level = array(
"level_id" => 2,
"initial_payment" => 0,
"billing_amount" => 0,
"cycle_number" => 0,
"cycle_period" => '',
"billing_limit" => '',
"trial_amount" => '',
"trial_limit" => '',
'expiration_number' => '',
'expiration_period' => ''
);
// Get the code ID
$SQL = "SELECT id from $wpdb->pmpro_discount_codes WHERE `code` = '" . esc_sql( $discount_code['code'] ) . "'";
$code_id = $wpdb->get_var( $SQL );
// Assign the discount code to a level etc.
$wpdb->insert(
$wpdb->pmpro_discount_codes_levels,
array(
'code_id' => $code_id,
'level_id' => $level['level_id'],
'initial_payment' => $level['initial_payment'],
'billing_amount' => $level['billing_amount'],
'cycle_number' => $level['cycle_number'],
'cycle_period' => $level['cycle_period'],
'billing_limit' => $level['billing_limit'],
'trial_amount' => $level['trial_amount'],
'trial_limit' => $level['trial_limit'],
'expiration_number' => $level['expiration_number'],
'expiration_period' => $level['expiration_period']
),
array(
'%d',
'%d',
'%f',
'%f',
'%d',
'%s',
'%d',
'%f',
'%d',
'%d',
'%s'
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment