I’m creating a WooCommerce site where the archive page shows products related to the city the user is interested in “London” or “Manchester.

On the /shop page of the website I’ve created a banner which will either have a button saying ‘Switch to London’ (if you’re in Manchester) or ‘Switch to Manchester’ (if you’re in London).

The content of this banner is set based on an IF function on which location you’re in.

Code below.

But, if you press the button it makes the submission BUT it doesn’t properly refresh the page, so it doesn’t reload the IF statement and you can’t click the button and change location again.

If you refresh the page it’s all sorted.

I’ve tried:

Adding some JavaScript to the button: onSubmit=”window.location.reload()” which does nothing AND

Adding something to one of the PHP functions which is fired by the button, but this just breaks things.

Any thoughts greatly appreciated and thanks in advance.

        <div class="switch-cities col-sm-12">
        <?php
            $user_id = get_current_user_id();

            if ( wc_memberships_is_user_active_member( $user_id, 'London' ) || wc_memberships_is_user_active_member( $user_id, 'Placeholder' ) ) {
                //$user_id = get_current_user_id();
                $newplan = 148230;
                $oldplan = 148169;

                if(array_key_exists('switchman',$_POST)){
                    lnz_wc_membershipswitch( $oldplan, $newplan, $user_id);
                return;
                }
                ?>
                    <form method="post">
                        <input type="submit" name="switchman" id="switchman" value="Switch to Manchester" onSubmit="window.location.reload()" /><br/>
                    </form>
                <?php;
                } elseif ( wc_memberships_is_user_active_member( $user_id, 'Manchester' ) || wc_memberships_is_user_active_member( $user_id, 'Placeholder' ) ) {
                //$user_id = get_current_user_id();
                $newplan = 148169;
                $oldplan = 148230;

                if(array_key_exists('switchlon',$_POST)){
                    lnz_wc_membershipswitch( $oldplan, $newplan, $user_id);
                return;
                }
                ?>
                    <form method="post">
                        <input type="submit" name="switchlon" id="switchlon" value="Switch to London" onSubmit="window.location.reload()" /><br/>
                    </form>
                <?php
                } else {
                    print 'something is wrong - not in a city';
                }
        ?>
        </div>

Read more here: Button not refreshing page


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: