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

Display schema for the latest 5 reviews inside product's schema #24278

Merged
merged 5 commits into from Nov 27, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 23 additions & 18 deletions includes/class-wc-structured-data.php
Expand Up @@ -29,7 +29,6 @@ public function __construct() {
add_action( 'woocommerce_before_main_content', array( $this, 'generate_website_data' ), 30 );
add_action( 'woocommerce_breadcrumb', array( $this, 'generate_breadcrumblist_data' ), 10 );
add_action( 'woocommerce_single_product_summary', array( $this, 'generate_product_data' ), 60 );
add_action( 'woocommerce_review_meta', array( $this, 'generate_review_data' ), 20 );
claudiosanches marked this conversation as resolved.
Show resolved Hide resolved
claudiosanches marked this conversation as resolved.
Show resolved Hide resolved
add_action( 'woocommerce_email_order_details', array( $this, 'generate_order_data' ), 20, 3 );

// Output structured data.
Expand Down Expand Up @@ -275,38 +274,44 @@ public function generate_product_data( $product = null ) {
'reviewCount' => $product->get_review_count(),
);

// Markup most recent rating/review.
// Markup 5 most recent rating/review.
$comments = get_comments(
array(
'number' => 1,
'number' => 5,
claudiosanches marked this conversation as resolved.
Show resolved Hide resolved
'post_id' => $product->get_id(),
'status' => 'approve',
'post_status' => 'publish',
'post_type' => 'product',
'parent' => 0,
'meta_key' => 'rating',
'orderby' => 'meta_value_num',
'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
claudiosanches marked this conversation as resolved.
Show resolved Hide resolved
array(
'key' => 'rating',
'type' => 'NUMERIC',
'compare' => '>',
'value' => 0,
),
),
)
);

if ( $comments ) {
$markup['review'] = array();
foreach ( $comments as $comment ) {
$rating = get_comment_meta( $comment->comment_ID, 'rating', true );

if ( ! $rating ) {
continue;
}

$markup['review'] = array(
'@type' => 'Review',
'reviewRating' => array(
'@type' => 'Rating',
'ratingValue' => $rating,
),
'author' => array(
$markup['review'][] = array(
'@type' => 'Review',
'author' => array(
'@type' => 'Person',
'name' => get_comment_author( $comment->comment_ID ),
),
'itemReviewed' => array(
'@type' => 'Product',
'name' => $product->get_name(),
),
'reviewRating' => array(
'@type' => 'Rating',
'ratingValue' => get_comment_meta( $comment->comment_ID, 'rating', true ),
),
'datePublished' => get_comment_date( 'c', $comment->comment_ID ),
);
}
}
Expand Down
1 change: 0 additions & 1 deletion templates/single-product/review.php
Expand Up @@ -48,7 +48,6 @@
* The woocommerce_review_meta hook.
*
* @hooked woocommerce_review_display_meta - 10
* @hooked WC_Structured_Data::generate_review_data() - 20
*/
do_action( 'woocommerce_review_meta', $comment );

Expand Down