Appointments+ app_footer_scripts Not Working

Hi,

I have Appointments+ v 1.5.7. I am trying to redirect a user to a thank you page instead of showing the JS alert box. However, it does not redirect.

I have added the following code into my functions file:

function whc_appplus_redirect( $script ){
echo 'Hello World!';
$thanks_page = site_url('/booking-confirmation');
return str_replace("window.location.href=app_location()", "window.location.href='".$thanks_page."'", $script);
}
add_filter( 'app_footer_scripts', 'whc_appplus_redirect' );

Also, for debugging purposes, I have temporarily edited the appointments.php file as follows:

function wp_footer() {
$script = '';
echo '
Pre filter:
';
var_dump($this->script);
$this->script = apply_filters( 'app_footer_scripts', $this->script );
echo '
Post filter:
';
var_dump($this->script);

if ( $this->script ) {
$script .= "<script type='text/javascript'>";
$script .= "var appDocReadyHandler = function($){";
$script .= $this->script;
$script .= "};";
$script .= "jQuery(document).ready(appDocReadyHandler)";
$script .= "</script>";
}

echo $this->esc_rn( $script );
do_action('app-footer_scripts-after');
}

If you look at the bottom of the "book appointment" page you will see the output of all of this: http://chirowh.roderickpughmarketing.com/make-an-appointment/

It seems to me that there is no "window.location.href=app_location()" in the script to be changed by the new function. Therefore, the before and after versions of the script are the same.

Have I got a setting wrong somewhere on the settings panel or should the filter function be looking for something different or am I missing something else?

Many thanks!

