Last active
February 25, 2021 20:03
-
-
Save razorfrog/989a14c97317de665e661a96bffb765b to your computer and use it in GitHub Desktop.
California Prop 65 Conditional Text in WooCommerce
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 | |
/////////////////////////////////////////////////////////////////////////////////////////////////////// | |
// Show Prop 65 Message Upon State of California Shipping Selection During WooCommerce Checkout | |
// https://razorfrog.com/california-prop-65-conditional-text-in-woocommerce/ | |
/////////////////////////////////////////////////////////////////////////////////////////////////////// | |
// First Step | |
// Create Prop 65 warning message and hook it into WooCommerce After Order Review | |
// "display:none" we have hidden the warning message by default | |
add_action( 'woocommerce_checkout_before_order_review', 'show_prop_message' ); | |
function show_prop_message() { | |
echo '<div class="prop65-warning woocommerce-warning" style="display:none;"><p><strong>California Proposition 65 WARNING:</strong> Products may contain lead and other chemicals known to the State of California to cause birth defects or other reproductive harm. <a href="https://www.p65warnings.ca.gov" target="_blank">Learn more</a>.</p></div>'; | |
} | |
// Second Step | |
// Show or hide warning message based on billing and shipping state | |
// First trigger is fired on billing state selection in case the "Ship to a different address" checkbox is unselected | |
// Second trigger is fired if the "Ship to a different address" checkbox is ticked | |
// Initally the "display:none" hides the warning message by default | |
add_action( 'woocommerce_after_checkout_form', 'show_warning_message' ); | |
function show_warning_message(){ | |
?> | |
<script> | |
jQuery(document).ready(function($){ | |
// On page load | |
if($(this).is(":not(:checked)")) { | |
if( $('select#billing_state').val() == 'CA' ) $('.prop65-warning').show(); | |
} else { | |
if( $('select#shipping_state').val() == 'CA' ) $('.prop65-warning').show(); | |
} | |
// On "State" dropdown change | |
$('select#billing_state').change(function() { | |
if($('input#ship-to-different-address-checkbox').is(":not(:checked)")) { | |
if( $('select#billing_state').val() == 'CA' ) $('.prop65-warning').show(); | |
else $('.prop65-warning').hide(); | |
} | |
}); | |
$('select#shipping_state').change(function() { | |
if($('input#ship-to-different-address-checkbox').is(":checked")) { | |
if( $('select#shipping_state').val() == 'CA' ) $('.prop65-warning').show(); | |
else $('.prop65-warning').hide(); | |
} | |
}); | |
// On "Ship to a different address?" checkbox change | |
$('input#ship-to-different-address-checkbox').click(function(){ | |
if($(this).is(":not(:checked)")) { | |
if( $('select#billing_state').val() == 'CA' ) $('.prop65-warning').show(); | |
else $('.prop65-warning').hide(); | |
} else { | |
if( $('select#shipping_state').val() == 'CA' ) $('.prop65-warning').show(); | |
else $('.prop65-warning').hide(); | |
} | |
}); | |
}); | |
</script> | |
<?php | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment