0

In category display type u can choose between (default, products, subcategories, both) options. Is there any way to make an if statement that uses those values?

I would like todo something like the below but i'm having a hard time figuring out how u can check for those values.

Now i have something like the below but that requires me to do this for hundreds of categories

<?php if (is_product_category('979') || is_product_category('979')) then do something ?>

So i'm actually looking for something like the below

<?php if (category is display type ('products')) then do something ?>

So if anyone know a way to filter using display type that would be great.

Thanks in advance

0

Depending on where whoocommerce posts are stored, you could do this via basic Wordpress post queries, via:

$args = array(
  'post_type' => 'your_post_type',
  'category_name' => 'your_category',
  'tag' => 'your_tag'
);

$arr_posts = new WP_Query($args);

if ($arr_posts->have_posts()) {
  while ($arr_posts->have_posts()) {
    echo $arr_posts->the_post();
      // Echo out content of each post, but here you may do whatever you want
      the_content();
      }
    } else {
    echo "<p class='error'>No posts fulfill your search criteria!<p>";
    }

Note that you can adapt your query parameters, by specifying additional query criteria in the $args array (or take out any of the ones I provided, if you please).

I've added an error class to your error feedback such that you can style it in CSS as you may please (color:red; etc). Again, this example works if your product posts are stored in the wp_posts database. If not, you may do something similar, by querying in the corresponding database holding your product posts.

Just found this https://wp-staging.com/in-which-database-table-is-woocommerce-storing-products/, so the code above should definitely work.

3
  • Instead of matching data from wp_posts i'm trying to look inside wp_termmeta and search for meta_key 'display_type' that has a value of 'products' inside meta_value. And it needs to check true or false against the currently displayed WooCommerce product-category. – Jeroen_L May 4 '20 at 14:21
  • According to the link I've sent you above, you should be able to then use 'post_type' => 'products' and retrieve currently_displayed_category and use 'category_name' => 'currently_displayed_category'. Use these two query specifications in the $args array as described above, and it should work. – Joe May 4 '20 at 14:42
  • I tried your solution which didnt gave the right true or false. i also tried code below but didnt get it working exactly right yet $display_type_check = get_term_meta( get_the_ID(), 'products' ); $cat_id = $queried_object->term_id; $product_pagina = ''; foreach ($display_type_check as $check_type) { $category_id = ($check_type->term_id); if ($check_type->term_id === $cat_id) { $product_pagina = 'goed'; } } – Jeroen_L May 5 '20 at 21:52
0

After some searching i found a solution in the code below which checks if your category display view is set in my case as products.

        $cat_id = $queried_object->term_id;
        $test = array(
            'hide_empty' => false, // also retrieve terms which are not used yet
            'meta_query' => array(
                array(
                'key'       => 'display_type',
                'value'     => 'products',
                'compare'   => 'LIKE'
                )
            ),
        );
        $terms = get_terms( $test );
            foreach ($terms as $product_page) {
            if ($product_page->term_id === $cat_id) {
        $category_value = $product_page->term_id;
        } }

Now u can use something like $cat_id === $category_value Whenever a category page is set to display_type products it will return a true

If needed u can change this to any of the other options as shown below.

                'value'     => 'subcategories',
                'value'     => '', // <-- this is the default
                'value'     => 'both',  // <-- this is both options
0

You can do this using the function get_term_meta.

// Get the current category ID;
$category_id = get_queried_object_id();

// Get the meta data using the ID;
$term_vals = get_term_meta( $category_id );

// One of the values is display type, with the value at index 0;
// this can be products, subcategories, or both
if ( $term_vals[ 'display_type' ][ 0 ] === 'products' ) {
    // Do your code here
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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