Last active
May 22, 2019 03:27
-
-
Save wpmudev-sls/8b941173db703e89806c92a6c647bf43 to your computer and use it in GitHub Desktop.
[Marketpress] Adds TOS / Terms of Service / Terms and conditions on checkout page
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: MarketPress Terms of Service | |
Plugin URI: https://premium.wpmudev.org/ | |
Description: Adds Terms of Service on MarketPress checkout page | |
Author: Panos Lyrakis @ WPMUDEV | |
Author URI: https://premium.wpmudev.org/ | |
License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_MP_TOS' ) ) { | |
class WPMUDEV_MP_TOS { | |
private static $_instance = null; | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_MP_TOS(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
if( isset( $_GET[ 'token' ] ) && isset( $_GET[ 'PayerID' ] ) ){ | |
return; | |
} | |
//add_filter( 'mp_cart/after_cart_html', array( $this, 'tos_after_cart' ), 40, 3 ); | |
add_filter( 'mp_cart/cart_meta', array( $this, 'tos_after_cart' ), 40, 2 ); | |
add_action( 'wp_footer', array( $this, 'tos_js' ), 99999 ); | |
} | |
public function tos_after_cart( $html, $cart ){ | |
if ( $payment_method = mp_get_post_value( 'payment_method' ) ) { | |
$html .= $this->tos_checkbox(); | |
} | |
return $html; | |
} | |
public function tos_checkbox( $additional_class = '' ){ | |
return '<div class="mp_checkout_field mp_checkout_checkbox '.$additional_class.'"> | |
<label class="mp_form_label"> | |
<input class="mp_form_checkbox" type="checkbox" id="mp-tos" /> I agree with the <a href="#">Terms of Service.</a> | |
</label> | |
</div>'; | |
} | |
public function tos_js(){ | |
if( ! function_exists( 'mp_is_shop_page' ) || ! mp_is_shop_page( 'checkout' ) ){ | |
return; | |
} | |
?> | |
<script type="text/javascript"> | |
(function($){ | |
$(document).ready(function(){ | |
$( document ).ajaxComplete(function( event, xhr, settings ) { | |
if( $('#mp-tos').length <= 0 && $( '#mp-cart-resume' ).length ){ | |
var tos_option = '<?php echo trim(preg_replace('/\s+/', ' ', $this->tos_checkbox())); ?>'; | |
$( '#mp-cart-resume' ).append( tos_option ); | |
} | |
}); | |
$( document ).on( 'click', '#mp-checkout-section-order-review-payment .mp_button-checkout', function( e ){ | |
if(!$('#mp-tos').is(':checked')) { | |
e.preventDefault(); | |
alert("You need to agree with terms and conditions"); | |
return false; | |
} | |
return true; | |
}); | |
}); | |
})(jQuery); | |
</script> | |
<?php | |
} | |
} | |
add_action( 'plugins_loaded', function(){ | |
$GLOBALS['WPMUDEV_MP_TOS'] = WPMUDEV_MP_TOS::get_instance(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment