php 积分和奖励 - 重新计算
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 积分和奖励 - 重新计算相关的知识,希望对你有一定的参考价值。
<?php
if ( isset( $_GET['ywpar_fix_points7'] ) ) {
add_action( 'init', 'start_the_script7' );
function start_the_script7() {
global $wpdb;
$table_name = $wpdb->prefix . 'yith_ywpar_points_log';
$limit = 10;
$users = $wpdb->get_col( "select distinct point.user_id from $table_name as point WHERE NOT EXISTS (
SELECT * FROM " . $wpdb->base_prefix . "usermeta as um
WHERE `meta_key` = '_ywpar_fixed7'
AND um.user_id= point.user_id
) LIMIT $limit" );
if ( $users ) {
foreach ( $users as $user ) {
ywpar_recalculation( $user );
}
wp_safe_redirect( add_query_arg( array( 'users_done' => count( $users ) ) ) );
}
}
}
//New calculation
function ywpar_recalculation( $user_id ){
global $wpdb;
//$user_id = 10511;
error_log( 'user_id: ' . $user_id );
$table_name = $wpdb->prefix . 'yith_ywpar_points_log';
update_user_meta( $user_id,'_ywpar_rewarded_points','');
update_user_meta( $user_id,'_ywpar_used_points','');
$wpdb->query("UPDATE $table_name set `cancelled`='0000-00-00 00:00:00' and user_id={$user_id}");
$wpdb->query( "DELETE from $table_name where action IN ('expired_points', 'admin_action') and user_id={$user_id}" );
$point_earned = array();
$status_earned = array( 'order_completed','reviews_exp','registration_exp','order_refund');
$status_redeemed = array( 'redeemed_points');
$total_points = 0; //total user points
$used_point = 0; //total used point
$redeemed_points = 0; //total redeemed points
$query = "SELECT * FROM $table_name as ywpar_points where user_id = $user_id";
// error_log($query);
$res = $wpdb->get_results( $query );
if ( $res ) {
foreach ( $res as $entry ) {
if ( in_array( $entry->action, $status_earned ) ) {
$point_earned[ $entry->id ] = array(
'amount' => $entry->amount,
'start_amount' => $entry->amount,
'date_earning' => $entry->date_earning
);
$total_points += $entry->amount;
continue;
}
if ( in_array( $entry->action, $status_redeemed ) ) {
$redeemed_points += absint( $entry->amount );
if ( $point_earned ) {
$to_redeemed = absint( $entry->amount );
foreach ( $point_earned as $key => $row ) {
if ( $to_redeemed == 0 ) {
continue;
}
//check we have saturated this row
if ( isset( $row['cancelled'] ) || $row['amount'] == 0 ) {
continue;
}
//check expired
if ( ywpar_are_expired( $row['date_earning'], $entry->date_earning ) ) {
$total_points -= $row['amount'];
$point_earned[ $key ]['cancelled'] = $entry->date_earning;
$point_earned[ $key ]['expired'] = ywpar_get_expired_date( $row['date_earning'] );
continue;
}
$difference = $row['amount'] - absint( $to_redeemed );
if ( $difference <= 0 ) {
// error_log('usati tutti e '.$row['amount']);
$point_earned[ $key ]['cancelled'] = $entry->date_earning;
$used_point += $row['amount'];
$point_earned[ $key ]['amount'] = 0;
$total_points -= $row['amount'];
$to_redeemed -= $row['amount'];
} else {
//error_log('usati solo '. $to_redeemed );
$used_point += absint( $to_redeemed );
$point_earned[ $key ]['amount'] = $difference;
$total_points -= absint( $to_redeemed );
$to_redeemed = 0;
}
}
}
}
}
}
if ( $point_earned ) {
foreach ( $point_earned as $id => $row ) {
if ( ! isset( $row['cancelled'] ) && ! isset( $row['expired'] ) ) {
if ( ywpar_are_expired( $row['date_earning'], date( 'Y-m-d H:i:s' ) ) ) {
$total_points -= $row['amount'];
$row['cancelled'] = ywpar_get_expired_date( $row['date_earning'] );
$row['expired'] = ywpar_get_expired_date( $row['date_earning'] );
}
}
error_log($id);
error_log( print_r( $point_earned[ $id ], true ) );
if ( isset( $row['cancelled'] ) && ! isset( $row['expired'] ) ) {
$q = $wpdb->prepare( "UPDATE $table_name set `cancelled`=%s where id=%d", $row['cancelled'], $id );
$wpdb->query( $q );
}
if ( isset( $row['expired'] ) ) {
$q = $wpdb->prepare( "INSERT INTO $table_name (`user_id`, `action`, `order_id`, `amount`, `date_earning` , `cancelled` , `description` ) VALUES
( %d, 'expired_points', 0, %d, %s, '0000-00-00 00:00:00','recalculated expiration points')", $user_id, - $row['start_amount'], $row['expired'] );
$wpdb->query( $q );
$q = $wpdb->prepare( "UPDATE $table_name set `cancelled`=%s where id=%d", $row['expired'], $id );
$wpdb->query( $q );
}
}
}
error_log('_ywpar_used_points '.$used_point );
error_log('_ywpar_rewarded_points '.$redeemed_points );
error_log('_ywpar_user_total_points '.$total_points );
update_user_meta( $user_id,'_ywpar_used_points',$used_point);
update_user_meta( $user_id,'_ywpar_rewarded_points',$redeemed_points);
update_user_meta( $user_id,'_ywpar_user_total_points',$total_points);
update_user_meta( $user_id, '_ywpar_fixed7', 1 );
}
function ywpar_are_expired( $data_earning, $data_check ) {
$is_expired = false;
if ( get_option( 'ywpar_enable_expiration_point' ) ) {
$num_days = get_option( 'ywpar_days_before_expiration' );
$date1 = new DateTime( $data_earning );
$date2 = new DateTime( $data_check );
$interval = $date1->diff( $date2 );
$is_expired = ( $interval->days > $num_days );
}
return $is_expired;
}
function ywpar_get_expired_date( $data_earning ) {
$expire_date = $data_earning;
if ( get_option( 'ywpar_enable_expiration_point' ) ) {
$num_days = get_option( 'ywpar_days_before_expiration' );
$date1 = new DateTime( $data_earning );
$date1->modify('+'.$num_days.' days');
$expire_date = $date1->format('Y-m-d H:i:s');
}
return $expire_date;
}
以上是关于php 积分和奖励 - 重新计算的主要内容,如果未能解决你的问题,请参考以下文章