Does anybody have any idea how to handle such scenario? I want to display an notice message and disable place order button in the checkout page when non XX country has been selected and cart contains a particular product (not deliverable to foreign country).
I could do with the Jquery, but cannot access the Place order button as its form is being refreshed via AJAX everytime I change billing address. This is what I have come so far:
add_action( 'woocommerce_checkout_before_customer_details', 'display_shipping_notice' );
function display_shipping_notice() {
echo '<div class="shipping-notice woocommerce-error" role="alert" style="display:none">We cannot ship this product to your country. Please remove it from the cart to continue!</div>';
}
add_action( 'woocommerce_after_checkout_form', 'show_shipping_notice_js' );
function show_shipping_notice_js(){
?>
<script>
jQuery(function($){
var countryCode = 'LV', // Set the country code (That will display the message)
countryField = 'select#billing_country'; // The Field selector to target
function showHideShippingNotice( countryCode, countryField ){
if( $(countryField).val() !== countryCode && $('.shop_table tr').hasClass('id-27733')){
$('.shipping-notice').show();
$('.woocommerce-checkout-payment').hide();
}
else {
$('.shipping-notice').hide();
$('.woocommerce-checkout-payment').show();
}
}
// On Ready (after DOM is loaded)
showHideShippingNotice( countryCode, countryField );
// On billing country change (Live event)
$('form.checkout').on('change', countryField, function() {
showHideShippingNotice( countryCode, countryField );
});
});
</script>
<?php
}
function cart_item_class( $class, $values, $values_key ) {
if ( isset( $values[ 'product_id' ] ) ) {
$class .= ' id-' . $values[ 'product_id' ];
}
return $class;
}
add_filter( 'woocommerce_cart_item_class', 'cart_item_class', 10, 3 );