Last active 1716197933

Malin's Avatar Malin revised this gist 1716197933. Go to revision

1 file changed, 2 insertions

gistfile1.txt renamed to block-purchse-from-CE-ML-GC-TF.php

@@ -1,3 +1,5 @@
1 + // for easycut.es
2 +
1 3 add_action('woocommerce_after_checkout_validation', 'restrict_shipping_counties_in_spain', 10, 2);
2 4
3 5 function restrict_shipping_counties_in_spain($data, $errors) {

Malin's Avatar Malin revised this gist 1716197896. Go to revision

1 file changed, 65 insertions

gistfile1.txt(file created)

@@ -0,0 +1,65 @@
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 + }
Newer Older