Ian

  • Michael Bissett

    Hey @Ian, Michael here! :slight_smile:

    While the removing of the alert would seem to require a core code edit presently, I do know that the redirect can be handled like this:

    function app_redirect(){
        echo '<script type="text/javascript">
    window.app_location = function () { return "' . site_url('/booking-confirmation') . '"; }
    </script>';
    }
    add_filter( 'wp_footer', 'app_redirect' );

    Can you give that a go, and see if that works on your site please? :slight_smile:

    Kind Regards,
    Michael

    P.S. The code for the alert notice would be found in line 187 of:

    /appointments/includes/shortcodes/js/app-confirmation.js

  • Ian

    Hi Michael,

    Thanks for getting back to me.

    I have added that function and it partially works. Confirmation of a booking still brings up the JS popup but after clicking on that it will redirect to the thank you page. So, I need a way to stop the popup. I think you are suggesting editing the core files but I would really prefer not to have to do that (I thought that was why the filter was suggested).

    Secondly, if I "cancel" from the confirmation section, it also goes to the thank you page so I need it to only redirect when a booking has actually been confirmed.

    Ian

  • Michael Bissett

    Hey @Ian,

    The code mentioned earlier wasn't working on my site, I offered the code I did because I found it to be operational over on my end. :slight_smile:

    As for the alert, the earlier code snippets (and my own code snippet) would only tackle the redirect side of things, the alert would have still been present. I did take a peek and see if there was a way of handling it otherwise, but the advice offered earlier was the conclusion I had arrived at, after doing some research.

    As for the redirect issues:

    function app_redirect(){
        echo '<script>
        window.app_location = function () { return "' . site_url('/booking-confirmation') . '"; }
        jQuery(document).ready(function() {
            jQuery( ".appointments-confirmation-cancel-button" ).on("click", function(){
    			window.app_location = function () { return "' . site_url('/booking') . '"; }
    		});
        });
    </script>';
    }
    add_filter( 'wp_footer', 'app_redirect' );

    Change /booking in this new code to your appointment page, and that should do the trick for cancellations. :slight_smile:

    Kind Regards,
    Michael

  • Ian

    Hi Michael,

    Thank you! I really appreciate your help.

    It seems to me though that I need to get hold of the developers somehow and get them to fix the bug. The documentation says to use the filter but, as you say, it does not work properly.

    Thank you for the revised function above. I'll put that in place to exclude the "cancel" option in the meantime. Do you know how I contact the developers? I cannot seem to find the "support" button on the site anywhere?

    Many thanks!

    Ian

  • Michael Bissett

    Hey @Ian,

    Two things to note:

    #1: A fresh look & some coffee helps in looking at this, the echo statement being followed by a return statement in the fashion noted in the function you mentioned:

    function whc_appplus_redirect( $script ){
    echo 'Hello World!';
    $thanks_page = site_url('/booking-confirmation');
    return str_replace("window.location.href=app_location()", "window.location.href='".$thanks_page."'", $script);
    }

    Wouldn't be correct from a code perspective (you'd either return, or echo, not both).

    That said...

    #2: The filter works, but it appears that there's something in the codebase that's changed, that requires an alteration to the latter part of the earlier code snippet:

    $thanks_page = site_url('/booking-confirmation');
    return str_replace("window.location.href=app_location()", "window.location.href='".$thanks_page."'", $script);

    You'll notice that if you were to have just this:

    function whc_appplus_redirect( $script ){
    echo 'Hello World!';
    }
    add_filter( 'app_footer_scripts', 'whc_appplus_redirect' );

    And the view the source for the page that's loaded in your browser, that you'd see the "Hello World!" statement there, so the filter is being used.

    So we're talking about writing up a different script, rather than a bug being involved. :slight_smile:

    That said, when it comes to bugs (since you mentioned the lack of a "support" button for the developers), it works like this:

    #1: You, the member, report an issue.
    #2: I as staff go check out the situation, and see if I can reproduce.
    #3: If I can reproduce it, then I report the bug to the developer via our issue tracker, and he looks into it.

    We the support staff are the middleman, as it were, so they can focus on doing their development work. :slight_smile:

    Kind Regards,
    Michael

  • Ian

    Hi Michael,

    Thanks for getting back to me. Sorry, I had not understood the way that things worked here. I had thought that you were another member offering suggestions. My apologies for getting that wrong.

    The "hello world" echo was, as you have demonstrated, simply added in there by me to confirm that the function was being called. As I mentioned, I inserted a few bits of code to help with debugging.

    The filter does NOT work. The FAQs in the product say that all I need to do is filter app_footer_scripts in answer to "How can I redirect a user after a confirmed appointment to a thank you page when payment is not required?" (actually, there is a bug in the FAQs that displays the wrong answer for the FAQ as well). This filter does not redirect the user in anything like an acceptable way. It still displays the popup and it redirects even if the appointment has not been confirmed. That's a bug and it needs to be fixed. I am sure that you are right that "something in the codebase has changed". That's how bugs happen.

    All I want is to simply "redirect a user after a confirmed appointment to a thank you page when payment is not required". If you cannot get the developers to fix it for me now then please help me with how I hack the core code until a real fix is provided.

    Can you officialy mark this as an "issue" that I am reporting please?

    Many thanks

    Ian

  • Michael Bissett

    Hey @Ian,

    No worries about me looking like another member, we're good there. :slight_smile:

    Just to back up a moment, I had only been aware of this snippet:

    function app_redirect( $script ){
    	return str_replace("window.location.href=app_location()", "window.location.href='http://example.com'", $script);
    }
    add_filter( 'app_footer_scripts', 'app_redirect' );

    Existing over here in the forums, rather than being around in the FAQs, and I do admit that this was an oversight of mine.

    In other words, it seemed to a code snippet suggested outside of the plugin (by someone else), rather than inside of it.

    So yes, I can get the developer's attention on this, so that this can be looked into. :slight_smile:

    On this part:

    (actually, there is a bug in the FAQs that displays the wrong answer for the FAQ as well)

    Yep, the developer's already aware of that one. :slight_smile:

    Now, as for the situation in the meantime...

    All I want is to simply "redirect a user after a confirmed appointment to a thank you page when payment is not required".

    Can I confirm that the earlier code snippet suggested here:

    https://premium.wpmudev.org/forums/topic/appointments-app_footer_scripts-not-working#post-1026504

    Plus the removal of the call to "alert" directly inside the code, mentioned up here:

    https://premium.wpmudev.org/forums/topic/appointments-app_footer_scripts-not-working#post-1025475

    Is working? :slight_smile:

    For the sake of clarity, all that the redirect code suggested does (loaded via the filter, as the filter's a place where JS code can inserted into), is change the location of the URL to redirect to, by doing a real time change of the code itself. It wouldn't touch the "alert" code by default (that's the sucker responsible for displaying that popup box), though I can ask the developer about that too. :slight_smile:

    Kind Regards,
    Michael

  • Ian

    Hi Michael,

    Sorry for the slow reply.

    The FAQ bug seems to have been fixed now. However, the redirect is not working. There seem to still be two problems ...

    1) when confirming an appointment, it will redirect to the thank you page but only after also showing the JS alert. I have temporarily hacked the code to stop that from happening but that is obviously not a real fix.

    2) The bigger problem is that it is also redirecting to the thank you page after a cancellation. I can see that an option has been added to redirect to a cancellation page but setting this seems to make no difference. Ideally I would like the user to simply remain on the appointment booking page after a cancellation but a redirect somewhere else is ok ... just not to the thank you page.

    Many thanks

    Ian

  • Michael Bissett

    Hey @ian32,

    #2: I do see what you mean about this, I've followed up with the developer on this, so that this can be addressed further.

    #1: I can bring up the idea of a toggle option for this, and have done so.

    In the meantime, the workaround I've been given would be this:

    add_action( 'wp_footer', 'app_hooks_enqueue_my_confirmation_js' );
    function app_hooks_enqueue_my_confirmation_js() {
        if ( wp_script_is( 'app-shortcode-confirmation', 'enqueued' ) ) {
            wp_dequeue_script( 'app-shortcode-confirmation' );
            wp_enqueue_script( 'my-app-shortcode-confirmation', 'http://mysite/wp-content/themes/my-theme/js/app-shortcode-confirmation.js' );
        }
    }

    It dequeues the current script, and enqueues a custom one located at /wp-content/themes/my-theme/js/app...

    You'd want to alter the URL to reflect the actual location of your script, but this would allow you to copy and paste the current script and modify whatever you need to.

    You'd need to pay attention to the big releases (1.x or 2.x, not 1.x.x), just in case it stops working.

    Kind Regards,
    Michael

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.