79820897

Date: 2025-11-15 14:44:11
Score: 1
Natty:
Report link

This works great for UK counties, but if I select Ireland as the Country for shipping I get an error saying:

Oops!
Unexpected error in: woocommerce/checkout-shipping-address-block
Error: Node.removeChild: The node to be removed is not a child of this node

The code I tried was:

add_action( 'wp_footer', 'ag_force_ni_shipping_counties_js', 20 );

function ag_force_ni_shipping_counties_js() {
  if ( ! function_exists( 'is_checkout' ) || ! is_checkout() ) {
      return;
  }
  ?>
  <script>
      (function () {
          // Our allowed counties for shipping
          const agNiCounties = [
              { value: 'ANT', label: 'Co Antrim' },
              { value: 'ARM', label: 'Co Armagh' },
              { value: 'DOW', label: 'Co Down' },
              { value: 'FER', label: 'Co Fermanagh' },
              { value: 'LDY', label: 'Co L/derry' },
              { value: 'TYR', label: 'Co Tyrone' }
          ];

          function agReplaceShippingStateOptions() {
              const select = document.getElementById('shipping-state');
              if (!select) {
                  return;
              }
              

              // Clear all existing options
              while (select.options.length > 0) {
                  select.remove(0);
              }

              // Placeholder option
              const placeholder = document.createElement('option');
              placeholder.value = '';
              placeholder.disabled = true;
              placeholder.selected = true;
              placeholder.textContent = 'Select a county';
              placeholder.setAttribute('data-alternate-values', '[Select a county]');
              select.appendChild(placeholder);

              // Add our NI counties
              agNiCounties.forEach(function (item) {
                  const opt = document.createElement('option');
                  opt.value = item.value;
                  opt.textContent = item.label;
                  opt.setAttribute('data-alternate-values', '[' + item.label + ']');
                  select.appendChild(opt);
              });
          }

          // Initial runs
          document.addEventListener('DOMContentLoaded', agReplaceShippingStateOptions);
          window.addEventListener('load', agReplaceShippingStateOptions);

          // Watch for Woo Blocks / dynamic updates
          const observer = new MutationObserver(function (mutations) {
              mutations.forEach(function (mutation) {
                  mutation.addedNodes.forEach(function (node) {
                      if (!(node instanceof HTMLElement)) {
                          return;
                      }
                      if (node.id === 'shipping-state' || node.querySelector?.('#shipping-state')) {
                          agReplaceShippingStateOptions();
                      }
                  });
              });
          });

          if (document.body) {
              observer.observe(document.body, { childList: true, subtree: true });
          }
      })();
  </script>
  <?php
}


Reasons:
  • RegEx Blacklisted phrase (1): I get an error
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Richard McClung