0

i have custom dropdown field on checkout page For region select. Have 2 rate:

  1. For Region X with cost 5$ (flat-rate:34)
  2. Second for all others with cost 10$ (flat-rate:35)

I want to set rates on field change.

// Enqueue custom JavaScript for detecting region change inline
add_action('wp_footer', 'add_inline_region_shipping_js');
function add_inline_region_shipping_js() {
    // Only add the script on the checkout page
    if (is_checkout()) {
        ?>
        <script type="text/javascript">
            jQuery(function($) {
                // Listen for changes on the region dropdown
                $('#easyway_region').change(function() {
                    // Trigger WooCommerce's checkout update after changing the region
                    $('body').trigger('update_checkout');
                });
            });
        </script>
        <?php
    }
}

// Adjust available shipping rates based on the selected region in the checkout form
add_filter('woocommerce_package_rates', 'adjust_shipping_methods_based_on_region', 10, 2);
function adjust_shipping_methods_based_on_region($rates, $package) {
    // Get the selected region from the checkout form
    $region = isset($_POST['easyway_region']) ? sanitize_text_field($_POST['easyway_region']) : '';

    // Tbilisi Region ID (ID 24, update if needed)
    if ($region == '24') {
        // Only allow the Tbilisi shipping rate (flat_rate:34)
        foreach ($rates as $rate_key => $rate) {
            if ($rate->method_id !== 'flat_rate' || $rate->id !== 'flat_rate:34') {
                unset($rates[$rate_key]); // Remove non-Tbilisi rates
            }
        }
    } else {
        // Only allow the other region shipping rate (flat_rate:35)
        foreach ($rates as $rate_key => $rate) {
            if ($rate->method_id !== 'flat_rate' || $rate->id !== 'flat_rate:35') {
                unset($rates[$rate_key]); // Remove non-other region rates
            }
        }
    }

    return $rates;
}

need to fix this one

3
  • See here, $('body').trigger('update_checkout');, is update_checkout and ID or a CLASS? If it's the button ID, it should be $('body').trigger('#update_checkout'); if it's the button CLASS name it should be $('body').trigger('.update_checkout');. If that's NOT it, please update us as to what specifically is happening. Commented Nov 20 at 15:33
  • $('#easyway_region').on('change', function() { $('body').trigger('update_checkout'); // Trigger recalculation }); its on dropdown change must be updated. Commented Nov 21 at 4:20
  • Ok, but what I'm saying is that you're trying to impact update_checkout so that something triggers it and I don't think your script knows what that is. Is update_checkout a button? And is that id="update_checkout" or is it class="update_checkout"? Commented Nov 21 at 15:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.