WooCommerce select shipping method first
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

We are using WooCommerce as a fast food delivery business. We want to have the customers land on a basic page where the customer simply adds their (UK) postcode and clicks either "local delivery" or "local pickup" as the respective delivery method plugins from woocommerce.

This would then select that as the method for them, and move them to the shop page, or error out and say "we can't deliver to that address, how about collecting" for deliveries outwith our area.

We have API use of GetAddress.io, so we could qualify the address and collect all the address info prior to clicking either of the buttons if this made the process more slick.

Just for clarity: you want a simple page where your customers can easily request your services, right?
feroldi over 3 years ago
No. I want the customer to arrive, and the homepage to have a box much like dominos.co.uk, where the customer is asked for their postcode prior to shopping. Instead of just a "go" button though, I want to ask for the postcode and they then click "delivery" or "collection", which then sets the delivery method for the (currently empty) basket to either "local deivery" or "local pickup". The local delivery however is validated per specific postcodes within that plugin - so it needs to check its a postcode we accept before it sets the delivery method to such.
lafav over 3 years ago
It would be good if you provide more information about the task, do you have a basic layout, like in PSD? Do you want only the integration from the form to the getaddress.io API? Do You already have the getaddress.io integration? Describe step by step your needs.
michaelnagy over 3 years ago
Nothing that complicated, I just need to code which sets the delivery method upon clicking a link. From there, we can provide two buttons - one that sets the method as local collection, and the other as local delivery. What would be a bonus is to make use of the getaddress.io, which yes we do have a key for, so that in addition to the delivery method we are also storing the customer address for the session. If they click provide an address and click deliver, we'd like to qualify that we actually deliver to that address. All of this can be a small amount of php, and I'll add it straight in to either functions.php or the page template.
lafav over 3 years ago
awarded to Nuno Freitas

Crowdsource coding tasks.

1 Solution

Winning solution

My solution consists of:

  • Form with postcode input and two submit buttons (local delivery and local pickup).
  • When the form is submitted it validates the following:
    • If the user choses local pickup, it sets the shipping method and redirects to the shop page.
    • If the user choses local delivery, it validates the postcode against the local delivery settings and if it is valid, it sets the shipping method and redirects to the shop page, otherwise it shows an error message.

In your WooCommerce settings you must have shipping enabled and the local delivery and local pickup methods enabled. You should customize the valid postcodes for local delivery.

Not included in the solution:

  • GetAddress.io integration.

Form. You can place this in your page template for example:

<?php
if (ff_has_shipping_errors()) {
    $errors = ff_get_shipping_errors();

    foreach ($errors->get_error_codes() as $code) {
        foreach ($errors->get_error_messages($code) as $message) {
            echo $message . "<br />";
        }
    }
}
?>
<form method="post">
    <input name="postcode" type="text" />
    <button name="shipping" value="delivery" type="submit">Local delivery</button>
    <button name="shipping" value="pickup" type="submit">Local pickup</button>
</form>

You should add your own formatting styles, etc.

Functions. You can place this in your functions.php:

function ff_get_shipping_errors() {
    global $shipping_errors;

    if (!isset($shipping_errors)) {
        $shipping_errors = new WP_Error();
    }

    return $shipping_errors;
}

function ff_has_shipping_errors() {
    $errors = ff_get_shipping_errors();
    $codes = $errors->get_error_codes();

    return !empty($codes);
}

function ff_set_shipping_and_redirect($shipping) {
    WC()->session->set('chosen_shipping_methods', array($shipping));
    wp_redirect(site_url('/shop/'));
    exit;
}

function ff_shipping_init() {
    if (isset($_POST['shipping'])) {
        if ($_POST['shipping'] === "pickup") {
            ff_set_shipping_and_redirect('local_pickup');
        } else if ($_POST['shipping'] === "delivery") {
            $isValidPostcode = false;

            if (isset($_POST['postcode'])) {
                $delivery = new WC_Shipping_Local_Delivery();
                $isValidPostcode = $delivery->is_valid_postcode($_POST['postcode'], 'GB');
            }

            if ($isValidPostcode) {
                ff_set_shipping_and_redirect('local_delivery');
            } else {
                $errors = ff_get_shipping_errors();
                $errors->add('shipping', __('We can\'t deliver to that address, how about local pickup?', 'ff'));
            }
        }
    }
}
add_action('init', 'ff_shipping_init');