WooCommerce Conditional Tag Checks All Pages

The code in this tutorial creates a custom made conditional tag which you can use with any custom function to check for all types of WooCommerce pages. This means you can check for shop, product tag, product category, terms & conditions, cart page, checkout page, my account page and all your different types of product pages.

The code checks all pages you create manually using your themes default page template as well as any pages generated by the system which are required to configure the settings.

Here’s the code you can add to your child themes functions file to create the WooCommerce conditional tag.

function is_woocommerce_page() {

        if (  function_exists ( "is_woocommerce" ) && is_woocommerce() ) {
                return true;
        }
        $woocommerce_keys   =   array ( "woocommerce_shop_page_id" ,
                                        "woocommerce_terms_page_id" ,
                                        "woocommerce_cart_page_id" ,
                                        "woocommerce_checkout_page_id" ,
                                        "woocommerce_pay_page_id" ,
                                        "woocommerce_thanks_page_id" ,
                                        "woocommerce_myaccount_page_id" ,
                                        "woocommerce_edit_address_page_id" ,
                                        "woocommerce_view_order_page_id" ,
                                        "woocommerce_change_password_page_id" ,
                                        "woocommerce_logout_page_id" ,
                                        "woocommerce_lost_password_page_id" ) ;
        foreach ( $woocommerce_keys as $wc_page_id ) {
                if ( get_the_ID () == get_option ( $wc_page_id , 0 ) ) {
                        return true ;
                }
        }
        return false;
}

You can then use is_woocommerce_page() as your conditional tag.

Credit to FAISHAL SAIYED Senior Web Engineer at 10up.com

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.