block-purchse-from-CE-ML-GC-TF.php
· 3.0 KiB · PHP
Orginalformat
// for easycut.es
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 | // for easycut.es |
2 | |
3 | add_action('woocommerce_after_checkout_validation', 'restrict_shipping_counties_in_spain', 10, 2); |
4 | |
5 | function restrict_shipping_counties_in_spain($data, $errors) { |
6 | // List of restricted counties |
7 | $restricted_counties = array('CE', 'ML', 'GC', 'TF'); |
8 | |
9 | // Get the shipping country and state from the checkout data |
10 | $shipping_country = isset($data['shipping_country']) ? $data['shipping_country'] : ''; |
11 | $shipping_state = isset($data['shipping_state']) ? $data['shipping_state'] : ''; |
12 | |
13 | // Check if the shipping country is Spain and the state is in the restricted list |
14 | if ($shipping_country === 'ES' && in_array($shipping_state, $restricted_counties)) { |
15 | // Add an error to the checkout |
16 | $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')); |
17 | } |
18 | } |
19 | |
20 | add_action('wp_footer', 'disable_checkout_button_for_restricted_counties_script'); |
21 | |
22 | function disable_checkout_button_for_restricted_counties_script() { |
23 | if (is_checkout()) { |
24 | ?> |
25 | <script type="text/javascript"> |
26 | jQuery(function($){ |
27 | var restrictedCounties = ['CE', 'ML', 'GC', 'TF']; |
28 | var $checkoutForm = $('form.checkout'); |
29 | var $placeOrderButton = $('#place_order'); |
30 | |
31 | function validateCounty() { |
32 | var selectedCountry = $('select#shipping_country').val(); |
33 | var selectedCounty = $('select#shipping_state').val(); |
34 | if (selectedCountry === 'ES' && restrictedCounties.indexOf(selectedCounty) !== -1) { |
35 | $placeOrderButton.attr('disabled', 'disabled'); |
36 | if ($('.shipping-county-error').length === 0) { |
37 | $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>'); |
38 | } |
39 | } else { |
40 | $placeOrderButton.removeAttr('disabled'); |
41 | $('.shipping-county-error').remove(); |
42 | } |
43 | } |
44 | |
45 | // Validate on load |
46 | validateCounty(); |
47 | |
48 | // Validate on country and county change |
49 | $('select#shipping_country, select#shipping_state').change(function(){ |
50 | validateCounty(); |
51 | }); |
52 | |
53 | // Validate on form submission |
54 | $checkoutForm.on('submit', function(e){ |
55 | validateCounty(); |
56 | if ($placeOrderButton.is(':disabled')) { |
57 | e.preventDefault(); |
58 | $('html, body').animate({ |
59 | scrollTop: ($checkoutForm.offset().top - 100) |
60 | }, 500); |
61 | } |
62 | }); |
63 | }); |
64 | </script> |
65 | <?php |
66 | } |
67 | } |
68 |