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?