Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save messica/73e6a43e8321235d0ddb7d2967409ab0 to your computer and use it in GitHub Desktop.
Save messica/73e6a43e8321235d0ddb7d2967409ab0 to your computer and use it in GitHub Desktop.
Generate a discount code for Paid Memberships Pro (with Subscription Delay)
<?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' => '',
'subscription_delay' => '2020-01-01'
);
// 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'
)
);
// Only continue if Subscription Delays Add On is activated.
if ( ! function_exists( 'pmpro_saveDCSDs' ) ) {
return;
}
// Add discount code subscription delay.
$delays = array( $level['level_id'] => $level['subscription_delay'] );
pmpro_saveDCSDs( $code_id, $delays );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment