Automatic redirect to checkout page is not working

With Appointments and Marketpress integration, after an appointment added into the cart, it is not redirecting user right away to a checkout page.
Even though " Redirect to cart page for immediate checkout" option is checked in MarketPress settings.
A visitor needs to click on "Proceed to checkout" link to be redirected. Is it possible to redirect a user automatically to the checkout page?

  • Konstantinos Xenos

    Hi Elzbieta !

    Can you add this code into your themes ( preferably child-themes ) functions.php and tell me if this works for you? The option you're referring to is bound only to native MarketPress products. This code will make Appointments+ to redirect to the checkout page when an appointment is added to MP's cart.

    // Appointments+ / MarketPress - Redirect to checkout when adding an Appointment to Cart
    add_action( 'wp_footer', function() {
    	global $post;
    	if ( ! $post instanceof WP_Post || strpos( $post->post_content, '[app_' ) === false || ! function_exists( 'mp_store_page_url' ) ) {
    		return;
    	}
    	$checkout_page = mp_store_page_url( 'checkout', false );
    	?>
    	<script type="text/javascript">
    	( function( $ ) {
    		var WPMUDEV_App_Post_Confirm = {
    			load: function(){
    				$( document ).ajaxComplete(function( event, xhr, settings ) {
    					var action = WPMUDEV_App_Post_Confirm.get_action( 'action', settings.data );
    					var response = JSON.parse(xhr.responseText);
    					if( action == 'post_confirmation' && typeof response.error === 'undefined' ){
    						document.location="<?php echo $checkout_page; ?>";
    					}
    				});
    			},
    			get_action: function(name, url){
    				if (!url){
    					return '';
    				}
    				name = name.replace(/[\[\]]/g, "\\$&");
    				var regex = new RegExp(name + "(=([^&#]*)|&|#|$)"),
    					results = regex.exec(url);
    				if (!results) return null;
    				if (!results[2]) return '';
    				return decodeURIComponent(results[2].replace(/\+/g, " "));
    			}
    		};
    		$( document ).ready(function(){
    			WPMUDEV_App_Post_Confirm.load();
    		});
    	} )( jQuery );
    	</script>
    	<?php
    }, 10 );

    Regards,
    Konstantinos

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.