bossanova808 / CommerceRegisterOnCheckout

ARCHIVED: SEE README Allow user registration during Craft Commerce V1 checkouts.
Other
33 stars 3 forks source link

Add standard POST redirect for non-AJAX requests #14

Closed auralon closed 6 years ago

auralon commented 6 years ago

If the form submission is not handled via AJAX, then it seems to make sense to be able to handle a redirect if one is supplied in the POST data.

auralon commented 6 years ago

Hi @bossanova808, the default Commerce templates actually contain the necessary code that displays flashed errors upon redirect. Line 12 of /shop/_layouts/checkout.html

<div class="flash">{{ craft.session.getFlash('error') }}</div>

This means that if your plugin submission has an error, the page will redirect back to the correct page with the error shown to the user.

I'm using the non-AJAX submission method for your plugin, with another stage added to the checkout process..

Instead of...

checkout index -> addresses ->shipping -> payment

... I've doing...

checkout index -> addresses ->shipping -> register -> payment

And in the shipping template, I've simply changed the redirect, like so...

<input type="hidden" name="redirect" value="shop/checkout/payment">

...to...

<input type="hidden" name="redirect" value="shop/checkout/register">

Finally, we can add the new register.html template that redirects to payment page...

{% extends 'shop/_layouts/checkout' %}

{% block main %}
    <div class="row">

        <div class="six columns">

            <h3>Register</h3>

            <form method="post" accept-charset="UTF-8" style="margin-top: 30px;">

                {{ getCsrfInput() }}

                <input type="hidden" name="action" value="commerceRegisterOnCheckout/saveRegistrationDetails">
                <input type="hidden" name="redirect" value="/shop/checkout/payment">

                <label for="password">Password</label>
                <input id="password" class="u-full-width" type="password" placeholder="New password (min. 6 characters)" name="password">

                <input type="submit" value="Complete Registration" class="button button-primary"/>

            </form>

        </div>

        <div class="six columns">

        </div>
    </div>
{% endblock %}

With this in place, the process works great, without any need to add in an AJAX call on any of the other templates.

I can provide some additional instructions for the README.md if you think it would be beneficial?

auralon commented 6 years ago

Also, numerous controllers handle redirects in such a way, if you grep the Commerce plugin, you'll see that Commerce_PaymentsController.php, Commerce_PromotionsController.php and Commerce_SettingsController.php all do much the same.

bossanova808 commented 6 years ago

Well, those are back end, not front end controllers.

Front end controllers don't generally re-direct on an error in my experience - look at the updateCart controller for example, or the payments controller, or the address controller. None of those re-direct on failure as far as I can see.

What you want to do makes sense, i.e. doing it without AJAX, I just don't think it should re-direct on error at the front end because that's not the Commerce pattern. I agree it should offer non ajax redirection on success if a url is POSTed though....kinda surprised I didn't do that already!

And probably while in there the password stuff should be a bit more consistent and actually check how many chars are submitted as well...rather just test for empty.

So basically I am fine with it all except for the re-direct on failure, and ideally you'd do a tad more in password area and indeed edit the Readme.md appropriately for the changes - if you're willing?

auralon commented 6 years ago

Yeah, I'm a little busy today, but I'll try to get the changes to you some time over the next week.

I also noticed that the plugin breaks if a user registers on checkout, logs out, then goes through the register on checkout process again with the same email address (without logging to their existing account). I've handled it in the commerce site we're working on by checking to see if the email address being provided when proceeding as guest is actually already a customer/user, then requesting that they login to proceed. Not given it too much thought as to how this scenario could be handled by the registerOnCheckout plugin itself (if it's even possible!)

:+1:

bossanova808 commented 6 years ago

Hmm, it doesn't with Ajax, I've tested that a bunch of times. And in the example code there is template stuff to handle it. So maybe look at that too for regular submission, while you're in there.

In the end I made an really call that we'd need JS for checkout, so I went with ajax all the way really...so this is underworked for non JS for sure. Main thing is to check out the Commerce front end controllers and copy them wherever possible so it behaves consistently, I'd say.

bossanova808 commented 6 years ago

How are you getting on with this?

bossanova808 commented 6 years ago

Given the conversation above I'm going to close this - I will add a basic redirect to a url if it has been posted though.

Feel free to re-open this if you get around to addressing the rest but right now it's working ok for most scenarios anyway.