• Resolved vincenzoparco

    (@vincenzoparco)


    My concern is whether there is a way to hide the other shipping options if a customer qualifies for free shipping.

    note: all my shipping options was setup by flexible shipping

Viewing 1 replies (of 1 total)
  • Thread Starter vincenzoparco

    (@vincenzoparco)

    After 24 hours of finding a workaround, I finally solved this.

    here’s the code snippet for fix amount threshold (1), dynamically check for qualifying conditions for free shipping without a fixed threshold (2).

    //(1)
    <script type="text/javascript">
    jQuery(document).ready(function($) {
    // Function to check if the customer qualifies for free shipping
    function checkShippingOptions() {
    // Check if the customer qualifies for free shipping based on their cart total
    var cartTotal = parseFloat($('.cart-subtotal .woocommerce-Price-amount').text().replace(/[^0-9.-]+/g,"")); // Adjust the selector as needed
    var freeShippingThreshold = 5000; // Set your free shipping threshold amount

    var qualifiesForFreeShipping = cartTotal >= freeShippingThreshold;

    if (qualifiesForFreeShipping) {
    // Hide Lalamove and In-store Pickup options by their labels
    $('label:contains("Lalamove")').closest('li').hide(); // Hide Lalamove
    $('label:contains("In-store Pickup")').closest('li').hide(); // Hide In-store Pickup
    } else {
    // Show both options if the customer does not qualify for free shipping
    $('label:contains("Lalamove")').closest('li').show();
    $('label:contains("In-store Pickup")').closest('li').show();
    }
    }

    // Initial check on page load
    checkShippingOptions();

    // Check again when the shipping method changes
    $('input[name="shipping_method[0]"]').change(function() {
    checkShippingOptions();
    });

    // Trigger check on cart updates
    $(document.body).on('updated_wc_div updated_checkout', function() {
    checkShippingOptions();
    });

    // Trigger check on the checkout form submit
    $('form.woocommerce-checkout').on('submit', function() {
    setTimeout(checkShippingOptions, 100); // Delay to allow for any AJAX updates
    });
    });
    </script>
    //(2)
    <script type="text/javascript">
    jQuery(document).ready(function($) {
    // Function to check if free shipping is available
    function checkShippingOptions() {
    // Check if the free shipping option is available
    var freeShippingAvailable = $('input[name="shipping_method[0]"]').filter(function() {
    return $(this).next('label').text().includes('Free Delivery');
    }).length > 0;

    if (freeShippingAvailable) {
    // Hide Lalamove and In-store Pickup options by their labels
    $('label:contains("Lalamove")').closest('li').hide(); // Hide Lalamove
    $('label:contains("In-store Pickup")').closest('li').hide(); // Hide In-store Pickup
    } else {
    // Show both options if free shipping is not available
    $('label:contains("Lalamove")').closest('li').show();
    $('label:contains("In-store Pickup")').closest('li').show();
    }
    }

    // Initial check on page load
    checkShippingOptions();

    // Check again when the shipping method changes
    $('input[name="shipping_method[0]"]').change(function() {
    checkShippingOptions();
    });

    // Trigger check on cart updates
    $(document.body).on('updated_wc_div updated_checkout', function() {
    checkShippingOptions();
    });

    // Trigger check on the checkout form submit
    $('form.woocommerce-checkout').on('submit', function() {
    setTimeout(checkShippingOptions, 100); // Delay to allow for any AJAX updates
    });
    });
    </script>

    Given that the shipping options have dynamic numbers at the end, I used the labels to identify them instead.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.