Created
February 4, 2019 15:32
-
-
Save CrispDev/686ea97d80cc0e32592dfd1d223a1455 to your computer and use it in GitHub Desktop.
Display a custom discounted price based on Selected Autoship frequency & type ( Simple Product Type Example )
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
jQuery(function ($) { | |
/** | |
* Displays a custom discounted price based on | |
* Selected Autoship frequency & type. | |
*/ | |
function xx_custom_autoship_schedule_options_price($) { | |
// Add the discount data per frequency and type as a variable loaded into the page. | |
var option_price_discount_lookup = { | |
'Days':.75, | |
'Weeks':.5, | |
'Months':.25, | |
}; | |
$('.autoship-schedule-options').each(function () { | |
var $container = $(this); | |
var $product = $(this).closest(".product"); | |
var $autoship_yes_radio = $container.find('.autoship-yes-radio'); | |
var $autoship_no_radio = $container.find('.autoship-yes-radio'); | |
// The price listed next to autoship and save. | |
var $autoship_checkout_price_html = $product.find('p.price'), | |
$autoship_checkout_original_price_html = $autoship_checkout_price_html.html(), | |
$original_price = parseFloat($('.woocommerce-Price-amount amount').text().replace(/ /g ,'').replace(/€/g ,'').replace(/,/g ,'.')); | |
function xx_displayselectedfrequencyPrice() { | |
// Get the value of the selected option. | |
var option = JSON.parse($container.find('.autoship-frequency-select').val()); | |
if ( typeof option === undefined || null === option ) | |
return; | |
// Simple example that pulls the discount percentage from the type. | |
var discount = option_price_discount_lookup[option.frequency_type]; | |
// Get the original price ( variations have price in the form data, simple you'll need to add it or get it. ) | |
var discount_amount = ( 20.00 - ( 20.00 * discount ) ).toFixed(2); | |
// Add Code to adjust displayed prices and discounts in the following locations: | |
var new_price = '<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span><span class="woocommerce-Price-priceAmount">' + discount_amount + '</span></span>'; | |
$autoship_checkout_price_html.html( new_price ); | |
} | |
// Trigger custom price display when new option is selected and | |
// The autoship yes option is clicked. | |
$container.find('.autoship-frequency-select').on( 'change' ,function () { | |
xx_displayselectedfrequencyPrice(); | |
}); | |
$container.find('.autoship-yes-radio').on( 'click' , function () { | |
xx_displayselectedfrequencyPrice(); | |
}); | |
$container.find('.autoship-no-radio').on( 'click' , function () { | |
// Reset the price to non-autoship custom price. | |
$autoship_checkout_price_html.html( $autoship_checkout_original_price_html ); | |
}); | |
}); | |
} | |
xx_custom_autoship_schedule_options_price($); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment