7

I'm creating a plugin to disable Cart on WooCommerce (even I know already some plugin doing this), I already removed the button to access cart page.

But how can I check if url is /cart? WordPress have some function to get it?

What I need to do is like:

if (is_cart()) {
    redirect('home_page');
}

I tried to use is_cart function as said in WooCommerce doc, but seems not working for me.

2

1 Answer 1

12

I'm not sure where are you hooking your function to, but you might be doing it too early.

Hook to template_redirect, and then redirect the user:

add_action('template_redirect','redirect_visitor');
function redirect_visitor(){
    if ( is_page( 'cart' ) || is_cart() ) {
        wp_safe_redirect(site_url());
        exit(); // Don't forget this one
    }
}
1
  • Just a sidenote, that parse_query also works and is earlier Jan 15, 2020 at 23:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.