Create a custom WP Symposium Pro reward

WP Symposium rewards (and badges) can be used for the “actions” that come ready with the Extensions plugin. However, you can create your own rewards that work with other plugins, like WooCommerce.

This article shows how to create a reward, with a badge, that is rewarded when (as an example) a user’s WooCommerce order is marked as completed.

The way it does it can be used with any WordPress plugin that provides a hook into the action that you want to reward. If you are not familiar with WordPress hooks, you should probably do a little research first, Google for “WordPress hooks” and you’ll find a wealth of information, probably starting with theWordPress codex pages.

If you’re not familiar, you may still find this article useful, taking for granted how it works!

WooCommerce hook

As we are going to reward users when their WooCommerce order is marked as completed, the WooCommerce hook we will use is “woocommerce_order_status_completed”.

Creating your reward type

The best and tidiest way to create your reward type, and apply it when the action occurs, is as a simple plugin. You can download the plugin file referred to here – it’s a ZIP file, so extract the file before using FTP to copy it to your site. It’s very small, basically the following code plus the plugin header.

Just upload it to your wp-content/plugins folder, it will appear as a plugin in your admin dashboard where you can activate it.

Download the complete plugin file (extract the ZIP file) and take a look (edit with any text editor) when reading the following. There are two main sections of code.

Section 1: Adding your reward type

The first section in the code adds your new reward type to the admin screens under WPS Pro->Rewards (and user rewards).

Three actions are added.

  • The first adds the reward type when adding a new reward
  • The second adds the reward type when editing a reward given to a user
  • The third adds the reward type when doing a “count of” reward

The value of the drop-down list option in each function is important, this is your reward “type”. Keep a note of this for section 2 below.

Here’s the three actions:

// Add reward type to admin
add_action( 'rewards_info_box_content_options_filter', 'wps_apply_woocommerce_completed_reward' , 10, 1);
add_action( 'reward_info_box_content_options_filter', 'wps_apply_woocommerce_completed_reward' , 10, 1);
function wps_apply_woocommerce_completed_reward($the_post_id) {
 echo '<option value="woocommerce_completed"';
 if (get_post_meta($the_post_id, 'wps_rewards_type', true) == 'woocommerce_completed') echo ' SELECTED';
 echo '>Completed WooCommerce order</option>';
}
// Add count of reward type to admin
add_action( 'rewards_info_box_content_options_count_filter', 'wps_apply_woocommerce_completed_reward_count_type' , 10, 1);
function wps_apply_woocommerce_completed_reward_count_type($the_post_id) {
 echo '<option value="woocommerce_completed"';
 if (get_post_meta($the_post_id, 'wps_rewards_count_type', true) == 'woocommerce_completed') echo ' SELECTED';
 echo '>Completed WooCommerce order</option>';
}

Section 2: Apply the reward

The second section of code is triggered through the WooCommerce hook mentioned above. When the order is marked as completed, the function is run.

The function takes the order ID, and gets the user who placed the order, for point 2 below.

With the user details obtained, a reward is generated with the wps_insert_reward() function, passing an array:

  1. type = the reward type from above (where I say keep a note!)
  2. title = the title of the reward, this is useful if set to the user’s display name for searching via admin
  3. content = this is a note of what’s been awarded
  4. post_author = the user ID of the user who placed with order, and who will get the reward

Here’s the code:

add_action( 'woocommerce_order_status_completed', 'wps_apply_woocommerce_completed_rewards' );
function wps_apply_woocommerce_completed_rewards($order_id) {
 // order object
 $order = new WC_Order( $order_id );
 // reward user (if user exists)
 $user_id = $order->user_id;
 if ($user_id):
 $user = get_user_by('id', $user_id);
 $reward = array(
 'type' => 'woocommerce_completed', // matches value of option in section 1
 'title' => $user->display_name, // show in reward title (good to be able to search on in admin)
 'content' => 'Reward for a completed WooCommerce order', // shown in reward content as a note
 'post_author' => $user_id
 );
 wps_insert_reward($reward);
 endif;
}

Using the new reward

You can now go to WPS Pro->Rewards and add a new reward. The reward is created in the same way as usual, but you’ll have a new option in the drop-down list in the Reward Details box (part of section 1 code above). Include a featured image if you want to show a badge with the [wps-badge] shortcode.

Next time you mark a WooCommerce order as completed the code in section 2 above will be applied, and the user will be awarded the points you’ve set.

Probably the most important thing to remember is that the value in section 1 must match the type in section2.

Sidebar