php - Replace WooCommerce placeholder image based on product category

one text

Solution:

You could use has_term() - Checks if the current post has any of given terms.

So you get:

// Single product page
function filter_woocommerce_placeholder_img_src( $src ) {
    // Get the global product object
    global $product;

    // Is a WC product
    if ( is_a( $product, 'WC_Product' ) ) {
        // Has term (product category)
        if ( has_term( 'categorie-1', 'product_cat', $product->get_id() ) ) {
            $src = 'http://website.com/imageforCategory1.png';
        } elseif ( has_term( array('categorie-2', 'categorie-3'), 'product_cat', $product->get_id() ) ) {
            $src = 'http://website.com/imageforCategory2.png';
        }
    }
    
    return $src;
}
add_filter( 'woocommerce_placeholder_img_src', 'filter_woocommerce_placeholder_img_src', 10, 1 );

// Archive/Shop page
function filter_woocommerce_placeholder_img ( $image_html, $size, $dimensions ) {
    $dimensions = wc_get_image_size( $size );
    
    $default_attr = array(
        'class' => 'woocommerce-placeholder wp-post-image',
        'alt'   => __( 'Placeholder', 'woocommerce' ),
    );
    
    $attr = wp_parse_args( '', $default_attr );
    
    $image      = wc_placeholder_img_src( $size );
    $hwstring   = image_hwstring( $dimensions['width'], $dimensions['height'] );
    $attributes = array();

    foreach ( $attr as $name => $value ) {
        $attribute[] = esc_attr( $name ) . '="' . esc_attr( $value ) . '"';
    }

    $image_html = '<img src="' . esc_url( $image ) . '" ' . $hwstring . implode( ' ', $attribute ) . '/>';
    
    return $image_html;
}
add_filter( 'woocommerce_placeholder_img', 'filter_woocommerce_placeholder_img', 10, 3 );

Note: the woocommerce_placeholder_img hook, used on archive/shop page returns $image_html. That string can be adapted to your needs. Such as image src, class, alt, size, etc ..

Source