Skip to content

Instantly share code, notes, and snippets.

@gabrielmerovingi
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielmerovingi/8700088 to your computer and use it in GitHub Desktop.
Save gabrielmerovingi/8700088 to your computer and use it in GitHub Desktop.
Simple WordPress shortcode that allows users to convert their myCRED Points into MarketPress Coupons. Version 1.1 requires myCRED 1.4 or higher.
/**
* Convert myCRED Points into MarketPress Coupon
* @version 1.1
*/
add_shortcode( 'mycred_to_mpress_coupon', 'mycred_pro_render_points_to_marketpress_coupon' );
function mycred_pro_render_points_to_marketpress_coupon( $atts, $content = NULL ) {
// Users must be logged in
if ( ! is_user_logged_in() )
return 'You must be logged in to generate store coupons.';
// myCRED must be enabled
if ( ! function_exists( 'mycred' ) )
return 'myCRED must be enabled to use this shortcode';
extract( shortcode_atts( array(
'exchange' => 1,
'type' => 'mycred_default',
'button_label' => 'Create Coupon'
), $atts ) );
// Load myCRED
$mycred = mycred( $type );
// Prep
$error = $code = false;
$output = '';
$user_id = get_current_user_id();
// No need to show this for excluded users
if ( $mycred->exclude_user( $user_id ) ) return;
$balance = $mycred->get_users_balance( $user_id, $type );
// Form submission
if ( isset( $_POST['mycred_to_mark'] ) && wp_verify_nonce( $_POST['mycred_to_mark']['token'], 'points-to-market-coupon' ) ) {
// Make sure amounts are always positive
$amount = abs( $_POST['mycred_to_mark']['amount'] );
// Exchange
$cost = round( $amount * $exchange );
// Make sure amount is not zero
if ( $amount == $mycred->zero() )
$error = 'Amount can not be zero';
// Make sure user has enough points
if ( $cost > $balance )
$error = 'Insufficient Funds. Please try a lower amount';
// If no errors
if ( $error === false ) {
// In this example coupons are valid for 30 days
// You can change this to a specific date or change
// the number of days. We need a unix timestamp.
$expires = strtotime( '+ 30 days' );
// In this exmaple we will want users to be able to use
// the generated coupon right away but you can change thi
// to any unix timestamp as long as it is befor the expire date.
$start = time();
// Create MarketPress Coupon
$code = wp_generate_password( 12, false, false );
$code = strtoupper( $code );
$coupon = array(
'discount_type' => 'amt',
'discount' => $cost,
'start' => $start,
'end' => $expires,
'uses' => 1
);
// Save
$coupons = get_option( 'mp_coupons', array() );
$coupons[ $code ] = $coupon;
update_option( 'mp_coupons', $coupons );
// Deduct points from user
$mycred->add_creds(
'points_to_coupon',
$user_id,
0-$cost,
'%plural% conversion into store coupon',
$new_coupon_id,
array( 'code' => $code ),
$type
);
$balance = $balance-$cost;
$balance = $mycred->number( $balance );
}
}
// Show users current balance
$output .= '
<p>Your current balance is: ' . $mycred->format_creds( $balance ) . '</p>';
// Error
if ( $error !== false )
$output .= '<p style="color:red;">' . $error . '</p>';
// Success
elseif ( $code !== false )
$output .= '<p>Your coupon code is: <strong>' . $code . '</strong></p>';
// The form for those who have points
if ( $balance > $mycred->zero() )
$output .= '
<form action="" method="post">
<input type="hidden" name="mycred_to_mark[token]" value="' . wp_create_nonce( 'points-to-market-coupon' ) . '" />
<label>Amount</label>
<input type="text" size="5" name="mycred_to_mark[amount]" value="" />
<input type="submit" name="submit" value="' . $button_label . '" />
</form>';
// Not enough points
else
$output .= '<p>Not enough points to create coupons.</p>';
return $output;
}
@digitalredefned
Copy link

Hi Gabriel - Would I have to add the call to the shortcode in the MarketPress Checkout-Edit page itself? If so, would I have to replace this code when I update MarketPress? Thanks!

@gabrielmerovingi
Copy link
Author

Hey. As long as you save this code snippet in your themes functions.php file and use the shortcode in a page content you are fine. If you paste this code in a MarketPress plugin file however then you will lose them when you update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment