0

I am trying to do something I thought would be fairly simple. But it's not working, and has me stumped as to why. Any insight would be appreciated.

I have a custom /woocommerce/archive-product.php page in a theme. It works fine, and displays wherever WC product pages are called.

I am trying to add a Worpdress conditional to it. As follows:

  if ( is_page( 'Bangles','bangles' ) ) {

CODE TO RUN

} else {

OTHER CODE TO RUN

}

Yet it never tests as true.

The page url I was expecting it to come back as true for is site.org/product-categories/bangles. "Bangles" in this case is the product category. It's also the title of the page.

I figured the Bangles (capital B) variant might get picked up through the title test this conditional does, and the bangles version would get picked up by the page name slug test. One, other, nor both work as expected.

In every case OTHER CODE TO RUN is what will run.

Am I over looking something? Is there any chance this conditional only tests for actual Wordpress Pages, as opposed to Woocommerce product archive pages?

In case it helps, below is the surround code in which this conditional has been placed.

<?php
if ( function_exists( 'wc_the_product_table' ) ) {

    if (is_page('Bangles','bangles')) {

echo "test";

    } else {
        $shortcode = '[product_table columns="image:blank,sku,name,weight,dimensions,price:Est. Cost,add-to-cart:Add?" sort="sku" variations="separate" links="none" filters="true" cart_button="checkbox" priorities="1,6,2,4,3,1" ajax_cart="true" show_footer="true" paging_type="full_numbers" product_limit="600" rows_per_page="50"]';
    }

    /**
     * Don't modify anything below here unless you know what you're doing!
     */
    $args = shortcode_parse_atts( str_replace( array( '[product_table ', ']' ), '', $shortcode ) );

    if ( is_product_category() ) {
        // Product category archive
        $args['category'] = get_queried_object_id();
    } elseif ( is_product_tag() ) {
        // Product tag archive
        $args['tag'] = get_queried_object_id();
    } elseif ( is_product_taxonomy() ) {
        // Other product taxonomy archive
        $term            = get_queried_object();
        $args['term']    = "{$term->taxonomy}:{$term->term_id}";
    } elseif ( is_post_type_archive( 'product' ) && get_query_var( 's' ) ) {
        // Product search results page
        add_filter( 'wc_product_table_run_in_search', '__return_true' );

        if ( have_posts() ) {
            global $wp_query;
            $args['include'] = wp_list_pluck( $wp_query->posts, 'ID' );
        } else {
            // Force 'nothing found' message if no posts in query
            $args['include'] = -1;
        }
    }

    // Display the product table
    wc_the_product_table( $args );
} elseif ( have_posts() ) {

UPDATE:

So, I've since found a solution to achieve what I was wanting to achieve. I would, however, still like to understand why the above did not work. I'd like to know what I've misunderstood about how is_page functions.

The solution I found was to instead use the has_term conditional. In my case, I've used, if ( has_term ('bangles','product_cat')) { and this has done the trick.

As pointed out by Jacob, my update was using the wrong approach. With his help, what I've come up with is: (which tests whether the current product category is the child of either of two parents)

$id = get_queried_object_id();
$ancestors = get_ancestors( $id, 'product_cat', 'taxonomy' );
if (in_array("94", $ancestors) || in_array("19", $ancestors)) {

echo "true";

    } else {

echo "false";

    }

1 Answer 1

3

"Bangles" isn't a page. It's a Product Category Archive. is_page() checks:

Is the query for an existing single page?

Which product categories are not. See the Template Hierarchy to get an idea of the different types of 'pages' in WordPress.

If you want to check if you are viewing a product category archive, use is_tax(), which checks:

Is the query for an existing custom taxonomy archive page?

So you would use it like this:

if ( is_tax( 'product_cat', 'bangles' ) ) {

}

WooCommerce also provides its own conditional tags, including a function for checking product categories, is_product_category():

if ( is_product_category( 'bangles' ) ) {

}

Regarding your edit, has_term is also incorrect. That function is for checking if an individual post has the bangles category. On an archive this will be checking the first post/product in the list. While this will be true on the Bangles category page, since all the products in that category have that category, it could also be true on other categories if a product in that category also happens to be in the Bangles category.

The important thing here is that WordPress serves up different types of content, primarily Posts and Archives, and there are different types of posts and archives. In WordPress a 'Page' is a type of post. So are products. If you want to check which 'page' (in the web browser sense of the word) you need to know which type of content you are looking for and use the appropriate function.

3
  • Thanks Jacob. This is extremely helpful. I am fairly new to this, so your clear detailed explanation has set me on the right course. Is it possible to test if the parent category? In my case, I basically want to test for around 30 different product categories. These are all children of one parent ("Chains"). All the other product categories are parents in their own right. I figure it would be more efficient to just test for "Chains" as the parent, if that's an option. I don't see any mention of that in the woocommerce conditional tags docs.
    – omega33
    Commented Apr 5, 2018 at 8:22
  • 1
    In that case I would use get_queried_object_id() to get the ID of the current category. Then use that ID in get_ancestors( $id, 'product_cat', 'taxonomy' ) to get all ancestors of the current category. Finally check the results of that to see if the ID for 'Chains' is in there. If it is then the current category is a descendent of 'Chains' and the condition will be true. Commented Apr 5, 2018 at 8:27
  • Brilliant. Thanks. After much messing around, and Googling, based on what you shared, I managed to figure it out. There was actually two possible parents I needed to test for. What I've come up with (which works) is, $id = get_queried_object_id(); $ancestors = get_ancestors( $id, 'product_cat', 'taxonomy' ); # if (preg_match('/\b94\b/',$ancestors)) { if (in_array("94", $ancestors) || in_array("19", $ancestors)) { echo "true"; } else { echo "false; } - Very happy.
    – omega33
    Commented Apr 5, 2018 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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