/**
  * Template function to render the template
  *
  * @since 1.0
  */
 function woocommerce_points_rewards_my_points()
 {
     global $wc_points_rewards;
     $points_balance = WC_Points_Rewards_Manager::get_users_points(get_current_user_id());
     $points_label = $wc_points_rewards->get_points_label($points_balance);
     $count = apply_filters('wc_points_rewards_my_account_points_events', 5, get_current_user_id());
     // get a set of points events, ordered newest to oldest
     $args = array('orderby' => array('field' => 'date', 'order' => 'DESC'), 'per_page' => $count, 'paged' => 0, 'user' => get_current_user_id());
     $events = WC_Points_Rewards_Points_Log::get_points_log_entries($args);
     // load the template
     woocommerce_get_template('myaccount/my-points.php', array('points_balance' => $points_balance, 'points_label' => $points_label, 'events' => $events), '', $wc_points_rewards->get_plugin_path() . '/templates/');
 }
 /**
  * Return zero points for composited cart items if container item has product level points.
  *
  * @param  int        $points
  * @param  string     $item_key
  * @param  array      $item
  * @param  WC_Order   $order
  * @return int
  */
 public static function points_earned_for_composited_order_item($points, $product, $item_key, $item, $order)
 {
     if (isset($item['composite_parent'])) {
         // find container item
         foreach ($order->get_items() as $order_item) {
             $is_parent = isset($order_item['composite_cart_key']) && $item['composite_parent'] === $order_item['composite_cart_key'];
             if ($is_parent) {
                 $parent_item = $order_item;
                 $composite_id = $parent_item['product_id'];
                 // check if earned points are set at product-level
                 $composite_points = get_post_meta($composite_id, '_wc_points_earned', true);
                 $per_product_priced_composite = isset($parent_item['per_product_pricing']) ? $parent_item['per_product_pricing'] : get_post_meta($composite_id, '_per_product_pricing_bto', true);
                 if (!empty($composite_points) || $per_product_priced_composite !== 'yes') {
                     $points = 0;
                 } else {
                     $points = WC_Points_Rewards_Manager::calculate_points($product->get_price());
                 }
                 break;
             }
         }
     }
     return $points;
 }
 /**
  * Process a payment
  */
 public function process_payment($order_id)
 {
     $order = wc_get_order($order_id);
     if (!is_user_logged_in()) {
         wc_add_notice(__('Payment error:', 'custom-points-product') . ' ' . __('You must be logged in to use this payment method', 'custom-points-product'), 'error');
         return;
     }
     $user_id = $order->get_user_id();
     $available_points = WC_Points_Rewards_Manager::get_users_points($user_id);
     $total_points = Custom_Points_Order::get_order_points_cost_total($order);
     if ($available_points < $total_points) {
         wc_add_notice(__('Payment error:', 'custom-points-product') . ' ' . __('Insufficient points in your account.', 'custom-points-product'), 'error');
         return;
     }
     // deduct points from account
     WC_Points_Rewards_Manager::decrease_points($user_id, $total_points, 'custom-points-gateway');
     $order->set_total(0);
     // Payment complete
     $order->payment_complete();
     // Remove cart
     WC()->cart->empty_cart();
     // Return thankyou redirect
     return array('result' => 'success', 'redirect' => $this->get_return_url($order));
 }
 public function replace_variables($text, $email_order)
 {
     global $wc_points_rewards;
     $event_data = get_option('fue_email_order_' . $email_order->id, false);
     if (!$event_data) {
         $event_data = array('user_id' => 0, 'points' => 0, 'event_type' => '');
     }
     $points = $event_data['points'];
     $points_label = $wc_points_rewards->get_points_label($points);
     $description = WC_Points_Rewards_Manager::event_type_description($event_data['event_type']);
     $search = array('{points_earned}', '{reward_event_description}');
     $replacements = array($points, $description);
     return str_replace($search, $replacements, $text);
 }
 /**
  * Returns all user points records
  *
  * @since 1.0
  * @param $args array arguments for the user query
  * @return array of user_points objects with user_id and points_balance fields
  */
 public static function get_all_users_points($args)
 {
     if (!isset($args['fields'])) {
         $args['fields'] = 'ID';
     }
     $args['meta_key'] = 'wc_points_balance';
     // perform the user query, altering the orderby as needed when ordering by user points
     if ('points' === $args['orderby']) {
         add_action('pre_user_query', array(__CLASS__, 'order_user_by_points'));
     }
     $wp_user_query = new WP_User_Query($args);
     if ('points' === $args['orderby']) {
         remove_action('pre_user_query', array(__CLASS__, 'order_user_by_points'));
     }
     // record the total result set (for pagination purposes)
     if (isset($args['count_total']) && $args['count_total']) {
         self::$found_users = $wp_user_query->get_total();
     }
     $results = array();
     // build the expected user points records
     foreach ($wp_user_query->get_results() as $user_id) {
         $result = new stdClass();
         $result->user_id = $user_id;
         $result->points_balance = self::get_users_points($user_id);
         $results[] = $result;
     }
     return $results;
 }
 /**
  * Prepare the list of user points items for display
  *
  * @see WP_List_Table::prepare_items()
  * @since 1.0
  */
 public function prepare_items()
 {
     $this->process_actions();
     $per_page = $this->get_items_per_page('wc_points_rewards_manage_points_users_per_page');
     $args = array('orderby' => $this->get_current_orderby(), 'order' => $this->get_current_order(), 'offset' => ($this->get_pagenum() - 1) * $per_page, 'number' => $per_page, 'count_total' => true);
     // Filter: by customer
     $args = $this->add_filter_args($args);
     $this->items = WC_Points_Rewards_Manager::get_all_users_points($args);
     $this->set_pagination_args(array('total_items' => WC_Points_Rewards_Manager::get_found_user_points(), 'per_page' => $per_page, 'total_pages' => ceil(WC_Points_Rewards_Manager::get_found_user_points() / $per_page)));
 }
 /**
  * Handle an order that is cancelled or refunded by:
  *
  * 1) Removing any points earned for the order
  *
  * 2) Crediting points redeemed for a discount back to the customer's account if the order that they redeemed the points
  * for a discount on is cancelled or refunded
  *
  * @since 1.0
  * @param int $order_id the WC_Order ID
  */
 public function handle_cancelled_refunded_order($order_id)
 {
     global $wc_points_rewards;
     $order = new WC_Order($order_id);
     // bail for guest user
     if (!$order->user_id) {
         return;
     }
     // handle removing any points earned for the order
     $points_earned = get_post_meta($order->id, '_wc_points_earned', true);
     if ($points_earned > 0) {
         // remove points
         WC_Points_Rewards_Manager::decrease_points($order->user_id, $points_earned, 'order-cancelled', null, $order->id);
         // remove points from order
         delete_post_meta($order->id, '_wc_points_earned');
         // add order note
         $order->add_order_note(sprintf(__('%d %s removed.', 'wc_points_rewards'), $points_earned, $wc_points_rewards->get_points_label($points_earned)));
     }
     // handle crediting points redeemed for a discount
     $points_redeemed = get_post_meta($order->id, '_wc_points_redeemed', true);
     if ($points_redeemed > 0) {
         // credit points
         WC_Points_Rewards_Manager::increase_points($order->user_id, $points_redeemed, 'order-cancelled', null, $order->id);
         // remove points from order
         delete_post_meta($order->id, '_wc_points_redeemed');
         // add order note
         $order->add_order_note(sprintf(__('%d %s credited back to customer.', 'wc_points_rewards'), $points_redeemed, $wc_points_rewards->get_points_label($points_redeemed)));
     }
 }
 /**
  * Calculate the points earned when a product or category is set to a percentage. This modifies the default points
  * earned based on the global "Earn Points Conversion Rate" setting and products price by the given $percentage.
  * e.g. a 200% multiplier will change 5 points to 10.
  *
  * @since 1.0
  * @param string $percentage the percentage to multiply the default points earned by
  * @param object $product the product to get the points earned for
  * @return int the points earned after adjusting for the multiplier
  */
 private static function calculate_points_multiplier($percentage, $product)
 {
     $percentage = str_replace('%', '', $percentage) / 100;
     return $percentage * WC_Points_Rewards_Manager::calculate_points($product->get_price());
 }
 public function replace_variables($text, $email_order)
 {
     global $wc_points_rewards;
     $event_data = get_option('fue_email_order_' . $email_order->id, false);
     if (!$event_data) {
         $event_data = array('user_id' => 0, 'points' => 0, 'event_type' => '');
     }
     $points = $event_data['points'];
     $points_label = $wc_points_rewards->get_points_label($points);
     $description = WC_Points_Rewards_Manager::event_type_description($event_data['event_type']);
     $search = array('{order_number}', '{order_date}', '{order_datetime}', '{customer_first_name}', '{customer_name}', '{customer_email}', '{points_earned}', '{reward_event_description}');
     if ($email_order->order_id > 0) {
         $order = new WC_Order($email_order->order_id);
         $order_number = $order->get_order_number();
         $order_date = date(get_option('date_format'), strtotime($order->order_date));
         $order_datetime = date(get_option('date_format') . ' ' . get_option('time_format'), strtotime($order->order_date));
         $first_name = $order->billing_first_name;
         $name = $order->billing_first_name . ' ' . $order->billing_last_name;
         $email = $order->billing_email;
     } else {
         $user = new WP_User($email_order->user_id);
         $order_number = '';
         $order_date = '';
         $order_datetime = '';
         $first_name = $user->first_name;
         $name = $user->first_name . ' ' . $user->last_name;
         $email = $user->user_email;
     }
     $replacements = array($order_number, $order_date, $order_datetime, $first_name, $name, $email, $points, $description);
     return str_replace($search, $replacements, $text);
 }
