Adding ALT and TITLE on product images (in category pages).

This simple snippet within functions.php will add ALT and TITLE tags to the images. I haven’t discovered how to add the same to the product category thumbnails yet and will update when I have sourced that information.

// ALT AND TITLE ON IMAGES
add_filter(
    "wp_get_attachment_image_attributes",
    "change_attachement_image_attributes",
    20,
    2
);
function change_attachement_image_attributes($attr, $attachment)
{
    // Get post parent
    $parent = get_post_field("post_parent", $attachment);

    // Get post type to check if it's product
    $type = get_post_field("post_type", $parent);
    if ($type != "product") {
        return $attr;
    }

    /// Get title
    $title = get_post_field("post_title", $parent);

    if ($attr["alt"] == "") {
        $attr["alt"] = $title;
        $attr["title"] = $title;
    }

    return $attr;
}