woocommerce / woocommerce

A customizable, open-source ecommerce platform built on WordPress. Build any commerce solution you can imagine.
https://woocommerce.com
9.29k stars 10.73k forks source link

Request: Filter for wc_get_gallery_image_html #31280

Open seldimi opened 2 years ago

seldimi commented 2 years ago

Hello. Here is a request that would be handy for many developers. On product archives and single product page, if we want to alter the alt text from an image, we can use the hook wp_get_attachment_image_attributes and supply an alt text (for example product title)

That is not working on flexslider though (wc-product-gallery-slider). On the function wc_get_gallery_image_html(), we have a filter woocommerce_gallery_image_html_attachment_image_params but it cannot affect the "alt" text. "Alt" text is read from attachment's _wp_attachment_image_alt custom field.

Can we add a filter so we can modify alt, as we do with the wp_get_attachment_image_attributes?

That would be handy for having alt texts related to products, with no need to modify wc_get_gallery_image_html()

seldimi commented 2 years ago

Can be easily done by the snippet below, but would be more clear to be on the same hook

add_filter( 'get_post_metadata', 'get_alt_for_thumbs_flexslider', 10, 3 );
function get_alt_for_thumbs_flexslider( $value, $object_id, $meta_key ) {
    if ( '_wp_attachment_image_alt' === $meta_key ) {
        return get_the_title($object_id);
    }

    return $value;
}
rodelgc commented 2 years ago

Hi @seldimi,

Thank you for taking the time to share this idea, we really appreciate your help.

While this is clearly a great improvement/idea we won’t be able to tackle it in the upcoming weeks. We’re going to add it to our backlog where it will be considered for future releases.

Stay tuned for updates.

vijayhardaha commented 2 years ago

Is it possible to allow override wc_get_gallery_image_html function in child theme? right now, we can't do it if we need to change something quickly.

rodelgc commented 2 years ago

Hi @vijayhardaha, thanks for the question. I'll remove the type: enhancement label and replace it with needs developer feedback so the Core team could take a look.

barryhughes commented 2 years ago

Is it possible to allow override wc_get_gallery_image_html function in child theme?

I'm afraid not, it's not a 'pluggable' function.

Can we add a filter so we can modify alt, as we do with the wp_get_attachment_image_attributes?

Yes, I think so. Per our policy on adding actions and filters it would definitely seem reasonable to add a filter hook so the output of that function can be replaced (and, args such as $alt_text could be made available to callbacks).

Would that work for you?

seldimi commented 2 years ago

Sure, that's also similar to wp_get_attachment_image_attributes filter, so that would do :)

barryhughes commented 2 years ago

Great: I've marked this as an enhancement and we'll try to get to it as quickly as we can—though we do have some higher priority items we're working through. If you (or anyone else interested in this) has the space to do so, we'd also be open to a PR introducing a new filter as discussed.

saqib59 commented 1 year ago

@barryhughes Is there any filter available which I can use with wc_get_gallery_image_html() function?

barryhughes commented 1 year ago

Hi @saqib59,

https://github.com/woocommerce/woocommerce/blob/6.8.2/plugins/woocommerce/includes/wc-template-functions.php#L1573-L1599

There are a number of existing filter hooks within that function—so it really depends on what you want to do (and, to be clear, the feature proposed in this issue has not yet been implemented).

wodim commented 1 year ago

If you are using the default templates, you can use the woocommerce_single_product_image_thumbnail_html filter to edit the output of the html that is displayed in single product thumbnails.

add_filter( 'woocommerce_single_product_image_thumbnail_html', function( $html, $attachment_id ) {
    return $html;
}, 10, 2 );