Created
November 29, 2018 08:05
-
-
Save kreamweb/199ebfb3658202cc72c3df59976ab1bb to your computer and use it in GitHub Desktop.
Call URL?points_update=1 to update points
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function ywpar_calculate_rewarded_points( $user_id ){ | |
global $wpdb; | |
$table_name = $wpdb->prefix . 'yith_ywpar_points_log'; | |
$query = "SELECT SUM(ywpar_points.amount) as rewarded_points FROM $table_name as ywpar_points where user_id = $user_id AND action = 'redeemed_points'"; | |
$res = $wpdb->get_row( $query ); | |
return ( empty( $res ) || $res->rewarded_points < 0 ) ? 0 : $res->rewarded_points; | |
} | |
function ywpar_calculate_total_points( $user_id ){ | |
global $wpdb; | |
$table_name = $wpdb->prefix . 'yith_ywpar_points_log'; | |
$query = "SELECT SUM(ywpar_points.amount) as total FROM $table_name as ywpar_points where user_id = $user_id "; | |
$res = $wpdb->get_row( $query ); | |
return ( empty( $res ) || $res->total < 0 ) ? 0 : $res->total; | |
} | |
function ywpar_update_points(){ | |
global $wpdb; | |
$table_name = $wpdb->prefix . 'yith_ywpar_points_log'; | |
$query = "SELECT user_id FROM $table_name as ywpar_points GROUP by user_id "; | |
$res = $wpdb->get_col( $query ); | |
if( $res){ | |
foreach ( $res as $customer_user ){ | |
$rewarded_points = ywpar_calculate_rewarded_points( $customer_user ); | |
$total_points = ywpar_calculate_total_points( $customer_user ); | |
update_user_meta( $customer_user, '_ywpar_user_total_points', $total_points); | |
update_user_meta( $customer_user, '_ywpar_rewarded_points', $rewarded_points); | |
} | |
} | |
} | |
if( isset($_GET['points_update'])){ | |
ywpar_update_points(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment