Last active
March 23, 2017 07:01
-
-
Save panoslyrakis/42095af56986221eb32c8ae94747a114 to your computer and use it in GitHub Desktop.
Adds the coupon price of appointments in gcal description. Requires MarketPress
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: Add coupon price of appointments in gcal | |
* Version: 1.2.0 | |
* Description: Adds the coupon price of appointments in gcal description | |
* Author: Panos Lyrakis (WPMU DEV) | |
* Author URI: https://premium.wpmudev.org/profile/panoskatws | |
* Plugin URI: https://premium.wpmudev.org/project/appointments-plus/ | |
* License: GPL2 | |
* License URI: https://www.gnu.org/licenses/gpl-2.0.html | |
*/ | |
/** | |
* Instead of adding the coupon code and dicounted price in the of the GCal event description, there are new placeholders | |
* that can be used. | |
* For displaying the coupon code use the placeholder COUPON_CODE | |
* For displaying the discounted price use the placeholder DISCOUNTED_PRICE | |
* Both placeholders should be added in the admin > Appointments > Settings > Google Calendar tab, in the "Event description" field | |
* The output already contains the description so | |
* COUPON_CODE will output : "Coupon Code: THE_CODE" and | |
* DISCOUNTED_PRICE will output : "Discounted Price: the_discounted_price" | |
* So in Event description field you only need to add the placeholder. This is helpfull in the case no coupon is used, | |
* in which case it will not add any text. This can be changed with filters | |
* WPMUDEV_APPS_Coupon_To_GCal/coupon_str for coupon code and | |
* WPMUDEV_APPS_Coupon_To_GCal/discounted_price_str for discounted price | |
* You can also edit lines 149 and 152 accordingly | |
*/ | |
if( ! class_exists( 'WPMUDEV_APPS_Coupon_To_GCal ') ){ | |
class WPMUDEV_APPS_Coupon_To_GCal{ | |
private static $_instance = null; | |
var $COUPON_PLACEHOLDER, $DISCOUNTED_PRICE_PLACEHOLDER; | |
public static function get_instance() { | |
if ( is_null( self::$_instance ) ) { | |
self::$_instance = new WPMUDEV_APPS_Coupon_To_GCal(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
$this->COUPON_PLACEHOLDER = apply_filters( 'WPMUDEV_APPS_Coupon_To_GCal/COUPON_PLACEHOLDER', 'COUPON_CODE' ); | |
$this->DISCOUNTED_PRICE_PLACEHOLDER = apply_filters( 'WPMUDEV_APPS_Coupon_To_GCal/DISCOUNTED_PRICE_PLACEHOLDER', 'DISCOUNTED_PRICE' ); | |
add_action( 'mp_order/new_order', array( $this, 'app_order' ), 10, 1 ); | |
add_filter( 'app-gcal-set_description', array( $this, 'set_description' ), 10, 2 ); | |
add_filter( 'app-appointment-appointment_created', array( $this, 'appointment_created' ), 20, 7 ); | |
} | |
public function appointment_created( $additional, $insert_id, $post_id, $service, $worker, $start, $end ){ | |
//In mp addon bridge, the 'mp' index isset as 1 on hook "app-appointment-appointment_created" with priority 10 - we use 20 so it should be set. | |
//Lets update the variation's meta with the app id so we can use in set_description(). | |
//Usefull with Stripe, where the "app-gcal-set_description" is reached before "mp_order/new_order" | |
if( is_array( $additional ) && isset( $additional['mp'] ) && $additional['mp'] == 1 ){ | |
if( isset( $additional['variation'] ) && is_numeric( $additional['variation'] ) ){ | |
update_post_meta( $additional['variation'], 'app_id', $insert_id ); | |
} | |
} | |
} | |
public function app_order( $order ){ | |
global $wpdb; | |
if( ! function_exists( 'appointments_get_table' ) ){ | |
return; | |
} | |
$apps_meta_table = appointments_get_table( 'appmeta' ); | |
$cart_info = is_object( $order ) && is_callable( array( $order, 'get_cart' ) ) | |
? $order->get_cart()->get_items() | |
: ( isset( $order->mp_cart_info ) ? $order->mp_cart_info : array() ); | |
if( !is_object( $cart_info ) || epmty( $cart_info ) ){ | |
global $mp_cart; | |
$cart_info = $mp_cart->get_items(); | |
} | |
$variation_type = MP_Product::get_variations_post_type(); | |
$appointment_ids = array(); | |
if ( is_array( $cart_info ) && count( $cart_info ) ) { | |
foreach ( $cart_info as $cart_id => $count ) { | |
$variation = get_post( $cart_id ); | |
if ( ! empty( $variation->post_type ) && $variation_type === $variation->post_type && self::is_app_mp_page( $variation->post_parent ) ) { | |
$app_id = MP_Product::get_variation_meta( $variation->ID, 'name' ); | |
if ( is_numeric( $app_id ) ) { | |
$appointment_ids[] = $app_id; | |
} | |
} | |
} | |
} | |
if( ! empty( $appointment_ids ) ){ | |
foreach( $appointment_ids as $appointment_id ){ | |
$wpdb->insert( $apps_meta_table, | |
array( | |
'app_appointment_id' => $appointment_id , | |
'meta_key' => 'mp_order_id', | |
'meta_value' => $order->ID | |
), | |
array( '%d', '%s', '%s' ) | |
); | |
} | |
} | |
} | |
public function set_description( $description, $app ){ | |
global $wpdb; | |
if( ! function_exists( 'appointments_get_table' ) ){ | |
return $description; | |
} | |
$replacement = array( | |
$this->COUPON_PLACEHOLDER => '', | |
$this->DISCOUNTED_PRICE_PLACEHOLDER => '' | |
); | |
$coupon_str = ''; | |
$discounted_price_str = ''; | |
$order_coupon_meta = array(); | |
$meta_table = appointments_get_table( 'appmeta' ); | |
$order_id = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM {$meta_table} WHERE app_appointment_id = %d AND meta_key='mp_order_id' ", $app->ID )); | |
/* | |
* Order id in apps meta table | |
*/ | |
if( $order_id && is_numeric( $order_id ) ){ | |
$order = new MP_Order( $order_id ); | |
$cart = $order->get_meta( 'mp_cart_info' ); | |
$order_coupon_meta = get_post_meta( $order->ID, 'mp_discount_info', true ); | |
if( is_array( $order_coupon_meta ) && !empty( $order_coupon_meta ) ){ | |
foreach( $order_coupon_meta as $coupon=>$discount ){ | |
$coupon_obj = get_page_by_title( $coupon, OBJECT, 'mp_coupon' ); | |
$coupon_meta = get_post_meta( $coupon_obj->ID ); | |
$coupon_discount = $coupon_meta['discount'][0]; | |
$is_percentage = substr($coupon_discount, -1) == '%' ? true : false; | |
$app_price = $app->price; | |
if( $is_percentage ){ | |
$app_price = number_format( $app_price - ( $app_price * $coupon_discount / 100 ), 2 ); | |
} | |
else{ | |
$app_price = $app_price - $coupon_discount; | |
} | |
} | |
$coupon_str = 'Coupon Code: '. $coupon_obj->post_title; | |
$coupon_str = apply_filters( 'WPMUDEV_APPS_Coupon_To_GCal/coupon_str', $coupon_str, $coupon_obj->post_title ); | |
$discounted_price_str = 'Discounted Price: '. $app_price; | |
$discounted_price_str = apply_filters( 'WPMUDEV_APPS_Coupon_To_GCal/discounted_price_str', $discounted_price_str, $app_price ); | |
} | |
$replacement = array( | |
$this->COUPON_PLACEHOLDER => $coupon_str, | |
$this->DISCOUNTED_PRICE_PLACEHOLDER => $discounted_price_str | |
); | |
foreach( $replacement as $macro => $repl ) { | |
$description = preg_replace('/\b' . preg_quote($macro, '/') . '\b/U', $repl, $description); | |
} | |
return $description; | |
} | |
/* | |
* No Order id in apps meta table, but lets check if app id in cart and if there is any coupon applied | |
*/ | |
else{ | |
$coupons_applied = array(); | |
$cart_items = mp_cart()->get_items(); | |
if ( is_multisite() ) { | |
$blog_id = mp_cart()->get_blog_id(); | |
$coupons_applied = mp_get_session_value( "mp_cart_coupons->{$blog_id}", array() ); | |
} else { | |
$coupons_applied = mp_get_session_value( 'mp_cart_coupons', array() ); | |
} | |
if( ! empty( $cart_items ) && ! empty( $coupons_applied ) ){ | |
foreach( $cart_items as $item_id => $quantity ){ | |
$app_id = get_post_meta( $item_id, 'app_id', true ); | |
if( $app->ID == $app_id ){ | |
$app_price = $app_price_discounted = $app->price; | |
foreach( $coupons_applied as $key => $coupon_id ){ | |
$coupon = new MP_Coupon( $coupon_id ); | |
$discount = get_post_meta( $coupon->ID, 'discount', true ); | |
if ( strpos( $discount, '%' ) !== false ){ | |
$discount = (float)str_replace( '%', '', $discount ); | |
$app_price_discounted = number_format( $app_price_discounted - ( $app_price_discounted*$discount/100 ), 2 ); | |
} | |
else{ | |
$app_price_discounted = number_format( $app_price_discounted - $discount, 2 ); | |
} | |
} | |
$coupon_str = 'Coupon Code: '. $coupon->post_title; | |
$coupon_str = apply_filters( 'WPMUDEV_APPS_Coupon_To_GCal/coupon_str', $coupon_str, $coupon->post_title ); | |
$discounted_price_str = 'Discounted Price: '. $app_price_discounted; | |
$discounted_price_str = apply_filters( 'WPMUDEV_APPS_Coupon_To_GCal/discounted_price_str', $discounted_price_str, $app_price_discounted ); | |
$replacement = array( | |
$this->COUPON_PLACEHOLDER => $coupon_str, | |
$this->DISCOUNTED_PRICE_PLACEHOLDER => $discounted_price_str | |
); | |
foreach( $replacement as $macro => $repl ) { | |
$description = preg_replace('/\b' . preg_quote($macro, '/') . '\b/U', $repl, $description); | |
} | |
return $description; | |
} | |
} | |
} | |
} | |
/* | |
*If no coupon, remove the coupon placeholders | |
*/ | |
foreach( $replacement as $macro => $repl ) { | |
$description = preg_replace('/\b' . preg_quote($macro, '/') . '\b/U', '', $description); | |
} | |
return $description; | |
} | |
public static function is_app_mp_page ($product) { | |
$product = get_post($product); | |
$result = is_object( $product ) && strpos( $product->post_content, '[app_' ) !== false | |
? true | |
: false | |
; | |
// Maybe required for templates | |
return apply_filters( 'app_is_mp_page', $result, $product ); | |
} | |
} | |
add_action( 'plugins_loaded', function(){ | |
$GLOBALS['WPMUDEV_APPS_Coupon_To_GCal'] = WPMUDEV_APPS_Coupon_To_GCal::get_instance(); | |
} ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment