Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This custom shortcode allows your users to convert their myCRED points into a WooCommerce coupon, in order to give themselves a discount or partial payment on their order. In order for this to work, you need to set an exchange rate between points and your store currency and select. Requires myCRED 1.6+ and WooCommerce 2.4+
/**
* Convert myCRED Points into WooCommerce Coupon
* Requires myCRED 1.4 or higher!
* @version 1.3.1
*/
add_shortcode( 'mycred_to_woo_coupon', 'mycred_pro_render_points_to_coupon' );
function mycred_pro_render_points_to_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,
'minimum' => 0,
'maximum' => 0,
'type' => 'mycred_default',
'button_label' => 'Create Coupon',
'before_tax' => 'yes',
'free_shipping' => 'no'
), $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 $content;
$balance = $mycred->get_users_balance( $user_id );
// Form submission
if ( isset( $_POST['mycred_to_woo'] ) && wp_verify_nonce( $_POST['mycred_to_woo']['token'], 'points-to-woo-coupon' ) ) {
// Make sure amounts are always positive
$amount = abs( $_POST['mycred_to_woo']['amount'] );
// Exchange rate
$value = wc_format_decimal( ( $amount*$exchange ), '' );
// Make sure amount is not zero
if ( $amount == $mycred->zero() )
$error = 'Amount can not be zero';
// If we are enforcing a minimum
if ( $minimum > 0 && $amount < $minimum )
$error = sprintf( 'Amount must be minimum %s', $mycred->format_creds( $minimum ) );
// If we are enforcing a maximum
elseif ( $maximum > 0 && $amount > $maximum )
$error = sprintf( 'Amount can not be higher than %s', $mycred->format_creds( $maximum ) );
// Make sure user has enough points
if ( $amount > $balance )
$error = 'Insufficient Funds. Please try a lower amount';
// If no errors
if ( $error === false ) {
// Create Woo Coupon
$code = strtolower( wp_generate_password( 12, false, false ) );
$new_coupon_id = wp_insert_post( array(
'post_title' => $code,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'shop_coupon'
) );
// Deduct points from user
$mycred->add_creds(
'points_to_coupon',
$user_id,
0-$amount,
'%plural% conversion into store coupon: %post_title%',
$new_coupon_id,
array( 'ref_type' => 'post', 'code' => $code ),
$type
);
$balance = $balance-$amount;
$balance = $mycred->number( $balance );
// Update Coupon details
update_post_meta( $new_coupon_id, 'discount_type', 'fixed_cart' );
update_post_meta( $new_coupon_id, 'coupon_amount', $value );
update_post_meta( $new_coupon_id, 'individual_use', 'no' );
update_post_meta( $new_coupon_id, 'product_ids', '' );
update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
// Make sure you set usage_limit to 1 to prevent duplicate usage!!!
update_post_meta( $new_coupon_id, 'usage_limit', 1 );
update_post_meta( $new_coupon_id, 'usage_limit_per_user', 1 );
update_post_meta( $new_coupon_id, 'limit_usage_to_x_items', '' );
update_post_meta( $new_coupon_id, 'usage_count', '' );
update_post_meta( $new_coupon_id, 'expiry_date', '' );
update_post_meta( $new_coupon_id, 'apply_before_tax', ( in_array( $before_tax, array( 'no', 'yes' ) ) ? $before_tax : 'yes' ) );
update_post_meta( $new_coupon_id, 'free_shipping', ( in_array( $free_shipping, array( 'no', 'yes' ) ) ? $free_shipping : 'no' ) );
update_post_meta( $new_coupon_id, 'product_categories', array() );
update_post_meta( $new_coupon_id, 'exclude_product_categories', array() );
update_post_meta( $new_coupon_id, 'exclude_sale_items', 'no' );
update_post_meta( $new_coupon_id, 'minimum_amount', '' );
update_post_meta( $new_coupon_id, 'customer_email', array() );
}
}
// 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_woo[token]" value="' . wp_create_nonce( 'points-to-woo-coupon' ) . '" />
<label>Amount</label>
<input type="text" size="5" name="mycred_to_woo[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;
}
@advokatb

