This commit adds a check to verify if a given product has a image before
adding the image node to the structured data output. If there is no
image, the image node should be omitted instead of display with `false`
as its value.
If rating is not mandatory on comments, generate_product_data() produces incorrect output.
For example items with comments but no reviews are output with rating of zero which is getting flagged in google webmasters tools under the heading "Rating is missing required best and/or worst values".
The output google is objecting to is eg:
"aggregateRating":{"@type":"AggregateRating","ratingValue":"0","reviewCount":1}}
postmeta in this case is:
_wc_average_rating 0
_wc_rating_count 0.0
_wc_review_count 1
product lookup table is reporting
rating_count 0
average_rating 0.00
the meta is correct and reflected in the product object, it is the test that is wrong since
if $product->get_review_count()
may be true even if there are no ratings.
This is solved by substituting get_rating_count() which has the correct number of ratings.
With the removal of generation on archives, the filters `woocommerce_structured_data_product_limit` and `woocommerce_structured_data_product_limited` have no context nor purpose. This commit removes them so full schema is generated when used.
This prevents warnings about 'image', 'description' and 'sku'.
'brand' included as empty fields just to register.
Note that warnings still comes for empty fields like 'sku' or 'brand'.
This is the message that should throw from Google Console:
> The brand field is recommended. Please provide a value if available.
Since is recommended, this PR should solve the max warnings as possible.
Fixes#22842
I noticed that the structured data generated by WooCommerce doesn't pass Google Structured Data validation (https://search.google.com/structured-data/testing-tool) due to the following error in the last element of the BreadcrumbList:
"The value provided for item.id must be a valid URL."
This happens because the code was explicitly omitting the URL for the last element. This commit simply removes this check and now an URL is included for every element (unless there is no URL set).
This commit removes an unneeded call to wc_clean() before outputting product structured data to the browser. wp_json_encode() is called before the data is sent to the browser and it should be enough to escape it. wc_clean(), which calls sanitize_text_field() internally, was causing issues with the product URL when its name contained some non-ASCII characters (see #21057 for examples of characters).
- Replaced outdated `priceSpecification` with `AggregateOffer`.
- Restructured data according to Google's latest specifications.
- Use product short_description if available.