myCred WooCommerce Plus

Download Link:

This is a support topic for myCred WooCommerce Plus

The intention of this topic is to give you the opportunity to discuss problems and difficulties with other Festinger Vault customers, e.g. how to address incompatibilities or fix bugs, report malfunctioning features, or get help on how to configure this download.

:warning: You can ask us for update requests here as well! :love_you_gesture:

To request an update of a particular theme or plugin, kindly let us know your preferred version number and we will update it as soon as possible. Although update requests usually do not make much sense because we publish them as soon as they are available and we have the time to upload them.

Please update myCred WooCommerce Plus 1.7.5 to 1.8.1. @Steve

Daily login Rewards, Social Share, Woocommerce plus addons not working because of license error
@Martin @Steve please update

Hello everyone,

I’d like to share a script I’ve developed for creating a reward system in WooCommerce using the MyCred plugin. This script rewards users with points for each purchase, and displays the number of points they would earn or need for each product, cart, and during checkout.
Basically it makes what myCred Woocommerce Plus addon does…
Here’s the code:

Here’s the code:

// Reward users with points for each purchase
function mycred_pro_reward_points_for_purchase( $order_id ) {
    // Get the order
    $order = wc_get_order( $order_id );

    // Get the user
    $user = $order->get_user();

    // If the user is a guest, return early
    if( ! $user ) {
        return;
    }

    // Get the total order cost
    $order_cost = $order->get_total();

    // Calculate points (10 points per $1)
    $points = $order_cost * 10; 

    // Get the myCRED account of the user
    $account = mycred_get_account( $user->ID );

    // If the account was found, add points
    if( $account ) {
        // Assumes that your point type key is "mycred_default"
        $balance = $account->balance->point_types->mycred_default;

        // Add points to the user's account
        $balance->add( $points, 'Reward for purchase', $order_id );
    }
}
add_action( 'woocommerce_order_status_completed', 'mycred_pro_reward_points_for_purchase' );

// Show points to be earned on product page
function mycred_pro_show_points_on_product_page() {
    global $product;

    // Get product price
    $price = $product->get_price();

    // Calculate points to be earned
    $points = $price * 10;

    echo '<p class="points-earned">Purchase this product to earn ' . round($points, 2) . ' points.</p>';
}
add_action( 'woocommerce_single_product_summary', 'mycred_pro_show_points_on_product_page', 25 );

// Show points to be earned and points cost on cart page
function mycred_pro_show_points_and_balance_on_cart() {
    // Get cart total
    $cart_total = WC()->cart->total;

    // Calculate points to be earned and points cost
    $points_earned = $cart_total * 10;
    $points_cost = $cart_total * 30;

    // Get current user and their points balance
    $user_id = get_current_user_id();
    $points_balance = mycred_get_users_balance( $user_id );

    echo '<p class="points-earned">Complete your purchase to earn ' . round($points_earned, 2) . ' points.</p>';
    echo '<p class="points-cost">This cart total is equivalent to ' . round($points_cost, 2) . ' points.</p>';
    echo '<p class="points-balance">Your current balance is ' . round($points_balance, 2) . ' points.</p>';

    // Notify user if they have enough points to cover the purchase
    if ($points_balance < $points_cost) {
        echo '<p>You don\'t have enough points to cover this purchase, but you can still purchase with $.</p>';
    } else {
        echo '<p>Congrats! You have enough points to cover this purchase.</p>';
    }
}
add_action( 'woocommerce_cart_totals_after_order_total', 'mycred_pro_show_points_and_balance_on_cart' );

// Global variable to check if checkout has already run
$checkout_has_run = false;

// Show points cost and points balance on checkout page
function mycred_pro_show_points_and_balance_on_checkout_updated() {
    global $checkout_has_run;

    // If checkout has already run, return
    if($checkout_has_run){
        return;
    }

    // Get cart total
    $cart_total = WC()->cart->total;

    // Calculate points cost
    $points_cost = $cart_total * 10;

    // Get current user and their points balance
    $user_id = get_current_user_id();
    $points_balance = mycred_get_users_balance( $user_id );

    echo '<p class="points-cost">This order total is equivalent to ' . round($points_cost, 2) . ' points.</p>';
    echo '<p class="points-balance">Your current balance is ' . round($points_balance, 2) . ' points.</p>';

    // Notify user if they have enough points to cover the purchase
    if ($points_balance < $points_cost) {
        echo '<p>You don\'t have enough points to cover this purchase, but you can still purchase with $.</p>';
    } else {
        echo '<p>Congrats! You have enough points to cover this purchase.</p>';
    }

    // Set that checkout has run
    $checkout_has_run = true;
}
add_action( 'woocommerce_review_order_after_order_total', 'mycred_pro_show_points_and_balance_on_checkout_updated' );

// Show points to be earned on checkout page
function mycred_pro_show_points_earned_on_checkout() {
    // Get cart total
    $cart_total = WC()->cart->total;

    // Calculate points to be earned
    $points_earned = $cart_total * 10;

    echo '<p class="points-earned">Complete your purchase to earn ' . round($points_earned, 2) . ' points.</p>';
}
add_action( 'woocommerce_review_order_before_order_total', 'mycred_pro_show_points_earned_on_checkout' );

Remember to add this to your functions.php file…

I hope this proves useful! Feel free to modify it as per your needs.

1 Like

Thanks @stinq – much appreciated!