How to Get Product Images in WooCommerce Using Code

In WooCommerce development, manipulating product images through code is crucial for creating unique shopping experiences. This guide breaks down a fundamental WordPress code snippet, allowing  you to seamlessly integrate product images into your WooCommerce templates and enhance your online store’s visual appeal.

What is the WooCommerce product image?

In WooCommerce, a product image is a file attached to a product on a WooCommerce site. It’s uploaded through the WordPress Media Library and used to visually display the product on the website’s front-end. Developers can access and manipulate these images using WooCommerce functions to retrieve and display them programmatically in custom functionalities or templates.

Get WooCommerce product image code snippet

This WordPress code snippet is used to retrieve the image URL of a product in a WooCommerce store. Let’s break it down step by step:


// Get the product object based on the product ID
$product   = wc_get_product( $product_id );

// Retrieve the image ID associated with the product
$image_id  = $product->get_image_id();

// Get the image URL using the image ID and specify the image size ('full' in this case)
$image_url = wp_get_attachment_image_url( $image_id, 'full' );


wc_get_product( $product_id ): This function is part of WooCommerce and is used to retrieve a product object based on its unique product ID ($product_id).

$product->get_image_id(): The get_image_id() method is called on the product object to retrieve the attachment (image) ID associated with that product. This ID represents the featured image or the main product image.

wp_get_attachment_image_url( $image_id, 'full' ): This function is a WordPress core function that retrieves the URL of an image attachment based on its ID ($image_id) and the specified image size (‘full’ in this case).

Unlock the Full Potential of Your WooCommerce Customer Data

The 'full' size parameter indicates that the function should retrieve the full-size image. Depending on your needs, you can specify other image sizes like ‘thumbnail’, ‘medium’, or ‘large’.

After executing these lines of code, the variable $image_url will contain the full URL of the product’s featured image. This URL can then be used to display the product image on the front-end of your WooCommerce website like this:

<img src="<?php echo image_url; ?>">

This line outputs an HTML <img> tag with the src attribute set to the URL of the product image. It uses the $image_url variable obtained in the previous step. This code is meant to be used within the context of a WordPress template or product loop where $product_id is defined.

So, when this code is executed on a WordPress page or template, it dynamically generates the image URL using the image_url variable and inserts it as the source (src) of the <img> tag. The result is that the image specified by the URL will be displayed on the web page where this code is implemented.


Programmatically accessing product images in WooCommerce offers developers endless customization possibilities. This code snippet provides a step-by-step guide to retrieving and utilizing product images. By using functions like “wp_get_attachment_image_url”, developers can easily customize and integrate product images into their themes, product gallery images, and plugins. Enhance your WooCommerce website’s visual appeal and customization options with this valuable resource. Embrace the power of code and let your product images take center stage.