Tag Archives: Points and Rewards

Customize WooCommerce Points and Rewards

WooCommerce Points and Rewards is a fantastic extension from WC team that lets us setup reward points for customers. By default, it can be configured for users to earn points for certain actions like “account signup” & “writing a review”. My requirement was to add another action called “Points earned for first login per day”. I will share the code along with relevant comments and screenshots to showcase the implementation.
Add the below filters and actions in functions.php file of your WP theme (Child themes recommended).
Ref: API Docs for WC Points and Rewards
//Filter to add the option of points for login per day
add_filter( 'wc_points_rewards_action_settings', 'points_rewards_for_login_per_day' );
function points_rewards_for_login_per_day( $settings ) {
$settings[] = array(
'title' => __( 'Points earned for first login per day' ),
'desc_tip' => __( 'Enter the amount of points earned when a customer logs in for the first time each day.' ),
'id' => 'wc_points_rewards_login_per_day',
);
return $settings;
}
//Filter to add events description for Points Log tab
add_filter( 'wc_points_rewards_event_description', 'points_rewards_for_login_per_day_event_description', 10, 3 );
function points_rewards_for_login_per_day_event_description( $event_description, $event_type, $event ) {
$points_label = get_option( 'wc_points_rewards_points_label' );
switch ( $event_type ) {
case 'wc-add-points-login-per-day': $event_description = sprintf( __( '%s earned for first login of the day' ), $points_label );
break;
}
return $event_description;
}
//Action to capture the log in event. My requirement is to register the first log in of the day
function func_on_login($user_login, $user) {
$points = get_option( 'wc_points_rewards_login_per_day' ); //get points configured during rewards setup in admin
$lastlogindatetime = get_user_meta( $user->ID, 'last_login_time', true ); //I am persisting the value of first log in of the day in a user meta variable
if ( $lastlogindatetime != '' && (date( 'Y-m-d', time() ) != date( 'Y-m-d', $lastlogindatetime )) ) {
if ( ! empty( $points ) ) {
WC_Points_Rewards_Manager::increase_points( $user->ID, $points, 'wc-add-points-login-per-day' );
}
update_user_meta( $user->ID, 'last_login_time', time() );
} else if ($lastlogindatetime == '') {
if ( ! empty( $points ) ) {
WC_Points_Rewards_Manager::increase_points( $user->ID, $points, 'wc-add-points-login-per-day' ); //increase point on first log in of the day
}
add_user_meta( $user->ID, 'last_login_time', time() );
}
}
add_action('wp_login', 'func_on_login', 10, 2);

Also attached are two screenshots corresponding to the first two filters, one for action settings and the other for Points Log tab. The results are highlighted in boxes.WC Action settingsWC Points Log tab
Hope this helps! Please feel free to share your experiences with WooCommerce customization and if there is a better way to achieve the same results as described in this post.