/** * Deducts the points redeemed for a discount when the order is processed at checkout. Note that points are deducted * immediately upon checkout processing to protect against abuse. * * @since 1.0 * @param int $order_id the WC_Order ID */ public function maybe_deduct_redeemed_points($order_id) { global $woocommerce, $wc_points_rewards; $order = new WC_Order($order_id); // bail for guest user if (!$order->user_id) { return; } $discount_code = WC_Points_Rewards_Discount::get_discount_code(); // only deduct points if they were redeemed for a discount if (!$woocommerce->cart->has_discount($discount_code)) { return; } $discount_amount = isset($woocommerce->cart->coupon_discount_amounts[$discount_code]) ? $woocommerce->cart->coupon_discount_amounts[$discount_code] : 0; $points_redeemed = WC_Points_Rewards_Manager::calculate_points_for_discount($discount_amount); // deduct points WC_Points_Rewards_Manager::decrease_points($order->user_id, $points_redeemed, 'order-redeem', array('discount_code' => $discount_code, 'discount_amount' => $discount_amount), $order->id); update_post_meta($order->id, '_wc_points_redeemed', $points_redeemed); // add order note $order->add_order_note(sprintf(__('%d %s redeemed for a %s discount.', 'wc_points_rewards'), $points_redeemed, $wc_points_rewards->get_points_label($points_redeemed), woocommerce_price($discount_amount))); }
/** * Renders a message and button above the cart displaying the points available to redeem for a discount * * @since 1.0 */ public function render_redeem_points_message() { global $woocommerce, $wc_points_rewards; // don't display a message if coupons are disabled or points have already been applied for a discount if (!$woocommerce->cart->coupons_enabled() || $woocommerce->cart->has_discount(WC_Points_Rewards_Discount::get_discount_code())) { return; } // get the total discount available for redeeming points $discount_available = $this->get_discount_for_redeeming_points(); $message = get_option('wc_points_rewards_redeem_points_message'); // bail if no message set or no points will be earned for purchase if (!$message || !$discount_available) { return; } // points required to redeem for the discount available $points = WC_Points_Rewards_Manager::calculate_points_for_discount($discount_available); $message = str_replace('{points}', number_format_i18n($points), $message); // the maximum discount available given how many points the customer has $message = str_replace('{points_value}', woocommerce_price($discount_available), $message); // points label $message = str_replace('{points_label}', $wc_points_rewards->get_points_label($points), $message); // add 'Apply Discount' button $message .= '<form class="wc_points_rewards_apply_discount" action="' . esc_url($woocommerce->cart->get_cart_url()) . '" method="post">'; $message .= '<input type="hidden" name="wc_points_rewards_apply_discount_amount" class="wc_points_rewards_apply_discount_amount" />'; $message .= '<input type="submit" class="button wc_points_rewards_apply_discount" name="wc_points_rewards_apply_discount" value="' . __('Apply Discount', 'wc_points_rewards') . '" /></form>'; // wrap with info div $message = '<div class="woocommerce-info wc_points_rewards_redeem_points">' . $message . '</div>'; echo apply_filters('wc_points_rewards_redeem_points_message', $message, $discount_available); if ('yes' === get_option('wc_points_rewards_partial_redemption_enabled')) { // Add code to prompt for points amount $js = ' $( "input.wc_points_rewards_apply_discount" ).click( function() { var points = prompt( "' . esc_js(__('How many points would you like to apply?', 'wc_points_rewards')) . '", "' . $points . '" ); if ( points != null ) { $( "input.wc_points_rewards_apply_discount_amount" ).val( points ); } return true; }); '; if (function_exists('wc_enqueue_js')) { wc_enqueue_js($js); } else { $woocommerce->add_inline_js($js); } } // add AJAX submit for applying the discount on the checkout page if (is_checkout()) { $js = ' /* Points & Rewards AJAX Apply Points Discount */ $( "form.wc_points_rewards_apply_discount" ).submit( function() { var $section = $( "div.wc_points_rewards_redeem_points" ); if ( $section.is( ".processing" ) ) return false; $section.addClass( "processing" ).block({message: null, overlayCSS: {background: "#fff url(" + woocommerce_params.ajax_loader_url + ") no-repeat center", backgroundSize: "16px 16px", opacity: 0.6}}); var data = { action: "wc_points_rewards_apply_discount", discount_amount: $("input.wc_points_rewards_apply_discount_amount").val(), security: ( woocommerce_params.apply_coupon_nonce ? woocommerce_params.apply_coupon_nonce : wc_checkout_params.apply_coupon_nonce ) }; $.ajax({ type: "POST", url: woocommerce_params.ajax_url, data: data, success: function( code ) { $( ".woocommerce-error, .woocommerce-message" ).remove(); $section.removeClass( "processing" ).unblock(); if ( code ) { $section.before( code ); $section.remove(); $( "body" ).trigger( "update_checkout" ); } }, dataType: "html" }); return false; }); '; if (function_exists('wc_enqueue_js')) { wc_enqueue_js($js); } else { $woocommerce->add_inline_js($js); } } }