-1

I am trying to add multiple product categories -to display category thumbnail has product badge in catalog page

add_action ('woocommerce_before_shop_loop_item_title','alt_category_badge', 99);
add_action ('woocommerce_product_thumbnails','alt_category_badge', 100);
function alt_category_badge() {
  global $product;
  if ( is_product()){
    global $post;
    $brands_id = get_term_by('slug', 'sunglasses', 'product_cat');
    $terms = get_the_terms( $post->ID, 'product_cat' );
    foreach ( $terms as $term ){
      if($term->parent === $brands_id->term_id) {
        $category_name = $term->name;
        $category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
        $image = wp_get_attachment_url($category_thumbnail);
        if ( $image )
          echo '<div class="brand-icon-logo sunglasses" ><img height="100%" width="35%" src="'.$image.'" alt="'.$category_name.' sunglasses online in dubai"></div>';
      }
    }
  }
}
  • _"I am trying to ..."_ - uh huh, I see. And where is the "and my problem is ..." part, that would actually make this into a _question?_ – CBroe Feb 24 '22 at 11:56
  • thumbnail is only display on frontend for specific category - try to add another product category in the same code – Muhammed Faheem Feb 24 '22 at 12:00
  • So what have you done so far to debug this then? Did you verify that `$terms` contains all the categories you expect? Did you verify the `if` condition was true for each of them? Did you check what the `wp_get_attachment_url` call actually returned? – CBroe Feb 24 '22 at 12:02

2 Answers2

0
/**
 * Display product category thumbnail.
 *
 */
add_action('woocommerce_before_shop_loop_item_title', 'display_product_category_thumbnail', 20);
function display_product_category_thumbnail()
{
    global $product;

    $productFirstCategory = reset(get_the_terms($product->get_id(), 'product_cat'));

    $small_thumb_size = 'woocommerce_thumbnail';
    $dimensions = wc_get_image_size($small_thumb_size);

    if ($thumbnail_id = get_term_meta($productFirstCategory->term_id, 'thumbnail_id', true)) {
        $image = wp_get_attachment_image_src($thumbnail_id, $small_thumb_size);

        if (is_array($image)) {
            $image = reset($image);
        }

        $image_srcset = function_exists('wp_get_attachment_image_srcset') ? wp_get_attachment_image_srcset($thumbnail_id, $small_thumb_size) : false;
        $image_sizes = function_exists('wp_get_attachment_image_sizes') ? wp_get_attachment_image_sizes($thumbnail_id, $small_thumb_size) : false;
    } else {
        $image = wc_placeholder_img_src();
        $image_srcset = false;
        $image_sizes = false;
    }
        
        if ( $image )
          echo '<div class="brand-icon-logo sunglasses" ><img height="100%" width="35%" src="'.$image.'" alt="'.$category_name.' sunglasses online in dubai"></div>';
    }

0

for specific categorys

/**
 * Display product category thumbnail.
 *
 */
add_action('woocommerce_before_shop_loop_item_title', 'display_product_category_thumbnail', 20);
function display_product_category_thumbnail()
{
    global $product;
    if ( has_term( array( 'catagory1','catagory2'), 'product_cat', $product->get_id() ) ){

    $productFirstCategory = reset(get_the_terms($product->get_id(), 'product_cat'));

    $small_thumb_size = 'woocommerce_thumbnail';
    $dimensions = wc_get_image_size($small_thumb_size);

    if ($thumbnail_id = get_term_meta($productFirstCategory->term_id, 'thumbnail_id', true)) {
        $image = wp_get_attachment_image_src($thumbnail_id, $small_thumb_size);

        if (is_array($image)) {
            $image = reset($image);
        }

        $image_srcset = function_exists('wp_get_attachment_image_srcset') ? wp_get_attachment_image_srcset($thumbnail_id, $small_thumb_size) : false;
        $image_sizes = function_exists('wp_get_attachment_image_sizes') ? wp_get_attachment_image_sizes($thumbnail_id, $small_thumb_size) : false;
    } else {
        $image = wc_placeholder_img_src();
        $image_srcset = false;
        $image_sizes = false;
    }
        
        if ( $image )
          echo '<div class="brand-icon-logo sunglasses" ><img height="100%" width="35%" src="'.$image.'" alt="'.$category_name.' sunglasses online in dubai"></div>';
    }
}