This comment has been minimized.

Copy link

commented Mar 28, 2015

Where can I set ammount of points that will correspond to coupon value ($)? Thanks

@masstack

This comment has been minimized.

Copy link

commented Sep 23, 2016

After reenter on page, coupon disappear. How to always show generate coupon? Or how send email with coupon to user?

@Gaeius

This comment has been minimized.

Copy link

commented Mar 31, 2017

Whenever converting points to coupon. It submits twice. So it produces 2 same eamount coupons.

Anyone having this issue?

@gabrielmerovingi

This comment has been minimized.

Copy link
Owner Author

commented Apr 25, 2017

Version 1.3.1 has been tested to work with WooCommerce 3.0.+

@Carin

This comment has been minimized.

Copy link

commented Sep 5, 2017

HI, My coupons are removing the entire purchase price, no matter what it is, my ratio is setup in woocommerce $1=.01, and that shows correct in the cart, but the coupons just zero out any total in the cart, thanks, I love this idea can you help, I did not edit anything in your code.

@Carin

This comment has been minimized.

Copy link

commented Sep 5, 2017

I'd be happy to donate but I don't see a paypal button!!

@sadue

This comment has been minimized.

Copy link

commented Sep 19, 2017

Please help me out.
How do I install this addon?
I donloaded the zip file, and went to my plugin intallation page to install it, but got an error message that there is no valid plugin file on the package.

Some help here.

@sadue

This comment has been minimized.

Copy link

commented Sep 20, 2017

@Carin how do you intalled this code, or where do you place the code. Some help @gabrielmerovingi

@reasecret

This comment has been minimized.

Copy link

commented Oct 11, 2017

@sadue add those codes to your theme's functions.php and use [mycred_to_woo_coupon] shortcode where you want.

@reasecret

This comment has been minimized.

Copy link

commented Oct 11, 2017

@gabrielmerovingi when a user remove coupon from payment page price is updating but mycred point isn't. Because coupon is generated already. When a coupon removed, points should be earn again. This would be better I think. And adding an expire date automatically (Like 15 days or 30 days).

EDIT:

I've done this. Automatically adds expiry date to coupons:

Added after $balance = $mycred->number( $balance );:
$today = date('Y-m-d'); $exp_date = date('Y-m-d', strtotime($today. ' + 15 days'));
Change update_post_meta( $new_coupon_id, 'expiry_date', ''); to update_post_meta( $new_coupon_id, 'expiry_date', $exp_date);

@Marinante

This comment has been minimized.

Copy link

commented Dec 10, 2017

Hello everyone who can help me? I would like to add to this code the possibility of restricting the use of the coupon to only those who created it? thanks..

@Momiji0814

This comment has been minimized.

Copy link

commented Jan 19, 2018

When I use coupon at the checkout page, the payment gateway stuck and can't proceed purchase.
Anyone can help me?

@shamsiaidin

This comment has been minimized.

Copy link

commented May 3, 2018

hi
After reenter on page, coupon disappear. How to always show generate coupon? Or how send email with coupon to user?

@trendingconsulting

This comment has been minimized.

Copy link

commented Sep 21, 2018

Hi,

Great script! Does anyone know how to use several point types to exchange Total balance points for coupons?

Thanks!

@panthera26

This comment has been minimized.

Copy link

commented Mar 4, 2019

Bonjour mes shortcode reste entre [ ] pourquoi que dois je faire cordialement

@panthera26

This comment has been minimized.

Copy link

commented Mar 4, 2019

J ai mis le code en functions.php
[mycred_to_woo_coupon] sur ma page
j ai activer payer par mes points
rien ne se passe

@ignitusentertainment

This comment has been minimized.

Copy link

commented Oct 1, 2019

Hi,
This is a great script!

May I know where or how do I set the requirements for user to convert?

  1. limit the number of times to convert, to prevent user separate the cart to make purchase
  2. the promo code to that particular user
  3. set expiry date. and if once expired, the points will return back to the user

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.