'Replace WooCommerce placeholder image based on product category

I'm trying to change default image placeholder for products with no images to images according to their product category.

I'm using:

add_filter( 'woocommerce_placeholder_img_src', 'my_custom_woocommerce_placeholder', 10 );

    //  Function to return new placeholder image URL. 
    function my_custom_woocommerce_placeholder( $image_url )
     {  
        if ($product_category == 'category1') {
            $image_url = 'http://website.com/imageforCategory.png'; 
        }
        elseif ($product_category == 'category2') {
            $image_url = 'http://website.com/imageforCategory.png'; 
        }
        else
        {
            $image_url = 'http://website.com/defaultImage.png'; 
        }
        
        return $image_url;
     }

I'm stuck at getting product category ($product_category) detected in functions.php file of my theme.

In content-single-product.php page I'm just defining

global $product 

and then just using

$product->get_categories();

to get the category of product.

How do I do it in functions.php of my theme?



Solution 1:[1]

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 ..

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1