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 ><img height="100%" width="35%" src="'.$image.'" alt="'.$category_name.' sunglasses online in dubai"></div>';
}
}
}
}
CodePudding user response:
/**
* 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 ><img height="100%" width="35%" src="'.$image.'" alt="'.$category_name.' sunglasses online in dubai"></div>';
}
CodePudding user response:
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 ><img height="100%" width="35%" src="'.$image.'" alt="'.$category_name.' sunglasses online in dubai"></div>';
}
}