我正在尝试在我的functions.php文件中添加一个代码段,以遵守加利福尼亚的Prop65法规。结帐期间,我希望显示一个框,其中显示文本警告,并带有一个复选框,提示用户同意。仅当用户将送货地址标记为加利福尼亚时,才需要显示文本和复选框,否则应将其隐藏。我能够显示文本,但不确定如何在同一字段中添加复选框。这是我正在使用的代码,因此文本框仅根据特定条件显示:
我尝试为复选框按钮添加单独的操作,但是我不确定如何根据条件使其显示和隐藏。
// Part 1
// Add the message notification and place it over the billing section
// The "display:none" hides it by default
add_action( 'woocommerce_after_checkout_billing_form',
'bbloomer_echo_notice_shipping' );
function bbloomer_echo_notice_shipping() {
echo '<div class="shipping-notice woocommerce-info"
style="display:none"><img src="https://emartmedia.com/images/bw6pt.png"
/><strong>WARNING</strong> SOME PRODUCTS SOLD ON THIS STORE WEBSITE CAN
CONTAIN CHEMICALS KNOWN TO THE STATE OF CALIFORNIA TO CAUSE CANCER,
BIRTH DEFECTS OR OTHER REPRODUCTIVE HARM. <a
href="https://www.p65warnings.ca.gov" target="_blank">Details</a></p>
</div>';
}
// Part 2
// Show or hide message based on billing state
// The "display:none" hides it by default
add_action( 'woocommerce_before_checkout_form',
'bbloomer_show_notice_shipping' );
function bbloomer_show_notice_shipping(){
?>
<script>
jQuery(document).ready(function($){
// Set the state code (That will display the message)
var stateCode = 'CA';
$('select#billing_state').change(function(){
selectedstate = $('select#billing_state').val();
if( selectedstate == stateCode ){
$('.shipping-notice').show();
}
else {
$('.shipping-notice').hide();
}
});
});
</script>
<?php
}