Home > Software engineering >  Is there a way to query the Shipping Zone on a Woocommerce checkout page?
Is there a way to query the Shipping Zone on a Woocommerce checkout page?

Time:01-13

I need to hide/show a WooCommerce checkout field based on the Shipping Zone AND Total Cart weight. I've got the following code working when its just querying the cart weight I just need to get it to check the Zone first. See my failed code below...

add_action( 'wp_footer', 'hide_fieldset_based_on_zone_and_cart_weight', 9999 );

function hide_fieldset_based_on_zone_and_cart_weight() {

    // THIS IS WHERE I NEED TO QUERY THE SHIPPING ZONE
    $shipping_zone = WC_Shipping_Zones::get_zones();
   
    // Get the total weight of the cart
    $cart_weight = WC()->cart->get_cart_contents_weight();

    // If the Zone is Blue Zone and the cart weight is less than 500, add some jQuery to hide the fieldset element
      if ($shipping_zone == 'Blue Zone' && $cart_weight < 500) {
        ?>
        <script>
            jQuery( document ).ready( function( $ ) {
                // Find the fieldset element and hide it
                $( 'fieldset#review_order_before_payment_tester_group_field' ).hide();
            } );
        </script>
        <?php
    }
} 

Any ideas?

Thanks in advance.

CodePudding user response:

Try this :

// Get the package details
$package = array(
    'destination' => array(
        'country' => WC()->customer->get_shipping_country(),
        'state' => WC()->customer->get_shipping_state(),
        'postcode' => WC()->customer->get_shipping_postcode(),
        'city' => WC()->customer->get_shipping_city(),
        'address' => WC()->customer->get_shipping_address(),
        'address_2' => WC()->customer->get_shipping_address_2()
    ),
    'weight' => WC()->cart->get_cart_contents_weight()
);

// Get the matching zone
$zone = WC_Shipping_Zones::get_zone_matching_package($package);

// Check if it's the zone you're looking for
if ($zone && $zone->get_zone_name() == 'Blue Zone') {
    // Check the cart weight
    if ($cart_weight < 500) {
        ?>
        <script>
            jQuery( document ).ready( function( $ ) {
                // Find the fieldset element and hide it
                $( 'fieldset#review_order_before_payment_tester_group_field' ).hide();
            } );
        </script>
        <?php
    }
}

CodePudding user response:

Add an ID to the postcode field in your form, for example:

In your JavaScript code, use jQuery to listen for changes to the postcode field:

jQuery(document).ready(function($) {
    $('#shipping_postcode').on('change', function() {
        // Get the new postcode value
        var postcode = $(this).val();
        // Update the shipping zone and cart weight based on the new postcode
        updateShippingZoneAndCartWeight(postcode);
    });
});

Create a new function called updateShippingZoneAndCartWeight that will handle updating the shipping zone and cart weight based on the new postcode:

    function updateShippingZoneAndCartWeight(postcode) {
    // Make an AJAX call to the server to update the shipping zone and cart weight
    jQuery.ajax({
        type: 'POST',
        url: 'your-ajax-url.php',
        data: {
            action: 'update_shipping_zone_and_cart_weight',
            postcode: postcode
        },
        success: function(response) {
            // Update the shipping zone and cart weight on the page
            // 
        },
        error: function(error) {
            console.log(error);
        }
    });
}

In your ajax url you can use the above code to update the shipping zone and cart weight based on the new postcode.
  • Related