/** * Returns the amount of points earned for the purchase, calculated by getting the points earned for each individual * product purchase multiplied by the quantity being ordered * * @since 1.0 */ private function get_points_earned_for_purchase($order) { $points_earned = 0; foreach ($order->get_items() as $item_key => $item) { $product = $order->get_product_from_item($item); if (!is_object($product)) { continue; } // If prices include tax, we include the tax in the points calculation if (get_option('woocommerce_prices_include_tax') == 'no') { // Get the un-discounted price paid and adjust our product price // Get item subtotal was buggy pre 2.0.19 if (version_compare(WOOCOMMERCE_VERSION, '2.0.19', '<')) { $item_price = $item['line_subtotal'] / $item['qty']; $item_price = number_format($item_price, 2, '.', ''); } else { $item_price = $order->get_item_subtotal($item, false, true); } } else { // Get the un-discounted price paid and adjust our product price // Get item subtotal was buggy pre 2.0.19 if (version_compare(WOOCOMMERCE_VERSION, '2.0.19', '<')) { $item_price = ($item['line_subtotal'] + $item['line_subtotal_tax']) / $item['qty']; $item_price = number_format($item_price, 2, '.', ''); } else { $item_price = $order->get_item_subtotal($item, true, true); } } $product->set_price($item_price); // Calc points earned $points_earned += WC_Points_Rewards_Product::get_points_earned_for_product_purchase($product, $item_price) * $item['qty']; } // reduce by any discounts. One minor drawback: if the discount includes a discount on tax and/or shipping // it will cost the customer points, but this is a better solution than granting full points for discounted orders $discount = $order->order_discount + $order->cart_discount; $points_earned -= min(WC_Points_Rewards_Manager::calculate_points($discount), $points_earned); // check if applied coupons have a points modifier and use it to adjust the points earned $coupons = $order->get_used_coupons(); if (!empty($coupons)) { $points_modifier = 0; // get the maximum points modifier if there are multiple coupons applied, each with their own modifier foreach ($coupons as $coupon_code) { $coupon = new WC_Coupon($coupon_code); if (!empty($coupon->coupon_custom_fields['_wc_points_modifier'][0]) && $coupon->coupon_custom_fields['_wc_points_modifier'][0] > $points_modifier) { $points_modifier = $coupon->coupon_custom_fields['_wc_points_modifier'][0]; } } if ($points_modifier > 0) { $points_earned = round($points_earned * ($points_modifier / 100)); } } return apply_filters('wc_points_rewards_points_earned_for_purchase', $points_earned, $order); }
/** * Points and Rewards single product message for per-product priced Bundles. * @param string $message * @param WC_Points_Rewards_Product $points_n_rewards * @return string */ public static function points_rewards_composite_message($message, $points_n_rewards) { global $product; if ($product->product_type == 'composite') { if (!$product->is_priced_per_product()) { return $message; } // Will calculate points based on min_composite_price, which is saved as _price meta $composite_points = WC_Points_Rewards_Product::get_points_earned_for_product_purchase($product); $message = $points_n_rewards->create_at_least_message_to_product_summary($composite_points); } return $message; }
/** * Points and Rewards single product message for per-product priced Bundles. * * @param string $message * @param WC_Points_Rewards_Product $points_n_rewards * @return string */ function points_rewards_bundle_message($message, $points_n_rewards) { global $product; if ($product->product_type == 'bundle') { if (!$product->is_priced_per_product()) { return $message; } // Will calculate points based on min_bundle_price $bundle_points = WC_Points_Rewards_Product::get_points_earned_for_product_purchase($product); $message = $points_n_rewards->create_at_least_message_to_product_summary($bundle_points); } return $message; }
/** * Returns the amount of points earned for the purchase, calculated by getting the points earned for each individual * product purchase multiplied by the quantity being ordered * * @since 1.0 */ private function get_points_earned_for_purchase() { global $woocommerce; $points_earned = 0; foreach ($woocommerce->cart->cart_contents as $item_key => $item) { $points_earned += WC_Points_Rewards_Product::get_points_earned_for_product_purchase($item['data']) * $item['quantity']; } // reduce by any discounts. One minor drawback: if the discount includes a discount on tax and/or shipping // it will cost the customer points, but this is a better solution than granting full points for discounted orders $discount = $woocommerce->cart->discount_cart + $woocommerce->cart->discount_total; $points_earned -= min(WC_Points_Rewards_Manager::calculate_points($discount), $points_earned); // check if applied coupons have a points modifier and use it to adjust the points earned $coupons = $woocommerce->cart->get_applied_coupons(); if (!empty($coupons)) { $points_modifier = 0; // get the maximum points modifier if there are multiple coupons applied, each with their own modifier foreach ($coupons as $coupon_code) { $coupon = new WC_Coupon($coupon_code); if (!empty($coupon->coupon_custom_fields['_wc_points_modifier'][0]) && $coupon->coupon_custom_fields['_wc_points_modifier'][0] > $points_modifier) { $points_modifier = $coupon->coupon_custom_fields['_wc_points_modifier'][0]; } } if ($points_modifier > 0) { $points_earned = round($points_earned * ($points_modifier / 100)); } } return apply_filters('wc_points_rewards_points_earned_for_purchase', $points_earned, $woocommerce->cart); }