Skip to content

Instantly share code, notes, and snippets.

@sidharrell
Created January 16, 2014 22:12
Show Gist options
  • Select an option

  • Save sidharrell/8464476 to your computer and use it in GitHub Desktop.

Select an option

Save sidharrell/8464476 to your computer and use it in GitHub Desktop.
diff for apply coupon to cart once modification
--- a/includes/admin-files/optional_event_settings.php
+++ b/includes/admin-files/optional_event_settings.php
@@ -103,7 +103,13 @@ $values=array(
<?php _e('Allow discounts in the shopping cart?','event_espresso'); ?>
</label>
<?php echo select_input('allow_mer_discounts', $values, isset($org_options['allow_mer_discounts']) ? $org_options['allow_mer_discounts'] : ''); ?> <br />
- </li>
+ </li>
+ <li>
+ <label>
+ <?php _e('Apply discounts in the shopping cart once only?','event_espresso'); ?>
+ </label>
+ <?php echo select_input('apply_mer_discounts_once', $values, isset($org_options['apply_mer_discounts_once']) ? $org_options['apply_mer_discounts_once'] : ''); ?> <br />
+ </li>
<?php endif; ?>
<?php if ( function_exists( 'event_espresso_groupon_payment_page' ) ) : ?>
<li>
--- a/includes/functions/cart.php
+++ b/includes/functions/cart.php
@@ -287,6 +287,7 @@ if (!function_exists('event_espresso_update_item_in_session')) {
*/
if (!function_exists('event_espresso_calculate_total')) {
function event_espresso_calculate_total( $update_section = FALSE, $mer = TRUE ) {
+ global $org_options;
do_action('action_hook_espresso_log', __FILE__, __FUNCTION__, '');
@@ -362,6 +363,7 @@ if (!function_exists('event_espresso_calculate_total')) {
//echo '<h4>event_cost : ' . $event_individual_cost[$event_id] . ' <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span></h4>';
+ $apply_coupon = TRUE;
if ( is_array( $event_price )) {
foreach ( $event_price as $_price_id => $qty ) {
@@ -369,12 +371,20 @@ if (!function_exists('event_espresso_calculate_total')) {
if ( $attendee_quantity > 0 ) {
// Process coupons
- $coupon_results['event_cost'] = event_espresso_get_final_price( $_price_id, $event_id );
- $coupon_results = apply_filters( 'filter_hook_espresso_coupon_results', $coupon_results, $event_id, $mer );
- $coupon_notifications = apply_filters( 'filter_hook_espresso_cart_modifier_strings', $coupon_notifications, $coupon_results['msg'] );
- $coupon_errors = apply_filters( 'filter_hook_espresso_cart_modifier_strings', $coupon_errors, $coupon_results['error'] );
- if ( $coupon_results['valid'] ) {
- $coupon_events = apply_filters( 'filter_hook_espresso_cart_coupon_events_array', $coupon_events, $event['event_name'] );
+ $pre_discount_price = $coupon_results['event_cost'] = event_espresso_get_final_price( $_price_id, $event_id );
+ if ($apply_coupon) {
+ $coupon_results = apply_filters( 'filter_hook_espresso_coupon_results', $coupon_results, $event_id, $mer );
+ $coupon_notifications = apply_filters( 'filter_hook_espresso_cart_modifier_strings', $coupon_notifications, $coupon_results['msg'] );
+ $coupon_errors = apply_filters( 'filter_hook_espresso_cart_modifier_strings', $coupon_errors, $coupon_results['error'] );
+ if ( $coupon_results['valid'] ) {
+ $coupon_events = apply_filters( 'filter_hook_espresso_cart_coupon_events_array', $coupon_events, $event['event_name'] );
+ }
+ if ($pre_discount_price != $coupon_results['event_cost'] && $org_options['apply_mer_discounts_once']=='Y') {
+ $discount = $pre_discount_price - $coupon_results['event_cost'];
+ $discount_per_ticket = $discount/$qty;
+ $coupon_results['event_cost'] = $pre_discount_price - $discount_per_ticket;
+ $apply_coupon = FALSE;
+ }
}
$event_cost = $coupon_results['event_cost'];
--- a/includes/organization_config.php
+++ b/includes/organization_config.php
@@ -34,6 +34,7 @@ function organization_config_mnu() {
$org_options['time_reg_limit'] = isset($_POST['time_reg_limit']) && !empty($_POST['time_reg_limit']) ? $_POST['time_reg_limit'] : '';
$org_options['skip_confirmation_page'] = isset($_POST['skip_confirmation_page']) ? $_POST['skip_confirmation_page'] : 'N';
$org_options['allow_mer_discounts'] = isset($_POST['allow_mer_discounts']) ? $_POST['allow_mer_discounts'] : 'N';
+ $org_options['apply_mer_discounts_once'] = isset($_POST['apply_mer_discounts_once']) ? $_POST['apply_mer_discounts_once'] : 'N';
$org_options['allow_mer_vouchers'] = isset($_POST['allow_mer_vouchers']) ? $_POST['allow_mer_vouchers'] : 'N';
$org_options['use_attendee_pre_approval'] = isset($_POST['use_attendee_pre_approval']) && !empty($_POST['use_attendee_pre_approval']) ? $_POST['use_attendee_pre_approval'] : '';
if (!empty($_POST['event_ssl_active']))
--- a/includes/process-registration/add_attendees_to_db.php
+++ b/includes/process-registration/add_attendees_to_db.php
@@ -339,22 +339,37 @@ if ( ! function_exists( 'event_espresso_add_attendees_to_db' )) {
$end_date = $time->end_date;
}
-
-
-
-
+ //If we are using the number of attendees dropdown, find out how many tickets they want.
+ //echo $data_source['espresso_addtl_limit_dd'];
+ if (isset($data_source['espresso_addtl_limit_dd'])) {
+ $num_people = absint($data_source ['num_people']);
+ } elseif (isset($event_meta['additional_attendee_reg_info']) && $event_meta['additional_attendee_reg_info'] == 1) {
+ $num_people = absint($data_source ['num_people']);
+ } else {
+ $num_people = 1;
+ }
+
// check for coupon
if ( function_exists( 'event_espresso_process_coupon' )) {
- if ( $coupon_results = event_espresso_process_coupon( $event_id, $final_price, $multi_reg )) {
- //printr( $coupon_results, '$coupon_results <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' );
- if ( $coupon_results['valid'] ) {
- $final_price = number_format( $coupon_results['event_cost'], 2, '.', '' );
- $coupon_code = $coupon_results['code'];
- }
- if ( ! $multi_reg && ! empty( $coupon_results['msg'] )) {
- $notifications['coupons'] = $coupon_results['msg'];
- }
- }
+ static $apply_coupon = TRUE;
+ if ($apply_coupon) {
+ if ( $coupon_results = event_espresso_process_coupon( $event_id, $final_price, $multi_reg )) {
+ //printr( $coupon_results, '$coupon_results <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' );
+ if ( $coupon_results['valid'] ) {
+ $final_price = number_format( $coupon_results['event_cost'], 2, '.', '' );
+ $coupon_code = $coupon_results['code'];
+ }
+ if ( ! $multi_reg && ! empty( $coupon_results['msg'] )) {
+ $notifications['coupons'] = $coupon_results['msg'];
+ }
+ if ($orig_price != $final_price && $org_options['apply_mer_discounts_once']=='Y') {
+ $discount = $orig_price - $final_price;
+ $discount_per_ticket = $discount/$num_people;
+ $final_price = $orig_price - $discount_per_ticket;
+ $apply_coupon = FALSE;
+ }
+ }
+ }
}
// check for groupon
@@ -384,16 +399,6 @@ if ( ! function_exists( 'event_espresso_add_attendees_to_db' )) {
$orig_price = number_format( (float)$orig_price, 2, '.', '' );
$final_price = number_format( (float)$final_price, 2, '.', '' );
$total_cost = $total_cost + $final_price;
-
- //If we are using the number of attendees dropdown, find out how many tickets they want.
- //echo $data_source['espresso_addtl_limit_dd'];
- if (isset($data_source['espresso_addtl_limit_dd'])) {
- $num_people = absint($data_source ['num_people']);
- } elseif (isset($event_meta['additional_attendee_reg_info']) && $event_meta['additional_attendee_reg_info'] == 1) {
- $num_people = absint($data_source ['num_people']);
- } else {
- $num_people = 1;
- }
$columns_and_values = array(
'registration_id' => $registration_id,
@dorian2727

Copy link
Copy Markdown

Hopefully this solves my issue: Say you have a coupon code for $50. An event is $35 per ticket. Now lets say the one registering is adding one attendee. The sub total is $70. Applying the coupon you would think the total due is now $20. Well they tell me at Event Espresso it takes the $50 off each ticket. I need it off the total. Does this do this or do you have a way? I need this to work whether they add an attendee or select the number of tickets in the drop down. Thanks in advance

@dorian2727

Copy link
Copy Markdown

...and how would you use this file? How to tie it in to the registration and checkout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment