1

I have two product category

  1. MULTILINGUAL DIGITAL MARKETING (ID 75)
  2. INTERNATIONAL SALES CHANNELS (ID 107)

I want to run some code via if condition for these two category only.

I tried using this code but it didn't worked

if( is_product_category(107 || 75) ) { 

$term = get_queried_object();

$parent = $term->parent;

if (!empty($parent)) {
    $child_of = $parent;
} else {
    $child_of = $term->term_id;
}

$terms = get_terms( array(
    'taxonomy'      => 'product_cat',
    'child_of'      => $child_of,
) );

if ($terms) {
    foreach ( $terms as $category ) {

        $category_id = $category->term_id;
        $category_slug = $category->slug;
        $category_name = $category->name;
        $category_desc = $category->description;

        echo '<div class="'.$category_slug.'">';

        echo '<h2>'.$category_name.'</h2>';

        if ($category_desc) {
            echo '<p>'.$category_desc.'</p>';
        }

        $products_args = array(
            'post_type'     => 'product', 
            'tax_query'     => array( 
                array(
                    'taxonomy' => 'product_cat',
                    'field'    => 'term_id',
                    'terms'    => $category_id, 
                ),
            ),
        );

        $products = new WP_Query( $products_args );

        if ( $products->have_posts() ) { // only start if we hace some products

            // START some normal woocommerce loop

            woocommerce_product_loop_start();

            if ( wc_get_loop_prop( 'total' ) ) {

                while ( $products->have_posts() ) : $products->the_post();

                    /**
                     * Hook: woocommerce_shop_loop.
                     *
                     * @hooked WC_Structured_Data::generate_product_data() - 10
                     */
                    do_action( 'woocommerce_shop_loop' );

                    wc_get_template_part( 'content', 'product' );

                endwhile; // end of the loop.

            }

            woocommerce_product_loop_end();

            // END the normal woocommerce loop

            // Restore original post data, maybe not needed here (in a plugin it might be necessary)
            wp_reset_postdata();

        }
2

is_product_category() is to be used on woocommerce category archive pages only so first make sure that you are on category archive.

instead of category number use category slug name is_product_category('category-slug')

no need to run OR(||) condition just use is_product_category('category-slug1','category-slug2') to get same output

1

Try:

if( is_product_category( 'category1-slug' ) || is_product_category( 'category2-slug' ) ) {
   //...
}

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.