我尝试在选择某些州作为计费州时触发短信。我显示了该消息,但它只会显示一种状态。当我尝试将其他人添加到“var stateCode”中时,我没有运气。这是我到目前为止所拥有的。我应该如何添加其他状态来触发消息显示?
//Sales Tax Warning Script
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
style="font-size:9pt; padding:10px 10px; border:1px solid #d1d18e; background-
color:#ffffb8;"><img src="https://emartmedia.com/images/bw6pt.png" /><strong>
</strong> We are now required to collect sales tax in your state. To learn more
and see if you are eligible for exemption, click here. <a
href="https://www.p65warnings.ca.gov" target="_blank">Details</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($){
// Set the shipping state 2 char code (This selection will fire the warning message display)
// Fires secondary if shipping state is set to California
var stateCode = 'WI';
$('select#billing_state').change(function(){
selectedState = $('select#billing_state').val();
if( selectedState == stateCode ){
$('.prop65-warning').show();
}
else {
$('.prop65-warning').hide();
}
});
});
</script>
<script>
jQuery(document).ready(function($){
// Set the billing state 2 char code (This selection will fire the warning message display)
// Fires initially if billing state is California
var stateCode = 'WI';
$('select#billing_state').change(function(){
selectedState = $('select#billing_state').val();
if( selectedState == stateCode ){
$('.prop65-warning').show();
}
else {
$('.prop65-warning').hide()
我建议将状态添加到数组中,然后使用
includes()
方法来查看该值是否与其中一个状态匹配。它看起来像这样:
var stateCode = ['WI', 'NY', 'FL', 'CA'];
$('select#billing_state').change(function(){
selectedState = $('select#billing_state').val();
if( stateCode.includes(selectedState) ){
$('.prop65-warning').show();
}
else {
$('.prop65-warning').hide();
}
});
你让这个代码工作了吗?我尝试在我的functions.php 中使用它,但它导致了一个严重错误。基本上需要相同的功能(根据运输状态显示消息)