Zuletzt aktiv 1716197933

block-purchse-from-CE-ML-GC-TF.php Orginalformat
1// for easycut.es
2
3add_action('woocommerce_after_checkout_validation', 'restrict_shipping_counties_in_spain', 10, 2);
4
5function 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
20add_action('wp_footer', 'disable_checkout_button_for_restricted_counties_script');
21
22function 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