get_post_data( $post_id ); if ( empty( $post_data ) ) { return array( '@type' => $this->type ); } $schema = array( '@type' => $this->type, 'name' => $post_data['title'], 'description' => $post_data['excerpt'], 'url' => $post_data['url'], ); // Image. $image = $this->get_image_data( $post_id ); if ( ! empty( $image ) ) { $schema['image'] = $image; } // WooCommerce integration. if ( function_exists( 'wc_get_product' ) ) { $product = wc_get_product( $post_id ); if ( $product ) { // SKU. $sku = $product->get_sku(); if ( ! empty( $sku ) ) { $schema['sku'] = $sku; } // Brand from product attributes. $brand = $product->get_attribute( 'brand' ); if ( empty( $brand ) ) { $brand = $product->get_attribute( 'pa_brand' ); } if ( ! empty( $brand ) ) { $schema['brand'] = array( '@type' => 'Brand', 'name' => $brand, ); } // Offers. $price = $product->get_price(); $currency = function_exists( 'get_woocommerce_currency' ) ? get_woocommerce_currency() : 'USD'; if ( '' !== $price ) { $schema['offers'] = array( '@type' => 'Offer', 'price' => $price, 'priceCurrency' => $currency, 'availability' => $product->is_in_stock() ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock', 'url' => $post_data['url'], 'priceValidUntil' => gmdate( 'Y-12-31' ), ); // Sale price. $sale_price = $product->get_sale_price(); if ( ! empty( $sale_price ) && $sale_price !== $price ) { $schema['offers']['price'] = $sale_price; } } // Aggregate rating. $rating_count = $product->get_review_count(); $avg_rating = $product->get_average_rating(); if ( $rating_count > 0 && $avg_rating > 0 ) { $schema['aggregateRating'] = array( '@type' => 'AggregateRating', 'ratingValue' => $avg_rating, 'reviewCount' => $rating_count, 'bestRating' => 5, 'worstRating' => 1, ); } // Reviews. $reviews = $this->get_product_reviews( $post_id ); if ( ! empty( $reviews ) ) { $schema['review'] = $reviews; } // GTIN / MPN. $gtin = $product->get_meta( '_gtin' ); if ( ! empty( $gtin ) ) { $schema['gtin'] = $gtin; } $mpn = $product->get_meta( '_mpn' ); if ( ! empty( $mpn ) ) { $schema['mpn'] = $mpn; } } } // Merge AI-detected fields. $schema = $this->merge_fields( $schema, $fields, array( 'name', 'description', 'image', 'brand', 'sku', 'gtin', 'mpn', 'offers', 'aggregateRating', 'review', 'color', 'material', 'weight', ) ); return $schema; } /** * Get WooCommerce product reviews as schema.org Review objects. * * @param int $post_id The product post ID. * @return array Array of Review schema objects. */ private function get_product_reviews( $post_id ) { $comments = get_comments( array( 'post_id' => $post_id, 'status' => 'approve', 'type' => 'review', 'number' => 5, ) ); if ( empty( $comments ) ) { return array(); } $reviews = array(); foreach ( $comments as $comment ) { $rating = get_comment_meta( $comment->comment_ID, 'rating', true ); $review = array( '@type' => 'Review', 'author' => array( '@type' => 'Person', 'name' => $comment->comment_author, ), 'datePublished' => gmdate( 'c', strtotime( $comment->comment_date_gmt ) ), 'reviewBody' => $comment->comment_content, ); if ( $rating ) { $review['reviewRating'] = array( '@type' => 'Rating', 'ratingValue' => (int) $rating, 'bestRating' => 5, 'worstRating' => 1, ); } $reviews[] = $review; } return $reviews; } }