alextiley
alextiley

Reputation: 31

WooCommerce: Redirecting to cart page if thankyou page is requested without an order

If a user tries to directly access the 'Thank you' page they should be redirected to the woocommerce cart if there isn't an order in the request.

I've submitted a feature request for the above over on the Wordpress forums. In the meantime I was hoping somebody could think of a way of doing this via functions.php in my theme?

I'm unable to use the woocommerce_thankyou hook as this isn't loaded when you just land on the page (it's only hookable if you land on the thanks page with an order from the checkout).

Is there a way in functions.php I can check that it's the woocommerce thankyou page and also check if there's a global $order object?

Upvotes: 1

Views: 2885

Answers (1)

ips
ips

Reputation: 111

add_action( 'template_redirect', 'your_template_direction_function' );
function your_template_direction_function() {
    global $woocommerce;
    if ($woocommerce && is_page( woocommerce_get_page_id( 'thanks' ) ) && sizeof($woocommerce->cart->get_cart())==0) :
        wp_redirect(get_permalink(woocommerce_get_page_id('cart')));
        exit;
    endif;
}

Upvotes: 1

Related Questions