Conditional judgment tag function in WooCommerce – judging the current page in the theme

In order to facilitate theme development, WooCommerce provides us with some conditional judgment functions. We use these judgment functions to display or hide certain content under appropriate conditions. For example, if we only need to display one end of text on the store page, we just need to useis_shop()The function just needs to determine whether the current page is a store page. Because WooCommerce is a plugin for WordPress, allWordPress conditional judgment functionAlso works in WooCommerce.

[wprs_c-alert type=”warning” content=”Notice:We can use these conditional judgment functions in the template file only after the template is loaded.functions.phpfile is loaded before the template file, so infunctions.php , using these conditions to judge functions is invalid. ”]

Commonly used conditional judgment functions in WooCommerce

All conditional judgment functions test whether the current conditions match and returnTRUE, if it does not match, returnFALSE, the following list is our commonly used WooCommerce conditional judgment functions, please refer to all conditional functionsWooCommerceAPI Documentation

Whether it is a WooCommerce page

is_woocommerce()
Returns true if the page uses a WooCommerce template (the shopping cart and checkout pages use standard page templates and will not be included).

Is it the main store page?

is_shop()
Returns true (store) when the current page is a product archive page.

Is it a product category page?

is_product_category()
Returns true when the current page is a product category.
is_product_category( 'shirts' )
When the current page is the product category item ‘shirts’, true is returned, and ‘shirt’ is the category name.
is_product_category( array( 'shirts', 'games' ) )
Returns true when the current page is the product category item ‘shirts’ or ‘games’.

Is it a product label page?

is_product_tag()
Returns true when the current page is a product label.
is_product_tag( 'shirts' )
When the current page is the product tag item ‘shirts’, true is returned, and ‘shirt’ is the category name.
is_product_tag( array( 'shirts', 'games' ) )
Returns true when the current page is the product tag item ‘shirts’ or ‘games’.

Whether it is a single product details page

is_product()
When the current page is a single product details page, returns true, yesis_singularEncapsulation of conditional functions.

Is it a shopping cart page?

is_cart()
Returns true when the current page is a shopping cart page.

Is it a checkout page?

is_checkout()
Returns true when the current category is the checkout page.

Whether it is a customer account page

is_account_page()
Returns true when the current page is the customer account page.

Determine page endpoint

is_wc_endpoint_url()
Returns true when viewing the WooCommerce endpoint page.
is_wc_endpoint_url( 'order-pay' )
Returns true when viewing the payment order endpoint page.
is_wc_endpoint_url( 'order-received' )
Returns true when viewing the Order Received endpoint page.
is_wc_endpoint_url( 'view-order' )
Returns true when viewing the View Orders endpoint page.
is_wc_endpoint_url( 'edit-account' )
Returns true when viewing the edit account page.
is_wc_endpoint_url( 'edit-address' )
Returns true when viewing the edit address endpoint page.
is_wc_endpoint_url( 'lost-password' )
Returns true when viewing the password retrieval endpoint page.
is_wc_endpoint_url( 'customer-logout' )
Returns true when viewing the customer logout endpoint page.
is_wc_endpoint_url( 'add-payment-method' )
Returns true when viewing the Add Payment Method Endpoint page.

Determine whether it is an Ajax request

is_ajax()
Returns true when the current page is loaded via Ajax.

Usage example

The usage examples below demonstrate how to display different content in different categories.

if ( is_product_category() ) {
  if ( is_product_category( 'shirts' ) ) {
    echo '你好,我这里都 T-shirt。';
  } elseif ( is_product_category( 'games' ) ) {
    echo '你好,我这里都是游戏。';
  } else {
    echo '你好,欢迎光临我们的商店。';
  }
}

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *