dev tester
dev tester

Reputation: 337

is_product_category() not working woocommerce

I am trying to change the thumbnail dimensions for a specific category. For this, I added a custom field of the checkbox in the woocommerce category which admin will tick if need custom dimensioned thumbnail.

Now the issue is that I am trying to check if its product category page and if that category has that custom field selected or not. But the condition is not working.

Here is my code:

    // add image size for the portrait size
    function custom_thumbnail_dimensions()
    {
        add_image_size( 'portrait_size', 300 , 440, $crop = false );
    }
    add_action('init','custom_thumbnail_dimensions');
    /**
     * WooCommerce Loop Product Thumbs
     **/
     if ( ! function_exists( 'woocommerce_template_loop_product_thumbnail' ) ) {
        function woocommerce_template_loop_product_thumbnail() {
            printf( '<span class="et_shop_image">%1$s<span class="et_overlay"></span></span>',woocommerce_get_product_thumbnail_category());
        } 
     }
    /**
     * WooCommerce Product Thumbnail
     **/
     if ( ! function_exists( 'woocommerce_get_product_thumbnail_category' ) ) {

        function woocommerce_get_product_thumbnail_category( $size = 'portrait_size', $placeholder_width = 0, $placeholder_height = 0  ) {
            global $post, $woocommerce;
            if ( ! $placeholder_width )
                $placeholder_width = $woocommerce->get_image_size(300);
            if ( ! $placeholder_height )
                $placeholder_height = $woocommerce->get_image_size(440);

                $output = '<div class="imagewrapper">';
                if ( has_post_thumbnail() ) {

                    $output .= get_the_post_thumbnail( $post->ID, $size ); 

                } else {

                    $output .= '<img src="'. woocommerce_placeholder_img_src() .'" alt="Placeholder" width="' . $placeholder_width . '" height="' . $placeholder_height . '" />';

                }

                $output .= '</div>';

                return $output;
        }
    }
    function check_page_type()
    {
        if(is_product_category())
        {
            $cate = get_queried_object();
        print_r($cate);
            $term_id = $cate->term_id;
            echo $term_id."test";
            $is_portrait = get_term_meta($term_id, 'wh_layout_portrait', true);
            if($is_portrait == "yes")
            {
                remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);
                add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);
           }
        }
    }
    add_action('init','check_page_type');

Can anyone help me and let me know where i am wrong. Why it's not working.

Thanks in Advance

Upvotes: 0

Views: 4141

Answers (2)

Hue Man
Hue Man

Reputation: 309

When the shop page initializes without a category being selected then is_product_category() returns false. As soon as you select a category then it returns true. Hope this helps.

Upvotes: 0

dineshkashera
dineshkashera

Reputation: 1502

Please check your condition with global $wp_query.

 global $wp_query;
 $category_name = $wp_query->query_vars['product_cat'];

 if( $category_name ) {
    $category_object = get_term_by('name', $category_name, 'product_cat');
    $category_id = $category_object->term_id;
 }

I hope it helps you.

In your functions , please replace init hook with woocommerce_before_shop_loop

function check_page_type()
    {
        if(is_product_category())
        {
            $cate = get_queried_object();
        print_r($cate);
            $term_id = $cate->term_id;
            echo $term_id."test";
            $is_portrait = get_term_meta($term_id, 'wh_layout_portrait', true);
            if($is_portrait == "yes")
            {
                remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);
                add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);
           }
        }
    }
    add_action('woocommerce_before_shop_loop','check_page_type');

I hope it help you.

Upvotes: 3

Related Questions