Skip to content

Instantly share code, notes, and snippets.

@gabrielmerovingi
Last active June 28, 2020 15:19

Revisions

  1. gabrielmerovingi revised this gist Apr 25, 2017. No changes.
  2. gabrielmerovingi revised this gist Apr 25, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions mycred-woo-coupon
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    /**
    * Convert myCRED Points into WooCommerce Coupon
    * Requires myCRED 1.4 or higher!
    * @version 1.3
    * @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 ) {
    @@ -44,7 +44,7 @@ function mycred_pro_render_points_to_coupon( $atts, $content = NULL ) {
    $amount = abs( $_POST['mycred_to_woo']['amount'] );

    // Exchange rate
    $value = $mycred->number( $amount*$exchange );
    $value = wc_format_decimal( ( $amount*$exchange ), '' );

    // Make sure amount is not zero
    if ( $amount == $mycred->zero() )
  3. gabrielmerovingi revised this gist Apr 24, 2017. 1 changed file with 24 additions and 10 deletions.
    34 changes: 24 additions & 10 deletions mycred-woo-coupon
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,11 @@
    /**
    * Convert myCRED Points into WooCommerce Coupon
    * Requires myCRED 1.4 or higher!
    * @version 1.2.2
    * @version 1.3
    */
    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.';
    @@ -14,21 +15,25 @@ function mycred_pro_render_points_to_coupon( $atts, $content = NULL ) {
    return 'myCRED must be enabled to use this shortcode';

    extract( shortcode_atts( array(
    'exchange' => 1,
    'type' => 'mycred_default',
    'button_label' => 'Create Coupon'
    '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 = '';
    $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;
    if ( $mycred->exclude_user( $user_id ) ) return $content;

    $balance = $mycred->get_users_balance( $user_id );

    @@ -39,12 +44,20 @@ function mycred_pro_render_points_to_coupon( $atts, $content = NULL ) {
    $amount = abs( $_POST['mycred_to_woo']['amount'] );

    // Exchange rate
    $value = $mycred->number( $amount*$exchange );
    $value = $mycred->number( $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';
    @@ -89,8 +102,8 @@ function mycred_pro_render_points_to_coupon( $atts, $content = NULL ) {
    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', 'yes' );
    update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
    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' );
    @@ -127,4 +140,5 @@ function mycred_pro_render_points_to_coupon( $atts, $content = NULL ) {
    $output .= '<p>Not enough points to create coupons.</p>';

    return $output;

    }
  4. gabrielmerovingi revised this gist Feb 14, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions mycred-woo-coupon
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    /**
    * Convert myCRED Points into WooCommerce Coupon
    * Requires myCRED 1.4 or higher!
    * @version 1.2.1
    * @version 1.2.2
    */
    add_shortcode( 'mycred_to_woo_coupon', 'mycred_pro_render_points_to_coupon' );
    function mycred_pro_render_points_to_coupon( $atts, $content = NULL ) {
    @@ -53,7 +53,7 @@ function mycred_pro_render_points_to_coupon( $atts, $content = NULL ) {
    if ( $error === false ) {

    // Create Woo Coupon
    $code = wp_generate_password( 12, false, false );
    $code = strtolower( wp_generate_password( 12, false, false ) );
    $new_coupon_id = wp_insert_post( array(
    'post_title' => $code,
    'post_content' => '',
  5. gabrielmerovingi revised this gist Feb 11, 2015. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions mycred-woo-coupon
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    /**
    * Convert myCRED Points into WooCommerce Coupon
    * @version 1.2
    * Requires myCRED 1.4 or higher!
    * @version 1.2.1
    */
    add_shortcode( 'mycred_to_woo_coupon', 'mycred_pro_render_points_to_coupon' );
    function mycred_pro_render_points_to_coupon( $atts, $content = NULL ) {
    @@ -9,7 +10,7 @@ function mycred_pro_render_points_to_coupon( $atts, $content = NULL ) {
    return 'You must be logged in to generate store coupons.';

    // myCRED must be enabled
    if ( ! function_exists( 'mycred_get_settings' ) )
    if ( ! function_exists( 'mycred' ) )
    return 'myCRED must be enabled to use this shortcode';

    extract( shortcode_atts( array(
  6. gabrielmerovingi revised this gist Oct 2, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mycred-woo-coupon
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    /**
    * Convert myCRED Points into WooCommerce Coupon
    * @version 1.1
    * @version 1.2
    */
    add_shortcode( 'mycred_to_woo_coupon', 'mycred_pro_render_points_to_coupon' );
    function mycred_pro_render_points_to_coupon( $atts, $content = NULL ) {
  7. gabrielmerovingi revised this gist Oct 2, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions mycred-woo-coupon
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ function mycred_pro_render_points_to_coupon( $atts, $content = NULL ) {
    $amount = abs( $_POST['mycred_to_woo']['amount'] );

    // Exchange rate
    $amount = $mycred->number( $amount*$exchange );
    $value = $mycred->number( $amount*$exchange );

    // Make sure amount is not zero
    if ( $amount == $mycred->zero() )
    @@ -77,7 +77,7 @@ function mycred_pro_render_points_to_coupon( $atts, $content = NULL ) {

    // Update Coupon details
    update_post_meta( $new_coupon_id, 'discount_type', 'fixed_cart' );
    update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
    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', '' );
  8. gabrielmerovingi renamed this gist Jul 9, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion myCRED to WooCommerce Coupon → mycred-woo-coupon
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    /**
    * Convert myCRED Points into WooCommerce Coupon
    * @version 1.0
    * @version 1.1
    */
    add_shortcode( 'mycred_to_woo_coupon', 'mycred_pro_render_points_to_coupon' );
    function mycred_pro_render_points_to_coupon( $atts, $content = NULL ) {
  9. gabrielmerovingi revised this gist Jul 9, 2014. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions myCRED to WooCommerce Coupon
    Original file line number Diff line number Diff line change
    @@ -14,11 +14,12 @@ function mycred_pro_render_points_to_coupon( $atts, $content = NULL ) {

    extract( shortcode_atts( array(
    'exchange' => 1,
    'type' => 'mycred_default',
    'button_label' => 'Create Coupon'
    ), $atts ) );

    // Load myCRED
    $mycred = mycred_get_settings();
    $mycred = mycred( $type );

    // Prep
    $error = $code = false;
    @@ -67,7 +68,8 @@ function mycred_pro_render_points_to_coupon( $atts, $content = NULL ) {
    0-$amount,
    '%plural% conversion into store coupon: %post_title%',
    $new_coupon_id,
    array( 'ref_type' => 'post', 'code' => $code )
    array( 'ref_type' => 'post', 'code' => $code ),
    $type
    );

    $balance = $balance-$amount;
  10. gabrielmerovingi revised this gist Apr 2, 2014. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions myCRED to WooCommerce Coupon
    Original file line number Diff line number Diff line change
    @@ -82,9 +82,17 @@ function mycred_pro_render_points_to_coupon( $atts, $content = NULL ) {

    // 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', 'yes' );
    update_post_meta( $new_coupon_id, '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() );
    }

    }
  11. gabrielmerovingi created this gist Jan 30, 2014.
    119 changes: 119 additions & 0 deletions myCRED to WooCommerce Coupon
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,119 @@
    /**
    * Convert myCRED Points into WooCommerce Coupon
    * @version 1.0
    */
    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_get_settings' ) )
    return 'myCRED must be enabled to use this shortcode';

    extract( shortcode_atts( array(
    'exchange' => 1,
    'button_label' => 'Create Coupon'
    ), $atts ) );

    // Load myCRED
    $mycred = mycred_get_settings();

    // 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 );

    // 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
    $amount = $mycred->number( $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 ( $amount > $balance )
    $error = 'Insufficient Funds. Please try a lower amount';

    // If no errors
    if ( $error === false ) {

    // Create Woo Coupon
    $code = 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 )
    );

    $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', $amount );
    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, 'expiry_date', '' );
    update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
    update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
    }

    }

    // 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;
    }