示例#10
0
 /**
  * Return zero points for bundled cart items if container item has product level points.
  *
  * @param  int     $points
  * @param  string  $cart_item_key
  * @param  array   $cart_item_values
  * @return int
  */
 function points_earned_for_bundled_cart_item($points, $cart_item_key, $cart_item_values)
 {
     if (isset($cart_item_values['bundled_by'])) {
         $cart_contents = WC()->cart->get_cart();
         $bundle_cart_id = $cart_item_values['bundled_by'];
         $bundle = $cart_contents[$bundle_cart_id]['data'];
         // check if earned points are set at product-level
         $bundle_points = WC_Points_Rewards_Product::get_product_points($bundle);
         $per_product_priced_bundle = $bundle->is_priced_per_product();
         $has_bundle_points = is_numeric($bundle_points) ? true : false;
         if ($has_bundle_points || $per_product_priced_bundle == false) {
             $points = 0;
         } else {
             $points = WC_Points_Rewards_Manager::calculate_points($cart_item_values['data']->get_price());
         }
     }
     return $points;
 }
 /**
  * Gets point log entries based on $args
  *
  * @since 1.0
  * @param array $args the query arguments
  * @return array of log entry objects
  */
 public static function get_points_log_entries($args)
 {
     global $wc_points_rewards, $wpdb;
     // special handling for searching by user
     if (!empty($args['user'])) {
         $args['where'][] = $wpdb->prepare("{$wc_points_rewards->user_points_log_db_tablename}.user_id = %s", $args['user']);
     }
     // special handling for searching by event type
     if (!empty($args['event_type'])) {
         $args['where'][] = $wpdb->prepare("{$wc_points_rewards->user_points_log_db_tablename}.type = %s", $args['event_type']);
     }
     $entries = array();
     foreach (self::query($args) as $log_entry) {
         // maybe unserialize the arbitrary data object
         $log_entry->data = maybe_unserialize($log_entry->data);
         // Format the event date as "15 minutes ago" if the event took place in the last 24 hours, otherwise just show the date (timestamp on mouse hover)
         $timestamp = strtotime($log_entry->date);
         $t_time = date_i18n('Y/m/d g:i:s A', $timestamp);
         $time_diff = current_time('timestamp', true) - $timestamp;
         if ($time_diff > 0 && $time_diff < 24 * 60 * 60) {
             $h_time = sprintf(__('%s ago', 'wc_points_rewards'), human_time_diff($timestamp, current_time('timestamp', true)));
         } else {
             $h_time = date_i18n(woocommerce_date_format(), $timestamp);
         }
         $log_entry->date_display_human = $h_time;
         $log_entry->date_display = $t_time;
         // retrieve the description
         $log_entry->description = WC_Points_Rewards_Manager::event_type_description($log_entry->type, $log_entry);
         $entries[] = $log_entry;
     }
     return $entries;
 }
 /**
  * Returns the maximum possible discount available given the total amount of points the customer has
  *
  * @since 1.0
  */
 public static function get_discount_for_redeeming_points($applying = false)
 {
     global $woocommerce;
     // get the value of the user's point balance
     $available_user_discount = WC_Points_Rewards_Manager::get_users_points_value(get_current_user_id());
     // no discount
     if ($available_user_discount <= 0) {
         return 0;
     }
     if ($applying && 'yes' === get_option('wc_points_rewards_partial_redemption_enabled') && !empty($woocommerce->session->wc_points_rewards_discount_amount)) {
         $requested_user_discount = WC_Points_Rewards_Manager::calculate_points_value($woocommerce->session->wc_points_rewards_discount_amount);
         if ($requested_user_discount > 0 && $requested_user_discount < $available_user_discount) {
             $available_user_discount = $requested_user_discount;
         }
     }
     $discount_applied = 0;
     if (!did_action('woocommerce_before_calculate_totals')) {
         $woocommerce->cart->calculate_totals();
     }
     // calculate the discount to be applied by iterating through each item in the cart and calculating the individual
     // maximum discount available
     foreach ($woocommerce->cart->get_cart() as $item_key => $item) {
         $discount = 0;
         $max_discount = WC_Points_Rewards_Product::get_maximum_points_discount_for_product($item['data']);
         if (is_numeric($max_discount)) {
             // adjust the max discount by the quantity being ordered
             $max_discount *= $item['quantity'];
             // if the discount available is greater than the max discount, apply the max discount
             $discount = $available_user_discount <= $max_discount ? $available_user_discount : $max_discount;
             // Max should be product price. As this will be applied before tax, it will respect other coupons.
         } else {
             // Use the line price - this is the max we can apply here
             if (method_exists($item['data'], 'get_price_including_tax')) {
                 $max_discount = $item['data']->get_price_including_tax($item['quantity']);
             } else {
                 $max_discount = $item['data']->get_price() * $item['quantity'];
             }
             // if the discount available is greater than the max discount, apply the max discount
             $discount = $available_user_discount <= $max_discount ? $available_user_discount : $max_discount;
         }
         // add the discount to the amount to be applied
         $discount_applied += $discount;
         // reduce the remaining discount available to be applied
         $available_user_discount -= $discount;
     }
     // if the available discount is greater than the order total, make the discount equal to the order total less any other discounts
     if (get_option('woocommerce_prices_include_tax') == 'no') {
         $discount_applied = max(0, min($discount_applied, $woocommerce->cart->subtotal_ex_tax - $woocommerce->cart->discount_total));
     } else {
         $discount_applied = max(0, min($discount_applied, $woocommerce->cart->subtotal - $woocommerce->cart->discount_total));
     }
     // limit the discount available by the global maximum discount if set
     $max_discount = get_option('wc_points_rewards_cart_max_discount');
     if ($max_discount && $max_discount < $discount_applied) {
         $discount_applied = $max_discount;
     }
     return $discount_applied;
 }
 /**
  * Scan through the keys of $variables and apply the replacement if one is found
  * @param array     $variables
  * @param array     $email_data
  * @param object    $queue_item
  * @param FUE_Email $email
  * @return array
  */
 protected function add_variable_replacements($variables, $email_data, $queue_item, $email)
 {
     $event_data = get_option('fue_email_order_' . $queue_item->id, false);
     if (!$event_data) {
         $event_data = array('user_id' => 0, 'points' => 0, 'event_type' => '');
     }
     $points = $event_data['points'];
     $description = WC_Points_Rewards_Manager::event_type_description($event_data['event_type']);
     $variables['points_earned'] = $points;
     $variables['reward_event_description'] = $description;
     return $variables;
 }
 /**
  * Add points to customer for creating an account
  *
  * @since 1.0
  */
 public function create_account_action($user_id)
 {
     $points = get_option('wc_points_rewards_account_signup_points');
     if (!empty($points)) {
         WC_Points_Rewards_Manager::increase_points($user_id, $points, 'account-signup');
     }
 }
 /**
  * Deletes the user points for the deleted user identified by $user_id
  *
  * @since 1.0
  * @param int $user_id the identifier of the user being deleted
  */
 public function delete_user_points($user_id)
 {
     WC_Points_Rewards_Manager::delete_user_points($user_id);
 }