/** * 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; }
/** * 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); }
/** * 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()); }
/** * 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; }
/** * 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); }