Untitled
unknown
plain_text
a year ago
2.5 kB
59
Indexable
Never
$current_month = date('mY'); $current_year = date('Y'); $user_id = get_current_user_id(); // check if it's already been applied this month if (get_user_meta($user_id,'b2bking_monthly_rules_calculated_'.$current_month, true) !== 'yes'){ // Get all monthly group rules // get all group rules $group_rules = get_posts([ 'post_type' => 'b2bking_grule', 'post_status' => 'publish', 'numberposts' => -1, 'fields' => 'ids', ]); $monthly_rules = array(); foreach ($group_rules as $grule_id){ $type = get_post_meta($grule_id,'b2bking_rule_applies', true); if ($type === 'order_value_monthly'){ array_push($monthly_rules, $grule_id); } } $year = date('Y', strtotime(date('Y-m')." -1 month")); $month = date('m', strtotime(date('Y-m')." -1 month")); $days_in_month = date('t', strtotime(date('Y-m')." -1 month")); // get customer's monthly spend $args = array( 'limit' => '-1', 'customer_id' => $user_id, 'date_created' => $year.'-'.$month.'-01...'.$year.'-'.$month.'-'.$days_in_month, ); $orders = wc_get_orders( $args ); $monthly_spent = 0; foreach ($orders as $order){ $monthly_spent += $order->get_total(); } $is_b2b = get_user_meta($user_id,'b2bking_b2buser', true); if ($is_b2b === 'yes'){ $user_group = get_user_meta($user_id,'b2bking_customergroup', true); // check rules foreach ($monthly_rules as $grule_id){ $group1 = explode('_',get_post_meta($grule_id,'b2bking_rule_agents_who', true))[1]; $group2 = explode('_',get_post_meta($grule_id,'b2bking_rule_who', true))[1]; $type = get_post_meta($grule_id,'b2bking_rule_applies', true); $howmuch = get_post_meta($grule_id,'b2bking_rule_howmuch', true); // if agent group is group 1, check type agent value and condition, and if pass, promote to group 2 if ($user_group === $group1){ if ($type === 'order_value_monthly_higher'){ if ($monthly_spent > $howmuch){ // promote to group 2 update_user_meta($user_id,'b2bking_customergroup', $group2); $user_group = $group2; } } if ($type === 'order_value_monthly_lower'){ if ($monthly_spent < $howmuch){ // promote to group 2 update_user_meta($user_id,'b2bking_customergroup', $group2); $user_group = $group2; } } } } } // calculated finish update_user_meta($user_id,'b2bking_monthly_rules_calculated_'.$current_month, 'yes'); }