RavanH / xml-sitemap-feed

XML Sitemap & Google News feeds
GNU General Public License v2.0
16 stars 21 forks source link

WooCommerce products do not include <image:image> in sitemap #24

Closed gerdneuman closed 5 years ago

gerdneuman commented 6 years ago

Hey,

let me first again say thank you for this wonderful, feature-rich and painlessly working plugin!

Now the "issue": WooCommerce products include images (gallery style) and also a featured image. However, unlike in posts or pages these do not seem to be included. This is especially strange for the featured image since I think it uses nothing different than normal wordpress pages/posts regarding the API.

Here's an example you might want to check: view-source:https://www.flyingroasters.de/sitemap-posttype-product.xml

See https://docs.woocommerce.com/document/adding-product-images-and-galleries/#section-2

RavanH commented 6 years ago

Hi Gerd, thanks for reporting. Will look into this before the next release :)

RavanH commented 6 years ago

Hi Gerd, tested this on a few Woo sites but each time the product sitemap shows images (either the featured or all attached, depending on the settings) ... may I ask which plugin version you are using? Any other Woocommerce extensions? A special Woocommerce (compatible) theme that does special things with product images?

gerdneuman commented 5 years ago

Sorry for getting back this late, I was ill.

I use the latest plugin version. I now switched to the default Storefront theme (default for WooCommerce) and disabled all plugins except WooCommerce. Still a all product entrys in sitemap-posttype-product.xml look like this:

<url>
        <loc>https://fr.test/shop/funky-filter-coffee/</loc>
        <priority>0.5</priority>
        <lastmod>2018-12-03T20:46:40+00:00</lastmod>
 </url>

(using my local *.test staging server here)

Some more infos from the WooCommerce status page:

### Active Plugins (2) ###

WooCommerce: von Automattic – 3.5.2
XML Sitemap & Google News: von RavanH – 5.0.7

### Theme ###

Name: Storefront
Version: 2.4.1
Author URL: https://woocommerce.com/
gerdneuman commented 5 years ago

Uh, I found something, in the xml sitemap settings if I change the Add image tags for from Attached images (default) to the main Featured image then this image is included in the XML correctly:

<url>
        <loc>https://fr.test/shop/funky-filter-coffee/</loc>
        <priority>0.5</priority>
        <lastmod>2018-12-07T13:18:40+00:00</lastmod>
        <image:image>
            <image:loc>https://fr.test/wp-content/uploads/2016/02/Funky.jpg</image:loc>
            <image:title><![CDATA[Funky]]></image:title>
        </image:image>
 </url>

But in WooCommerce products (see very first comment of this bug) you have one featured product image and the so called product gallery which shows all other product images below the main one (as one is used from ebay or amazon when a product has several images). It's configured like: image

or better see official: https://docs.woocommerce.com/document/adding-product-images-and-galleries/#section-2

I guess this bug report is then that Add image tags for from Attached images (default) should include the main feature and all other images as in the gallery.

I googled a bit and here's how one would get the gallery product images: https://stackoverflow.com/questions/37274685/woocommerce-get-image-galleries-by-product-id

RavanH commented 5 years ago

Hmmm, I think the meaning of "Attached" is confusing here. It's when an image is bound to a certain post, which WordPress does by default when you upload an image to insert it into a post. But if the image was already uploaded and bound to another post, then inserting it on a product page will not make it "attached" to that product. One media file can only be attached to one post.

In such a case, a product (or any other post type) may exist with images inserted (or added to the product gallery) without having any images "attached" in the WordPress sense of the word.

You can see these attachments on the Media > Library page if you switch to the List view. There is a column called "Uploaded to" where you see either a post name or the word "(Unattached)" if the file was uploaded directly to the media library or if the original post was deleted.

Could this be going on?

gerdneuman commented 5 years ago

I looked for the specific product images in the Media Library and the "Uploaded to" column has the correct product there. So this "mapping" seems to be right and working, but still xml sitemap does not catch up the images.

Working both for images that are "featured" main image and for product gallery. I just checked several images and products.

Not sure if products are doing anything different when you "uploading" new images via content editor uploader vs uploading via featured image vs. via uploading via product galley.

Well, but, since you cannot reproduce then maybe let's close. I thought this is a "low hanging fruit" but seems to be more complicated probably :)

RavanH commented 5 years ago

If the Uploaded to column shows the correct product, then these images should show up in the sitemap when the option "Attached images" is selected. Very strange that I cannot reproduce this... Does it happen only for products or is this the same for other post types on your site?

gerdneuman commented 5 years ago

I only have posts, pages, products. It's working for posts and pages.

Now I am curious: I could set a breakpoint in a php debugger to see where nothing is returned. Where would be the relevant code lines to do so?

RavanH commented 5 years ago

You'd have to find out two things:

  1. Does line 10 in views/feed-sitempap-post_type.php extract ( xmlsf_do_tags( get_query_var('post_type') ) ); create a $image variable when the Include images tag option for Products is set to anything other than None. The function xmlsf_do_tags() is defined in models/public/sitemap.php line 92 where $type must be "product" at this point...

  2. Once that is established, the next thing would be to check if the function xmlsf_get_images() defined in models/public/shared.php returns a non-empty nested array where each contains a non-empty "loc" key.

Looking at your current products sitemap, I see no urlset image schema attributes set. Supposing you still have the option Include image tags for Products set, this would mean that the check at step one above will fail already. But why? Does get_query_var('post_type') not return "product" on the product sitemap request? Or does xmlsf_do_tags('product') does not return an array with an "image" key?

RavanH commented 5 years ago

Quick check could be to replace get_query_var('post_type') on line 10 in views/feed-sitempap-post_type.php with "product" ... then make sure on your Settings > XML Sitemap page, the Products post type has the option to include images set to "Attached".

gerdneuman commented 5 years ago

Supposing you still have the option Include image tags for Products set …

This let me look at the Settings again, where I noticed that Automatic Priority calculation is checked for posts and pages but not for products:

image

Just out of curiosity and to make sure all 3 post types have the same settings I activated the Automatic Priority calculation checkbox and strangely: With this setting activated it is working now!

I have no idea why this setting has such an (side) effect. I also think that I never fiddling with this setting before, so I think it was off already by default.

RavanH commented 5 years ago

That really is the weirdest thing. I can't think of any way the Automatic Priority calculation option could have an impact on the image tags.

It's off by default indeed, as Priority is officially ignored by Google, there is not much value in adding the extra load of dynamic calculation be default, unless the user specifically wants that. But whether it's on or off, it really should have no effect on the images in the sitemap.

I've tested on several installations various combinations of off/on spread across the different post types but could not reproduce the issue. Do the images disappear again when you deactivate the Automatic Priority calculation option?

gerdneuman commented 5 years ago

No, it does not make a difference. Still working now. I am now equally puzzled like you :thinking: I really have no idea why it was not working before, also I have no caching locally... Anyway, since it is working now for me, you cannot reproduce (me neither now), let's close it :)