Wie kann ich das Platzhalter-Bild für Produkte in WooCommerce austauschen

Marc Wag­ner

Febru­ar 2, 2023

1 min read|

Bei der Instal­la­ti­on von Woo­Com­mer­ce wird auto­ma­tisch ein Plat­z­hal­­ter-Bild für die Pro­duk­te ange­legt und in die Daten­bank ein­ge­tra­gen. Wie du die­ses Bild bequem aus­tau­schen kannst, möch­te ich dir heu­te in die­ser Anlei­tung demons­trie­ren.

option_{$option} #

Als Ers­tes lädst du über die Media­thek das gewünsch­te Bild in dei­ne Word­Press Umge­bung hoch. Anschlie­ßend sicherst du dir den Pfad zur Datei. Den Pfad fin­dest du in den Anhang Details.

Anschlie­ßend wech­selst du in die functions.php dei­nes Child-The­­mes und fügst dort das fol­gen­de PHP Snip­pet ein:

/**
 * @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);

Das war es auch schon. Nun wird immer anstatt dem Stan­­dard-Plat­z­hal­­ter Bild von Woo­Com­mer­ce dein Bild ver­wen­det.

Viel Spaß damit.

88e86fcb816eff22bc917094df2862d8dd5c0e978b333e6dd5f36f808990c261 96

Arti­kel von:

Marc Wag­ner

Hi Marc here. I’m the foun­der of Forge12 Inter­ac­ti­ve and have been pas­sio­na­te about buil­ding web­sites, online stores, appli­ca­ti­ons and SaaS solu­ti­ons for busi­nesses for over 20 years. Befo­re foun­ding the com­pa­ny, I alre­a­dy work­ed in publicly lis­ted com­pa­nies and acqui­red all kinds of know­ledge. Now I want to pass this know­ledge on to my cus­to­mers.

Hast du eine Fra­ge? Hin­ter­lass bit­te einen Kom­men­tar