Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebP (picture tags) does not work with WooCommerce variable products #495

Closed
markonikolic985 opened this issue May 7, 2020 · 3 comments · Fixed by #587
Closed

WebP (picture tags) does not work with WooCommerce variable products #495

markonikolic985 opened this issue May 7, 2020 · 3 comments · Fixed by #587
Assignees
Milestone

Comments

@markonikolic985
Copy link
Contributor

It looks like using WebP display (picture tags) won't work fine with WooCommerce variable products.

Tickets are tagged "img_variations" on HS - https://secure.helpscout.net/search/?query=tag%3Aimg_variations

@hogash
Copy link

hogash commented Oct 9, 2020

Problem is mainly caused by $.fn.wc_variations_image_update from wp-content/plugins/woocommerce/assets/js/frontend/add-to-cart-variation.js in which src and all those data attributes are added on the parent picture tag, not the inner's source.

@iCaspar
Copy link
Contributor

iCaspar commented Mar 29, 2021

Reproduced: ✔️

Root Cause:
It is as @hogash says, above.
Specifically, the product variation attributes are set on

$product_img      = $product_img_wrap.find( '.wp-post-image' ),

But when using the <picture> tags the image urls in the <source> and <img> tags within the <picture> are not targeted/updated.

Proposed solution:
We can use imagify filters imagify_picture_source_attributes and imagify_picture_img_attributes to add wp-post-image class to the inner elements, and this seems to work locally for swapping out the images to load variation images.
We also need to filter on woocommerce_gallery_image_html_attachment_image_params to remove the wp-post-image class from the outer picture tag to enable the initial post image to be reset when clicking back to the "Choose an option" toggle in the menu.

Sample proof-of-concept code:

add_filter( 'imagify_picture_img_attributes', 'cg_test_wc_picture_tag', 10, 2 );
add_filter( 'imagify_picture_source_attributes', 'cg_test_wc_picture_tag', 10, 2 );
function cg_test_wc_picture_tag( $attributes, $image) {
    $attributes['class'] = 'wp-post-image';
    return $attributes;
}

add_filter( 'woocommerce_gallery_image_html_attachment_image_params', 'cg_test_remove_wc_post_image', 10, 4 );
function cg_test_remove_wc_post_image( $attributes, $attachment_id, $image_size, $image ) {
    if (isset($attributes['class'])) {
        unset($attributes['class']);
    }
    return $attributes;
}

Because all these filters should only be applied on the variable products, we should first test for if( $product->is_type( 'variable' ) ){ ... } and then add the filters on woocommerce_single_product_summary, priority 25, and remove them immediately on woocommerce_single_product_summary, priority 35, to prevent other images on the page from having the wp-post-image class applied.

Estimate Effort:
[S]

@iCaspar iCaspar added GROOMING IN PROGRESS effort [S] 1-2 days of estimated development time and removed GROOMING IN PROGRESS needs: grooming labels Mar 29, 2021
@remyperona remyperona added this to the 1.9.16 milestone Apr 12, 2021
@remyperona remyperona modified the milestones: 1.9.16, 1.9.15 May 4, 2021
@vmanthos
Copy link
Contributor

@iCaspar FYI, I had opened a ticket with WooCommerce about this:
woocommerce/woocommerce#29213

According to their reply, the issue is related to the fact that the Photoswipe library they use to display the lightbox, doesn't support <picture>.

@iCaspar iCaspar self-assigned this Jul 7, 2021
iCaspar added a commit that referenced this issue Jul 13, 2021
#587)

* Add WooCommerce Compatibility Class

* Add docblocks and phpcs compat

* Remove unused parameter (codacity)

* Fix filename update
@iCaspar iCaspar mentioned this issue Jul 15, 2021
iCaspar added a commit that referenced this issue Jul 15, 2021
#587)

* Add WooCommerce Compatibility Class

* Add docblocks and phpcs compat

* Remove unused parameter (codacity)

* Fix filename update
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants