How can I change the placeholder image for products in WooCommerce

Table of Contents

When WooCommerce is installed, a placeholder image for the products is automatically created and entered into the database. In this tutorial I would like to show you how you can easily replace this image.

option_{$option} #

First, upload the image you want to your WordPress environment via the media library. Then you save the path to the file. You can find the path in the attachment details.

After that, go to the functions.php of your child theme and add the following PHP snippet there:

/**
 * @param $value
 * @param $option
 *
 * @return mixed|string
 */
function wc_change_default_image($value, $option): string
{
    if ('woocommerce_placeholder_image' !== $option) {
        return $value;
    }

    // Add the path to the image, e.g.: 
    $src = '/wp-content/uploads/2023/02/beispiel.jpg';

    // If the image does not exist return the default value
    if (!file_exists(get_home_path() . $src)) {
        return $value;
    }

    // return the image 
    return $src;
}

/*
 * Filter: apply_filters( "option_{$option}", mixed $value, string $option )
 */
add_filter('option_woocommerce_placeholder_image', 'wc_change_default_image', 10, 2);

That’s about it. Now your image will always be used instead of WooCommerce’s default placeholder image.

Have fun with it.

Hier klicken, um den Beitrag zu bewerten
[Gesamt: 0 Durchschnitt: 0]

Leave A Comment

Title