Last active
January 22, 2018 13:48
-
-
Save felixarntz/f9ada0ffb92d6b2e880c7dd0f826b486 to your computer and use it in GitHub Desktop.
Mini plugin to allow specifying no default gateway in Easy Digital Downloads.
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 | |
/** | |
* Mini plugin to allow specifying no default gateway in Easy Digital Downloads. | |
* | |
* @package EDDNoDefaultGateway | |
* @since 1.0.0 | |
* | |
* @wordpress-plugin | |
* Plugin Name: EDD No Default Gateway | |
* Plugin URI: https://gist.github.com/felixarntz/f9ada0ffb92d6b2e880c7dd0f826b486 | |
* Description: Mini plugin to allow specifying no default gateway in Easy Digital Downloads. | |
* Version: 1.0.0 | |
* Author: Felix Arntz | |
* Author URI: https://leaves-and-love.net | |
* License: GNU General Public License v3 | |
* License URI: http://www.gnu.org/licenses/gpl-3.0.html | |
*/ | |
function edd_ndg_add_default_gateway_option_none( $gateway_settings ) { | |
if ( ! empty( $gateway_settings['main']['default_gateway']['options'] ) ) { | |
// Add an option to select 'None' in the default gateway dropdown. | |
$gateway_settings['main']['default_gateway']['options'] = array_merge( array( | |
'' => array( | |
'admin_label' => __( 'None', 'easy-digital-downloads' ), | |
'checkout_label' => __( 'None', 'easy-digital-downloads' ), | |
), | |
), $gateway_settings['main']['default_gateway']['options'] ); | |
} | |
return $gateway_settings; | |
} | |
add_filter( 'edd_settings_gateways', 'edd_ndg_add_default_gateway_option_none' ); | |
function edd_ndg_allow_no_default_gateway( $gateway ) { | |
$original_option = edd_get_option( 'default_gateway', '' ); | |
// Allow an empty option value to be valid. | |
if ( empty( $original_option ) ) { | |
return $original_option; | |
} | |
return $gateway; | |
} | |
add_filter( 'edd_default_gateway', 'edd_ndg_allow_no_default_gateway' ); | |
function edd_ndg_maybe_prevent_default_gateway_load( $script_vars ) { | |
if ( edd_is_checkout() ) { | |
$chosen_gateway = edd_get_chosen_gateway(); | |
// This variable needs to be set to 0 so that no default gateway loads in JS. | |
if ( empty( $chosen_gateway ) ) { | |
$script_vars['is_checkout'] = '0'; | |
} | |
} | |
return $script_vars; | |
} | |
add_filter( 'edd_ajax_script_vars', 'edd_ndg_maybe_prevent_default_gateway_load' ); | |
function edd_ndg_maybe_print_default_gateway_free_purchase_fix() { | |
if ( ! edd_is_checkout() ) { | |
return; | |
} | |
$chosen_gateway = edd_get_chosen_gateway(); | |
if ( ! empty( $chosen_gateway ) ) { | |
return; | |
} | |
// The following script ensures that the purchase form is manually loaded when a purchase is free. | |
// Without this, it is impossible to complete such a purchase. | |
?> | |
<script type="text/javascript"> | |
( function() { | |
var is_free = false; | |
jQuery( document.body ).on( 'edd_discount_applied', function( e, discount ) { | |
if ( '0.00' == discount.total_plain ) { | |
is_free = true; | |
if ( window.edd_load_gateway ) { | |
window.edd_load_gateway( 'manual' ); | |
} | |
} else if ( is_free ) { | |
is_free = false; | |
jQuery( '#edd_purchase_form_wrap' ).html( '' ); | |
} | |
}); | |
})(); | |
</script> | |
<?php | |
} | |
add_action( 'wp_footer', 'edd_ndg_maybe_print_default_gateway_free_purchase_fix', 9999 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment