Skip to content

Instantly share code, notes, and snippets.

@New0 New0/ds-input-coupon.php
Last active May 23, 2019

Embed
What would you like to do?
Draft for Direct Stripe input coupons field
<?php
/*
* Plugin Name: Direct Stripe input coupons
* Author: Nicolas Figueira
* Description: Allow users to use promotional coupons with Direct Stripe
* Version: 0.0.1
* Text Domain: ds-coupons
*
* This is not production ready
* It will add an input field for coupon with CSS ID button_with_coupon
* Apply any coupon ID inserted as the input value as a coupon disount for the subscription
* I recommend leaving the Error message option empty in the Global settings, this will show a coupon does not exist error when this is the case.
*/
/**
* Set script for Direct Stripe coupon
* We Insert the JS code that will read the input field value in the footer using wp_footer action hook
*
*/
add_action( 'wp_footer', function(){
?><script type="text/javascript">
jQuery(function($) {
//Get value of coupon input field
$("#button_with_coupon").on("click", function() {
var coupon = $(".input-coupon").val();
$.post(
ds_coupons_vars.ajaxurl,
{
'action': 'ds_set_coupon',
'coupon': coupon
}
);
});
});
</script>
<?php
}, 10 );
/**
* Add an input field before the button
* We use 'direct_stripe_before_button' action hook to insert the html input field(s) required
*/
add_action( 'direct_stripe_before_button', function( $button_id ) {
if( $button_id === 'button_with_coupon') {
?>
<div class="coupon-input-field">
<label><?php _e('Do you have a coupon ?', 'ds-coupons'); ?></label>
<input type="text" class="input-coupon"></input>
<span class="ds-coupons-answer" style="display:none;"></span>
</div>
<?php
}
});
/**
* SET Coupon value as a transient for process
* The ds_set_coupon PHP function is triggered by WordPress ajax hooks
* in our case we called the wp ajax url with the action 'ds_set_coupon' in our wp_footer hook that printed the JS
* and ajax hooks are calling php function with the same name ( could have been a different name )
*/
add_action( 'wp_ajax_ds_set_coupon', 'ds_set_coupon' );
add_action( 'wp_ajax_nopriv_ds_set_coupon', 'ds_set_coupon' );
function ds_set_coupon() {
$coupon = isset($_POST['coupon']) ? $_POST['coupon'] : '';
if( isset( $coupon ) ) {
set_transient("ds_coupon_value", $coupon );
}
wp_die();
}
/**
* Set the coupon value in process Stripe API data
*
* We use 'direct_stripe_subscription_data' filter hook to add the coupon value stored as a transient to the data sent to Stripe API
*/
add_filter( 'direct_stripe_subscription_data', function( $subscriptiondata, $user, $token, $button_id, $amount, $coupon ) {
if( $button_id === 'button_with_coupon' ) {
$coupon_input = get_transient('ds_coupon_value');
if( isset( $coupon_input ) && $coupon_input !== false && !empty( $coupon_input ) ) {
$subscriptiondata['coupon'] = $coupon_input;
}
}
return $subscriptiondata;
}, 10, 6 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.