gistfile1.txt
· 3.0 KiB · Text
Raw
add_action('woocommerce_after_checkout_validation', 'restrict_shipping_counties_in_spain', 10, 2);
function restrict_shipping_counties_in_spain($data, $errors) {
// List of restricted counties
$restricted_counties = array('CE', 'ML', 'GC', 'TF');
// Get the shipping country and state from the checkout data
$shipping_country = isset($data['shipping_country']) ? $data['shipping_country'] : '';
$shipping_state = isset($data['shipping_state']) ? $data['shipping_state'] : '';
// Check if the shipping country is Spain and the state is in the restricted list
if ($shipping_country === 'ES' && in_array($shipping_state, $restricted_counties)) {
// Add an error to the checkout
$errors->add('validation', __('El envío a Ceuta, Melilla, Las Palmas y Santa Cruz de Tenerife no está permitido. Por favor, elige una región diferente.', 'woocommerce'));
}
}
add_action('wp_footer', 'disable_checkout_button_for_restricted_counties_script');
function disable_checkout_button_for_restricted_counties_script() {
if (is_checkout()) {
?>
<script type="text/javascript">
jQuery(function($){
var restrictedCounties = ['CE', 'ML', 'GC', 'TF'];
var $checkoutForm = $('form.checkout');
var $placeOrderButton = $('#place_order');
function validateCounty() {
var selectedCountry = $('select#shipping_country').val();
var selectedCounty = $('select#shipping_state').val();
if (selectedCountry === 'ES' && restrictedCounties.indexOf(selectedCounty) !== -1) {
$placeOrderButton.attr('disabled', 'disabled');
if ($('.shipping-county-error').length === 0) {
$placeOrderButton.before('<div class="woocommerce-error shipping-county-error"><?php _e('El envío a Ceuta, Melilla, Las Palmas y Santa Cruz de Tenerife no está permitido. Por favor, elige una región diferente.', 'woocommerce'); ?></div>');
}
} else {
$placeOrderButton.removeAttr('disabled');
$('.shipping-county-error').remove();
}
}
// Validate on load
validateCounty();
// Validate on country and county change
$('select#shipping_country, select#shipping_state').change(function(){
validateCounty();
});
// Validate on form submission
$checkoutForm.on('submit', function(e){
validateCounty();
if ($placeOrderButton.is(':disabled')) {
e.preventDefault();
$('html, body').animate({
scrollTop: ($checkoutForm.offset().top - 100)
}, 500);
}
});
});
</script>
<?php
}
}
1 | add_action('woocommerce_after_checkout_validation', 'restrict_shipping_counties_in_spain', 10, 2); |
2 | |
3 | function restrict_shipping_counties_in_spain($data, $errors) { |
4 | // List of restricted counties |
5 | $restricted_counties = array('CE', 'ML', 'GC', 'TF'); |
6 | |
7 | // Get the shipping country and state from the checkout data |
8 | $shipping_country = isset($data['shipping_country']) ? $data['shipping_country'] : ''; |
9 | $shipping_state = isset($data['shipping_state']) ? $data['shipping_state'] : ''; |
10 | |
11 | // Check if the shipping country is Spain and the state is in the restricted list |
12 | if ($shipping_country === 'ES' && in_array($shipping_state, $restricted_counties)) { |
13 | // Add an error to the checkout |
14 | $errors->add('validation', __('El envío a Ceuta, Melilla, Las Palmas y Santa Cruz de Tenerife no está permitido. Por favor, elige una región diferente.', 'woocommerce')); |
15 | } |
16 | } |
17 | |
18 | add_action('wp_footer', 'disable_checkout_button_for_restricted_counties_script'); |
19 | |
20 | function disable_checkout_button_for_restricted_counties_script() { |
21 | if (is_checkout()) { |
22 | ?> |
23 | <script type="text/javascript"> |
24 | jQuery(function($){ |
25 | var restrictedCounties = ['CE', 'ML', 'GC', 'TF']; |
26 | var $checkoutForm = $('form.checkout'); |
27 | var $placeOrderButton = $('#place_order'); |
28 | |
29 | function validateCounty() { |
30 | var selectedCountry = $('select#shipping_country').val(); |
31 | var selectedCounty = $('select#shipping_state').val(); |
32 | if (selectedCountry === 'ES' && restrictedCounties.indexOf(selectedCounty) !== -1) { |
33 | $placeOrderButton.attr('disabled', 'disabled'); |
34 | if ($('.shipping-county-error').length === 0) { |
35 | $placeOrderButton.before('<div class="woocommerce-error shipping-county-error"><?php _e('El envío a Ceuta, Melilla, Las Palmas y Santa Cruz de Tenerife no está permitido. Por favor, elige una región diferente.', 'woocommerce'); ?></div>'); |
36 | } |
37 | } else { |
38 | $placeOrderButton.removeAttr('disabled'); |
39 | $('.shipping-county-error').remove(); |
40 | } |
41 | } |
42 | |
43 | // Validate on load |
44 | validateCounty(); |
45 | |
46 | // Validate on country and county change |
47 | $('select#shipping_country, select#shipping_state').change(function(){ |
48 | validateCounty(); |
49 | }); |
50 | |
51 | // Validate on form submission |
52 | $checkoutForm.on('submit', function(e){ |
53 | validateCounty(); |
54 | if ($placeOrderButton.is(':disabled')) { |
55 | e.preventDefault(); |
56 | $('html, body').animate({ |
57 | scrollTop: ($checkoutForm.offset().top - 100) |
58 | }, 500); |
59 | } |
60 | }); |
61 | }); |
62 | </script> |
63 | <?php |
64 | } |
65 | } |
66 |