Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 244 Vote(s) - 3.66 Average
  • 1
  • 2
  • 3
  • 4
  • 5
In a WordPress template, how can I detect if the current page is the WooCommerce cart or checkout page?

#1
I would like to have a header banner hidden when a user is in the WooCommerce cart or checkout process. **Is there a flag or variable that I can check to see if the current page is in either of these WooCommerce sections?** I basically want to do something like the following:

```
if (!is_checkout() && !is_cart()) {
echo "<div>My Banner</div>";
}
```

I realize I can make a custom page template for each of these sections, but I just want to add a simple bit of code to my global site header.
Reply

#2
**Cart page**

is_cart()

*Returns true on the cart page.*

**Checkout page**

is_checkout()

*Returns true on the checkout page.*

You can see more about [WooCommerce conditional tags][1]


[1]:

[To see links please register here]

Reply

#3
If you want to use a wordpress function, there is `is_page( 'name')' here for you.

[To see links please register here]


You need to use the name (page slug) of your pages. In the example, I assume they are called 'cart and 'checkout'.

`if (!is_page('cart') && !is_page('checkout')) { ... }`

As in mujuonly s answer, you can also use what woocommerce offers you with conditional tags.

My answer may be an alternative if you are facing problems with woocommerce functions or want to stay with wordpress functions.
Reply

#4
Please note that `is_checkout()` is useless if you need it early (before the template is loaded I think) and will incorrectly return false on checkout page if it's called too early.

Something like this is more universal:

```php
/**
* Checks if checkout is the current page.
*
* @return boolean
*/
function better_is_checkout() {
$checkout_path = wp_parse_url(wc_get_checkout_url(), PHP_URL_PATH);
$current_url_path = wp_parse_url("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]", PHP_URL_PATH);

return (
$checkout_path !== null
&& $current_url_path !== null
&& trailingslashit($checkout_path) === trailingslashit($current_url_path)
);
}
```

is_checkout() is fine for the actual question, but the question also ranks first in Google for "WooCommerce check if checkout"

The same applies for cart:
```php
/**
* Checks if cart is the current page.
*
* @return boolean
*/
function better_is_cart() {
$cart_path = wp_parse_url(wc_get_cart_url(), PHP_URL_PATH);
$current_url_path = wp_parse_url("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]", PHP_URL_PATH);

return (
$cart_path !== null
&& $current_url_path !== null
&& trailingslashit($cart_path) === trailingslashit($current_url_path)
);
}
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through