Skip to content

Instantly share code, notes, and snippets.

@applehat
Forked from ODiesel/custom_discount_code.js
Last active June 27, 2020 02:37
Show Gist options
  • Save applehat/c8cc276d16b5c4f3d4188d1e0ef5509d to your computer and use it in GitHub Desktop.
Save applehat/c8cc276d16b5c4f3d4188d1e0ef5509d to your computer and use it in GitHub Desktop.
jQuery(document).on("click", "#discountPopupButton" , function(e) {
e.preventDefault();
alert("hello world");
jQuery('#testMessage' ).text('test');//elementor-heading-title
jQuery('.elementor-heading-title' ).text('testing');
jQuery( '.elementor-price-table__integer-part' ).text("nother test");
})
/*
$(document).on("click", "#discountPopupButton" , function() {
//jQuery( '#testButton' ).click(function() { // Binds this function to run when HTML element with id=discount is clicked
alert("hello world");
jQuery('#testMessage' ).text('test');//elementor-heading-title
jQuery('.elementor-heading-title' ).text('testing');
jQuery( '.elementor-price-table__integer-part' ).text("nother test");
});
*/
jQuery( '#discount' ).click(function() { // Binds this function to run when HTML element with id=discount is clicked
var dcode = jQuery('#form-field-promocode').val(); // Sets variable dcode to the value entered in element with id=promocode
jQuery.ajax({ // Makes ajax call to Wordpress ajax url
url : checkDiscountCode.ajax_url, // checkDiscountCode is included in wp_localize_script in php file
type : 'post', // this is necessary for users who are not logged in
dataType: 'json', // set the expected server response type to json
data : {
action : 'check_discount_code', // action value must match value defined in add_action in php file
dcode : dcode // posts the dcode variable as $_POST['dcode']
},
success : function( response ) { // the response variable contains the entire json response from the server
var discount_pct = Number(response.discount); // adding .discount returns the value of that key in the array
//jQuery('#message' ).text(response.message); // insert the message into the empty span
if ( discount_pct > 0) { // Only update the prices if the discount is above 0
jQuery( '.elementor-price-table__integer-part' ).each(function( i ) { // Iterate over all the prices
var price = jQuery( this ).text(); // Get the current price
var newprice = '';
if(price == '17-22')
{
newprice = '12';
}
if(price == '50-55')
{
newprice = '30';
}
if(price == '95-100')
{
newprice = '60';
}
//var discount = -(price * discount_pct/100); // Calculate the discount amount
//var total = Number(price) + Number(discount); // Subtract the discount from original price
//jQuery( this ).text(total.toFixed(0)); // Display the discounted price as a whole number
jQuery( this ).text(newprice); // Display the discounted price as a whole number
});
//jQuery( '#discount' ).off(); // Disable the button once a discount has been applied
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment