Last active
March 17, 2022 14:28
-
-
Save rynaldos/33fbc673bc9fe4632ad9a3fb1dfc2cd8 to your computer and use it in GitHub Desktop.
WooCommerce conditional shipping notice based on product and billing state
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
add_action( 'woocommerce_before_checkout_billing_form', 'rs_conditional_shipping_notice' ); | |
function rs_conditional_shipping_notice() { | |
echo '<div class="shipping-notice woocommerce-info" style="display:none">The x product is from supplier x and you can find it on this <a href="#"> link</a> </div>'; | |
} | |
add_action( 'woocommerce_after_checkout_form', 'rs_conditional_show_notice_shipping' ); | |
function rs_conditional_show_notice_shipping(){ | |
$product_id = 37; // can be extended to include more IDs. use array | |
$product_cart_id = WC()->cart->generate_cart_id( $product_id ); | |
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id ); | |
if ( $in_cart ) { | |
wc_enqueue_js( " | |
var stateCode = 'CA'; | |
selectedState = $('select#billing_state').val(); | |
function toggle_upsell( selectedState ) { | |
if( stateCode.includes(selectedState) ){ | |
$('.shipping-notice').show(); | |
} | |
else { | |
$('.shipping-notice').hide(); | |
} | |
} | |
toggle_upsell( selectedState ); | |
$('select#billing_state').change(function(){ | |
toggle_upsell( this.value ); | |
}); | |
" ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment