How to know if the user is on cart page (is_cart())

Viewed 62

I have written this code here. Currently doing some tests before going to write a tracking script for WooCommerce.

The idea is I want to check whether user is on cart page and to return a message.

The problem is is_cart() method is not working at all (I think it is not even getting called).

What am I missing?

Should I require wp-includes/query.php or what?

function my_action_callback() {
  check_ajax_referer( 'my-special-string', 'security' );
  $whatever = intval( $_POST['whatever'] );
 
  if ( is_page( 'cart' ) || is_cart()) {
    $message = "Its on cart page";
  }else{
    $message = "Its not in cart page";
  }

  $page_id = wc_get_page_id( 'cart');

  $whatever += 10;
  echo $whatever . '=>>' . $message . ' <==== d';
  die(); 

}
1 Answers

Well, I don't know the is_cart method very well but maybe you could use an approach without it to achieve your goal.

function my_action_callback() {
   check_ajax_referer( 'my-special-string', 'security' );
   $whatever = intval( $_POST['whatever'] );

   $slug = basename(get_permalink());
   if($slug == 'cart'){
     $message = "Its on cart page";
   }else{
     $message = "Its not in cart page";
   }

   page_id = wc_get_page_id( 'cart');

   $whatever += 10;
   echo $whatever . '=>>' . $message . ' <==== d';
   die(); 

}

In this case I'm using the slug to know if I'm on the cart page or not.

I didn't get to test the code and I don't know much about its structure either, but I believe this approach can work.

Related