-
-
Save stephenharris/6b80f310a107e8aca9ce93508e5b5934 to your computer and use it in GitHub Desktop.
Skeleton plugin for handling discounts in Event Organiser Pro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready( function($) { | |
wp.hooks.addFilter( 'eventorganiser.checkout_cart', function( cart ){ | |
var total = cart.total; //total amount | |
var quantity = cart.quantity; //quantity of tickets | |
//Calculate amount to be discounted: | |
var discount = 0; | |
if( discount ){ | |
cart.total = total - discount | |
$('.eo-booking-bulk-discount-row').hide(); | |
return cart; | |
} | |
$('.eo-booking-bulk-discount-row').show(); | |
return cart; | |
}, 5 ); | |
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Event Organiser Bulk Discounts | |
Plugin URI: http://www.wp-event-organiser.com | |
Version: 0.1.0 | |
Description: Bulk discounts for Event Organiser bookings | |
Author: Stephen Harris | |
Author URI: http://www.stephenharris.info | |
*/ | |
//Initiates the plug-in | |
add_action( 'plugins_loaded', array( 'EO_Discount_Codes', 'init' ) ); | |
class EO_Bulk_Discount{ | |
/** | |
* Instance of the class | |
* @static | |
* @access protected | |
* @var object | |
*/ | |
protected static $instance; | |
/** | |
* Instantiates the class | |
* @return object $instance | |
*/ | |
public static function init() { | |
is_null( self :: $instance ) AND self :: $instance = new self; | |
return self :: $instance; | |
} | |
public function __construct() { | |
//Register scripts | |
add_action( 'init', array( $this, 'register_scripts' ) ); | |
//Add discount row | |
add_action( 'eventorganiser_booking_pre_total_row', array( $this, 'insert_discount_row' ), 5, 2 ); | |
//Calculate and store discount amount | |
add_action( 'eventorganiser_new_booking', array( $this, 'store_discount_amount' ), 4 ); | |
//Apply discount | |
add_action( 'eventorganiser_get_booking_meta_booking_amount', array( $this, 'apply_discount' ), 400, 2 ); | |
//Add discount row to email | |
add_action( 'eventorganiser_email_ticket_list_pre_total', array( $this, 'add_to_email' ), 400, 2 ); | |
add_action( 'eventorganiser_get_booking_table_for_email_pre_total', array( $this, 'add_to_email' ), 400, 2 ); | |
//Add to PayPal | |
add_filter( 'eventorganiser_pre_gateway_checkout_paypal', array( $this, 'add_to_paypal_cart' ), 10, 2 ); | |
} | |
/** | |
* Register javascript and stylesheets | |
*/ | |
function register_scripts(){ | |
wp_register_script( | |
'eo_bulk_discount_script', | |
plugin_dir_url(__FILE__ ). "bulk-discount.js", | |
array( 'eo_pro_occurrence_picker' ) | |
); | |
} | |
/** | |
* Adds discount row to the ticket picker & enqueues scripts | |
* | |
* @param string $html | |
*/ | |
function insert_discount_row( $html ){ | |
//Get currency symbol | |
$currency = eventorganiser_pro_get_option( 'currency' ); | |
$symbol = eventorganiser_get_currency_symbol( $currency ); | |
$placeholder = ( eventorganiser_pro_get_option( 'currency_position' ) == 1 ? '%1$s%2$s' : '%2$s%1$s' ); | |
//'Discount' row | |
?> | |
<tr class="eo-booking-bulk-discount-row" style="display:none;"> | |
<td><strong> <?php esc_html_e( 'Discount:', 'eventorganiserbd' ); ?></strong></td> | |
<td> | |
-<?php printf( $placeholder, $symbol, '<span id="eo-booking-discount"></span>' ); ?> | |
</td> | |
<td></td> | |
</tr> | |
<?php | |
//Enqueue scripts | |
wp_enqueue_script( 'eo_bulk_discount_script' ); | |
} | |
/** | |
* After a booking is made calculate the discount amount. | |
* | |
* Calculates & stores the discounted amount with the booking | |
* @param int $booking_id | |
*/ | |
function store_discount_amount( $booking_id ){ | |
//Ticket quantity and non-discounted price | |
$amount = floatval( eo_get_booking_meta( $booking_id, 'booking_amount', true ) ); | |
$ticket_quantity = floatval( eo_get_booking_meta( $booking_id, 'ticket_quantity', true ) ); | |
$discount_amount = 0; | |
//Calculate amount to discount, store it i $discount_amount | |
update_post_meta( $booking_id, '_eo_booking_bulk_discount_amount', $discount_amount ); | |
} | |
/** | |
* If a discount was used for the booking, removes this from the booking total | |
* @param float $amount The booking total (pre discount) | |
* @param int $booking_id | |
* @return float $amount The booking total (post discount) | |
*/ | |
function apply_discount( $amount, $booking_id ){ | |
$discount_amount = get_post_meta( $booking_id, '_eo_booking_bulk_discount_amount', true ); | |
if( $discount_amount && floatval( $discount_amount ) > 0 ){ | |
$amount = max( 0, $amount - floatval( $discount_amount ) ); | |
} | |
return $amount; | |
} | |
/** | |
* Add a 'Discount' row to bookee / admin emails | |
* | |
* @param string $rows HTML of rows to insert just prior to 'total' row | |
* @param int $booking_id Booking ID | |
* @return string HTML of rows to insert just prior to 'total' row | |
*/ | |
function add_to_email( $rows, $booking_id ){ | |
$discount_amount = get_post_meta( $booking_id, '_eo_booking_bulk_discount_amount', true ); | |
if( $discount_amount && floatval( $discount_amount ) > 0 ){ | |
$rows .= sprintf( | |
'<tr> <td>%s</td> <td>%s</td> <td></td> </tr>', | |
__( 'Discount', 'eventorganiserdc' ), | |
eo_format_price( floatval( $discount_amount ) ) | |
); | |
} | |
return $rows; | |
} | |
/** | |
* Adds the discount to PayPal's itemised cart. | |
* | |
* Sets the 'tax_cart' attribute of the cart. | |
* @param array $cart | |
* @param array $booking array | |
* @return array | |
*/ | |
function add_to_paypal_cart( $cart, $booking ){ | |
$discount_amount = eo_get_booking_meta( $booking['booking_id'], 'bulk_discount_amount', true ); | |
if( $discount_amount && floatval( $discount_amount ) > 0 ){ | |
$cart['discount_amount_cart'] = $discount_amount; | |
} | |
return $cart; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment