woocommerce get product of a category code example

Example 1: get product image woocommerce

// Use $post->ID to get from post object
$id = $loop->post->ID
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $id ), 'single-post-thumbnail' );

<img src="<?php echo $image[0]; ?>" data-id="<?php echo $id; ?>" />

Example 2: woocommerce get category imgae

// verify that this is a product category page
if ( is_product_category() ){
    global $wp_query;

    // get the query object
    $cat = $wp_query->get_queried_object();

    // get the thumbnail id using the queried category term_id
    $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true ); 

    // get the image URL
    $image = wp_get_attachment_url( $thumbnail_id ); 

    // print the IMG HTML
    echo "<img src='{$image}' alt='' width='762' height='365' />";
}

Tags:

Php Example