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0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / class-wc-structured-data.php
woocommerce / includes Last commit date
abstracts 2 years ago admin 2 years ago blocks 5 years ago cli 2 years ago customizer 2 years ago data-stores 2 years ago emails 2 years ago export 2 years ago gateways 2 years ago import 2 years ago integrations 2 years ago interfaces 3 years ago legacy 2 years ago libraries 3 years ago log-handlers 2 years ago payment-tokens 5 years ago queue 4 years ago react-admin 2 years ago rest-api 2 years ago shipping 2 years ago shortcodes 2 years ago theme-support 2 years ago tracks 2 years ago traits 5 years ago walkers 5 years ago wccom-site 2 years ago widgets 2 years ago class-wc-ajax.php 2 years ago class-wc-api.php 4 years ago class-wc-auth.php 2 years ago class-wc-autoloader.php 5 years ago class-wc-background-emailer.php 5 years ago class-wc-background-updater.php 5 years ago class-wc-breadcrumb.php 5 years ago class-wc-cache-helper.php 3 years ago class-wc-cart-fees.php 2 years ago class-wc-cart-session.php 2 years ago class-wc-cart-totals.php 2 years ago class-wc-cart.php 2 years ago class-wc-checkout.php 2 years ago class-wc-cli.php 3 years ago class-wc-comments.php 3 years ago class-wc-countries.php 2 years ago class-wc-coupon.php 2 years ago class-wc-customer-download-log.php 5 years ago class-wc-customer-download.php 4 years ago class-wc-customer.php 2 years ago class-wc-data-exception.php 8 years ago class-wc-data-store.php 3 years ago class-wc-datetime.php 4 years ago class-wc-deprecated-action-hooks.php 2 years ago class-wc-deprecated-filter-hooks.php 3 years ago class-wc-discounts.php 2 years ago class-wc-download-handler.php 3 years ago class-wc-emails.php 2 years ago class-wc-embed.php 5 years ago class-wc-form-handler.php 2 years ago class-wc-frontend-scripts.php 2 years ago class-wc-geo-ip.php 2 years ago class-wc-geolite-integration.php 6 years ago class-wc-geolocation.php 3 years ago class-wc-https.php 2 years ago class-wc-install.php 2 years ago class-wc-integrations.php 5 years ago class-wc-log-levels.php 2 years ago class-wc-logger.php 2 years ago class-wc-meta-data.php 4 years ago class-wc-order-factory.php 3 years ago class-wc-order-item-coupon.php 4 years ago class-wc-order-item-fee.php 4 years ago class-wc-order-item-meta.php 4 years ago class-wc-order-item-product.php 2 years ago class-wc-order-item-shipping.php 4 years ago class-wc-order-item-tax.php 4 years ago class-wc-order-item.php 2 years ago class-wc-order-query.php 4 years ago class-wc-order-refund.php 2 years ago class-wc-order.php 2 years ago class-wc-payment-gateways.php 2 years ago class-wc-payment-tokens.php 3 years ago class-wc-post-data.php 3 years ago class-wc-post-types.php 3 years ago class-wc-privacy-background-process.php 5 years ago class-wc-privacy-erasers.php 4 years ago class-wc-privacy-exporters.php 4 years ago class-wc-privacy.php 2 years ago class-wc-product-attribute.php 4 years ago class-wc-product-download.php 2 years ago class-wc-product-external.php 5 years ago class-wc-product-factory.php 2 years ago class-wc-product-grouped.php 8 years ago class-wc-product-query.php 5 years ago class-wc-product-simple.php 2 years ago class-wc-product-variable.php 3 years ago class-wc-product-variation.php 4 years ago class-wc-query.php 2 years ago class-wc-rate-limiter.php 4 years ago class-wc-regenerate-images-request.php 3 years ago class-wc-regenerate-images.php 2 years ago class-wc-register-wp-admin-settings.php 4 years ago class-wc-rest-authentication.php 3 years ago class-wc-rest-exception.php 5 years ago class-wc-session-handler.php 2 years ago class-wc-shipping-rate.php 3 years ago class-wc-shipping-zone.php 5 years ago class-wc-shipping-zones.php 5 years ago class-wc-shipping.php 4 years ago class-wc-shortcodes.php 2 years ago class-wc-structured-data.php 2 years ago class-wc-tax.php 2 years ago class-wc-template-loader.php 2 years ago class-wc-tracker.php 2 years ago class-wc-validation.php 3 years ago class-wc-webhook.php 2 years ago class-woocommerce.php 3 months ago wc-account-functions.php 2 years ago wc-attribute-functions.php 3 years ago wc-cart-functions.php 2 years ago wc-conditional-functions.php 2 years ago wc-core-functions.php 2 years ago wc-coupon-functions.php 3 years ago wc-deprecated-functions.php 2 years ago wc-formatting-functions.php 2 years ago wc-notice-functions.php 2 years ago wc-order-functions.php 2 years ago wc-order-item-functions.php 3 years ago wc-page-functions.php 2 years ago wc-product-functions.php 2 years ago wc-rest-functions.php 3 years ago wc-stock-functions.php 3 years ago wc-template-functions.php 2 years ago wc-template-hooks.php 2 years ago wc-term-functions.php 3 years ago wc-update-functions.php 2 years ago wc-user-functions.php 2 years ago wc-webhook-functions.php 4 years ago wc-widget-functions.php 5 years ago
class-wc-structured-data.php
575 lines
1 <?php
2 /**
3 * Structured data's handler and generator using JSON-LD format.
4 *
5 * @package WooCommerce\Classes
6 * @since 3.0.0
7 * @version 3.0.0
8 */
9
10 defined( 'ABSPATH' ) || exit;
11
12 /**
13 * Structured data class.
14 */
15 class WC_Structured_Data {
16
17 /**
18 * Stores the structured data.
19 *
20 * @var array $_data Array of structured data.
21 */
22 private $_data = array();
23
24 /**
25 * Constructor.
26 */
27 public function __construct() {
28 // Generate structured data.
29 add_action( 'woocommerce_before_main_content', array( $this, 'generate_website_data' ), 30 );
30 add_action( 'woocommerce_breadcrumb', array( $this, 'generate_breadcrumblist_data' ), 10 );
31 add_action( 'woocommerce_single_product_summary', array( $this, 'generate_product_data' ), 60 );
32 add_action( 'woocommerce_email_order_details', array( $this, 'generate_order_data' ), 20, 3 );
33
34 // Output structured data.
35 add_action( 'woocommerce_email_order_details', array( $this, 'output_email_structured_data' ), 30, 3 );
36 add_action( 'wp_footer', array( $this, 'output_structured_data' ), 10 );
37 }
38
39 /**
40 * Sets data.
41 *
42 * @param array $data Structured data.
43 * @param bool $reset Unset data (default: false).
44 * @return bool
45 */
46 public function set_data( $data, $reset = false ) {
47 if ( ! isset( $data['@type'] ) || ! preg_match( '|^[a-zA-Z]{1,20}$|', $data['@type'] ) ) {
48 return false;
49 }
50
51 if ( $reset && isset( $this->_data ) ) {
52 unset( $this->_data );
53 }
54
55 $this->_data[] = $data;
56
57 return true;
58 }
59
60 /**
61 * Gets data.
62 *
63 * @return array
64 */
65 public function get_data() {
66 return $this->_data;
67 }
68
69 /**
70 * Structures and returns data.
71 *
72 * List of types available by default for specific request:
73 *
74 * 'product',
75 * 'review',
76 * 'breadcrumblist',
77 * 'website',
78 * 'order',
79 *
80 * @param array $types Structured data types.
81 * @return array
82 */
83 public function get_structured_data( $types ) {
84 $data = array();
85
86 // Put together the values of same type of structured data.
87 foreach ( $this->get_data() as $value ) {
88 $data[ strtolower( $value['@type'] ) ][] = $value;
89 }
90
91 // Wrap the multiple values of each type inside a graph... Then add context to each type.
92 foreach ( $data as $type => $value ) {
93 $data[ $type ] = count( $value ) > 1 ? array( '@graph' => $value ) : $value[0];
94 $data[ $type ] = apply_filters( 'woocommerce_structured_data_context', array( '@context' => 'https://schema.org/' ), $data, $type, $value ) + $data[ $type ];
95 }
96
97 // If requested types, pick them up... Finally change the associative array to an indexed one.
98 $data = $types ? array_values( array_intersect_key( $data, array_flip( $types ) ) ) : array_values( $data );
99
100 if ( ! empty( $data ) ) {
101 if ( 1 < count( $data ) ) {
102 $data = apply_filters( 'woocommerce_structured_data_context', array( '@context' => 'https://schema.org/' ), $data, '', '' ) + array( '@graph' => $data );
103 } else {
104 $data = $data[0];
105 }
106 }
107
108 return $data;
109 }
110
111 /**
112 * Get data types for pages.
113 *
114 * @return array
115 */
116 protected function get_data_type_for_page() {
117 $types = array();
118 $types[] = is_shop() || is_product_category() || is_product() ? 'product' : '';
119 $types[] = is_shop() && is_front_page() ? 'website' : '';
120 $types[] = is_product() ? 'review' : '';
121 $types[] = 'breadcrumblist';
122 $types[] = 'order';
123
124 return array_filter( apply_filters( 'woocommerce_structured_data_type_for_page', $types ) );
125 }
126
127 /**
128 * Makes sure email structured data only outputs on non-plain text versions.
129 *
130 * @param WP_Order $order Order data.
131 * @param bool $sent_to_admin Send to admin (default: false).
132 * @param bool $plain_text Plain text email (default: false).
133 */
134 public function output_email_structured_data( $order, $sent_to_admin = false, $plain_text = false ) {
135 if ( $plain_text ) {
136 return;
137 }
138 echo '<div style="display: none; font-size: 0; max-height: 0; line-height: 0; padding: 0; mso-hide: all;">';
139 $this->output_structured_data();
140 echo '</div>';
141 }
142
143 /**
144 * Sanitizes, encodes and outputs structured data.
145 *
146 * Hooked into `wp_footer` action hook.
147 * Hooked into `woocommerce_email_order_details` action hook.
148 */
149 public function output_structured_data() {
150 $types = $this->get_data_type_for_page();
151 $data = $this->get_structured_data( $types );
152
153 if ( $data ) {
154 echo '<script type="application/ld+json">' . wc_esc_json( wp_json_encode( $data ), true ) . '</script>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
155 }
156 }
157
158 /*
159 |--------------------------------------------------------------------------
160 | Generators
161 |--------------------------------------------------------------------------
162 |
163 | Methods for generating specific structured data types:
164 |
165 | - Product
166 | - Review
167 | - BreadcrumbList
168 | - WebSite
169 | - Order
170 |
171 | The generated data is stored into `$this->_data`.
172 | See the methods above for handling `$this->_data`.
173 |
174 */
175
176 /**
177 * Generates Product structured data.
178 *
179 * Hooked into `woocommerce_single_product_summary` action hook.
180 *
181 * @param WC_Product $product Product data (default: null).
182 */
183 public function generate_product_data( $product = null ) {
184 if ( ! is_object( $product ) ) {
185 global $product;
186 }
187
188 if ( ! is_a( $product, 'WC_Product' ) ) {
189 return;
190 }
191
192 $shop_name = get_bloginfo( 'name' );
193 $shop_url = home_url();
194 $currency = get_woocommerce_currency();
195 $permalink = get_permalink( $product->get_id() );
196 $image = wp_get_attachment_url( $product->get_image_id() );
197
198 $markup = array(
199 '@type' => 'Product',
200 '@id' => $permalink . '#product', // Append '#product' to differentiate between this @id and the @id generated for the Breadcrumblist.
201 'name' => wp_kses_post( $product->get_name() ),
202 'url' => $permalink,
203 'description' => wp_strip_all_tags( do_shortcode( $product->get_short_description() ? $product->get_short_description() : $product->get_description() ) ),
204 );
205
206 if ( $image ) {
207 $markup['image'] = $image;
208 }
209
210 // Declare SKU or fallback to ID.
211 if ( $product->get_sku() ) {
212 $markup['sku'] = $product->get_sku();
213 } else {
214 $markup['sku'] = $product->get_id();
215 }
216
217 if ( '' !== $product->get_price() ) {
218 // Assume prices will be valid until the end of next year, unless on sale and there is an end date.
219 $price_valid_until = gmdate( 'Y-12-31', time() + YEAR_IN_SECONDS );
220
221 if ( $product->is_type( 'variable' ) ) {
222 $lowest = $product->get_variation_price( 'min', false );
223 $highest = $product->get_variation_price( 'max', false );
224
225 if ( $lowest === $highest ) {
226 $markup_offer = array(
227 '@type' => 'Offer',
228 'price' => wc_format_decimal( $lowest, wc_get_price_decimals() ),
229 'priceValidUntil' => $price_valid_until,
230 'priceSpecification' => array(
231 'price' => wc_format_decimal( $lowest, wc_get_price_decimals() ),
232 'priceCurrency' => $currency,
233 'valueAddedTaxIncluded' => wc_prices_include_tax() ? 'true' : 'false',
234 ),
235 );
236 } else {
237 $markup_offer = array(
238 '@type' => 'AggregateOffer',
239 'lowPrice' => wc_format_decimal( $lowest, wc_get_price_decimals() ),
240 'highPrice' => wc_format_decimal( $highest, wc_get_price_decimals() ),
241 'offerCount' => count( $product->get_children() ),
242 );
243 }
244 } elseif ( $product->is_type( 'grouped' ) ) {
245 if ( $product->is_on_sale() && $product->get_date_on_sale_to() ) {
246 $price_valid_until = gmdate( 'Y-m-d', $product->get_date_on_sale_to()->getTimestamp() );
247 }
248
249 $tax_display_mode = get_option( 'woocommerce_tax_display_shop' );
250 $children = array_filter( array_map( 'wc_get_product', $product->get_children() ), 'wc_products_array_filter_visible_grouped' );
251 $price_function = 'incl' === $tax_display_mode ? 'wc_get_price_including_tax' : 'wc_get_price_excluding_tax';
252
253 foreach ( $children as $child ) {
254 if ( '' !== $child->get_price() ) {
255 $child_prices[] = $price_function( $child );
256 }
257 }
258 if ( empty( $child_prices ) ) {
259 $min_price = 0;
260 } else {
261 $min_price = min( $child_prices );
262 }
263
264 $markup_offer = array(
265 '@type' => 'Offer',
266 'price' => wc_format_decimal( $min_price, wc_get_price_decimals() ),
267 'priceValidUntil' => $price_valid_until,
268 'priceSpecification' => array(
269 'price' => wc_format_decimal( $min_price, wc_get_price_decimals() ),
270 'priceCurrency' => $currency,
271 'valueAddedTaxIncluded' => wc_prices_include_tax() ? 'true' : 'false',
272 ),
273 );
274 } else {
275 if ( $product->is_on_sale() && $product->get_date_on_sale_to() ) {
276 $price_valid_until = gmdate( 'Y-m-d', $product->get_date_on_sale_to()->getTimestamp() );
277 }
278 $markup_offer = array(
279 '@type' => 'Offer',
280 'price' => wc_format_decimal( $product->get_price(), wc_get_price_decimals() ),
281 'priceValidUntil' => $price_valid_until,
282 'priceSpecification' => array(
283 'price' => wc_format_decimal( $product->get_price(), wc_get_price_decimals() ),
284 'priceCurrency' => $currency,
285 'valueAddedTaxIncluded' => wc_prices_include_tax() ? 'true' : 'false',
286 ),
287 );
288 }
289
290 if ( $product->is_in_stock() ) {
291 $stock_status_schema = ( 'onbackorder' === $product->get_stock_status() ) ? 'BackOrder' : 'InStock';
292 } else {
293 $stock_status_schema = 'OutOfStock';
294 }
295
296 $markup_offer += array(
297 'priceCurrency' => $currency,
298 'availability' => 'http://schema.org/' . $stock_status_schema,
299 'url' => $permalink,
300 'seller' => array(
301 '@type' => 'Organization',
302 'name' => $shop_name,
303 'url' => $shop_url,
304 ),
305 );
306
307 $markup['offers'] = array( apply_filters( 'woocommerce_structured_data_product_offer', $markup_offer, $product ) );
308 }
309
310 if ( $product->get_rating_count() && wc_review_ratings_enabled() ) {
311 $markup['aggregateRating'] = array(
312 '@type' => 'AggregateRating',
313 'ratingValue' => $product->get_average_rating(),
314 'reviewCount' => $product->get_review_count(),
315 );
316
317 // Markup 5 most recent rating/review.
318 $comments = get_comments(
319 array(
320 'number' => 5,
321 'post_id' => $product->get_id(),
322 'status' => 'approve',
323 'post_status' => 'publish',
324 'post_type' => 'product',
325 'parent' => 0,
326 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
327 array(
328 'key' => 'rating',
329 'type' => 'NUMERIC',
330 'compare' => '>',
331 'value' => 0,
332 ),
333 ),
334 )
335 );
336
337 if ( $comments ) {
338 $markup['review'] = array();
339 foreach ( $comments as $comment ) {
340 $markup['review'][] = array(
341 '@type' => 'Review',
342 'reviewRating' => array(
343 '@type' => 'Rating',
344 'bestRating' => '5',
345 'ratingValue' => get_comment_meta( $comment->comment_ID, 'rating', true ),
346 'worstRating' => '1',
347 ),
348 'author' => array(
349 '@type' => 'Person',
350 'name' => get_comment_author( $comment ),
351 ),
352 'reviewBody' => get_comment_text( $comment ),
353 'datePublished' => get_comment_date( 'c', $comment ),
354 );
355 }
356 }
357 }
358
359 // Check we have required data.
360 if ( empty( $markup['aggregateRating'] ) && empty( $markup['offers'] ) && empty( $markup['review'] ) ) {
361 return;
362 }
363
364 $this->set_data( apply_filters( 'woocommerce_structured_data_product', $markup, $product ) );
365 }
366
367 /**
368 * Generates Review structured data.
369 *
370 * Hooked into `woocommerce_review_meta` action hook.
371 *
372 * @param WP_Comment $comment Comment data.
373 */
374 public function generate_review_data( $comment ) {
375 $markup = array();
376 $markup['@type'] = 'Review';
377 $markup['@id'] = get_comment_link( $comment->comment_ID );
378 $markup['datePublished'] = get_comment_date( 'c', $comment->comment_ID );
379 $markup['description'] = get_comment_text( $comment->comment_ID );
380 $markup['itemReviewed'] = array(
381 '@type' => 'Product',
382 'name' => get_the_title( $comment->comment_post_ID ),
383 );
384
385 // Skip replies unless they have a rating.
386 $rating = get_comment_meta( $comment->comment_ID, 'rating', true );
387
388 if ( $rating ) {
389 $markup['reviewRating'] = array(
390 '@type' => 'Rating',
391 'bestRating' => '5',
392 'ratingValue' => $rating,
393 'worstRating' => '1',
394 );
395 } elseif ( $comment->comment_parent ) {
396 return;
397 }
398
399 $markup['author'] = array(
400 '@type' => 'Person',
401 'name' => get_comment_author( $comment->comment_ID ),
402 );
403
404 $this->set_data( apply_filters( 'woocommerce_structured_data_review', $markup, $comment ) );
405 }
406
407 /**
408 * Generates BreadcrumbList structured data.
409 *
410 * Hooked into `woocommerce_breadcrumb` action hook.
411 *
412 * @param WC_Breadcrumb $breadcrumbs Breadcrumb data.
413 */
414 public function generate_breadcrumblist_data( $breadcrumbs ) {
415 $crumbs = $breadcrumbs->get_breadcrumb();
416
417 if ( empty( $crumbs ) || ! is_array( $crumbs ) ) {
418 return;
419 }
420
421 $markup = array();
422 $markup['@type'] = 'BreadcrumbList';
423 $markup['itemListElement'] = array();
424
425 foreach ( $crumbs as $key => $crumb ) {
426 $markup['itemListElement'][ $key ] = array(
427 '@type' => 'ListItem',
428 'position' => $key + 1,
429 'item' => array(
430 'name' => $crumb[0],
431 ),
432 );
433
434 if ( ! empty( $crumb[1] ) ) {
435 $markup['itemListElement'][ $key ]['item'] += array( '@id' => $crumb[1] );
436 } elseif ( isset( $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'] ) ) {
437 $current_url = set_url_scheme( 'http://' . wp_unslash( $_SERVER['HTTP_HOST'] ) . wp_unslash( $_SERVER['REQUEST_URI'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
438
439 $markup['itemListElement'][ $key ]['item'] += array( '@id' => $current_url );
440 }
441 }
442
443 $this->set_data( apply_filters( 'woocommerce_structured_data_breadcrumblist', $markup, $breadcrumbs ) );
444 }
445
446 /**
447 * Generates WebSite structured data.
448 *
449 * Hooked into `woocommerce_before_main_content` action hook.
450 */
451 public function generate_website_data() {
452 $markup = array();
453 $markup['@type'] = 'WebSite';
454 $markup['name'] = get_bloginfo( 'name' );
455 $markup['url'] = home_url();
456 $markup['potentialAction'] = array(
457 '@type' => 'SearchAction',
458 'target' => home_url( '?s={search_term_string}&post_type=product' ),
459 'query-input' => 'required name=search_term_string',
460 );
461
462 $this->set_data( apply_filters( 'woocommerce_structured_data_website', $markup ) );
463 }
464
465 /**
466 * Generates Order structured data.
467 *
468 * Hooked into `woocommerce_email_order_details` action hook.
469 *
470 * @param WP_Order $order Order data.
471 * @param bool $sent_to_admin Send to admin (default: false).
472 * @param bool $plain_text Plain text email (default: false).
473 */
474 public function generate_order_data( $order, $sent_to_admin = false, $plain_text = false ) {
475 if ( $plain_text || ! is_a( $order, 'WC_Order' ) ) {
476 return;
477 }
478
479 $shop_name = get_bloginfo( 'name' );
480 $shop_url = home_url();
481 $order_url = $sent_to_admin ? $order->get_edit_order_url() : $order->get_view_order_url();
482 $order_statuses = array(
483 'pending' => 'https://schema.org/OrderPaymentDue',
484 'processing' => 'https://schema.org/OrderProcessing',
485 'on-hold' => 'https://schema.org/OrderProblem',
486 'completed' => 'https://schema.org/OrderDelivered',
487 'cancelled' => 'https://schema.org/OrderCancelled',
488 'refunded' => 'https://schema.org/OrderReturned',
489 'failed' => 'https://schema.org/OrderProblem',
490 );
491
492 $markup_offers = array();
493 foreach ( $order->get_items() as $item ) {
494 if ( ! apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
495 continue;
496 }
497
498 $product = $item->get_product();
499 $product_exists = is_object( $product );
500 $is_visible = $product_exists && $product->is_visible();
501
502 $markup_offers[] = array(
503 '@type' => 'Offer',
504 'price' => $order->get_line_subtotal( $item ),
505 'priceCurrency' => $order->get_currency(),
506 'priceSpecification' => array(
507 'price' => $order->get_line_subtotal( $item ),
508 'priceCurrency' => $order->get_currency(),
509 'eligibleQuantity' => array(
510 '@type' => 'QuantitativeValue',
511 'value' => apply_filters( 'woocommerce_email_order_item_quantity', $item->get_quantity(), $item ),
512 ),
513 ),
514 'itemOffered' => array(
515 '@type' => 'Product',
516 'name' => wp_kses_post( apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, $is_visible ) ),
517 'sku' => $product_exists ? $product->get_sku() : '',
518 'image' => $product_exists ? wp_get_attachment_image_url( $product->get_image_id() ) : '',
519 'url' => $is_visible ? get_permalink( $product->get_id() ) : get_home_url(),
520 ),
521 'seller' => array(
522 '@type' => 'Organization',
523 'name' => $shop_name,
524 'url' => $shop_url,
525 ),
526 );
527 }
528
529 $markup = array();
530 $markup['@type'] = 'Order';
531 $markup['url'] = $order_url;
532 $markup['orderStatus'] = isset( $order_statuses[ $order->get_status() ] ) ? $order_statuses[ $order->get_status() ] : '';
533 $markup['orderNumber'] = $order->get_order_number();
534 $markup['orderDate'] = $order->get_date_created()->format( 'c' );
535 $markup['acceptedOffer'] = $markup_offers;
536 $markup['discount'] = $order->get_total_discount();
537 $markup['discountCurrency'] = $order->get_currency();
538 $markup['price'] = $order->get_total();
539 $markup['priceCurrency'] = $order->get_currency();
540 $markup['priceSpecification'] = array(
541 'price' => $order->get_total(),
542 'priceCurrency' => $order->get_currency(),
543 'valueAddedTaxIncluded' => 'true',
544 );
545 $markup['billingAddress'] = array(
546 '@type' => 'PostalAddress',
547 'name' => $order->get_formatted_billing_full_name(),
548 'streetAddress' => $order->get_billing_address_1(),
549 'postalCode' => $order->get_billing_postcode(),
550 'addressLocality' => $order->get_billing_city(),
551 'addressRegion' => $order->get_billing_state(),
552 'addressCountry' => $order->get_billing_country(),
553 'email' => $order->get_billing_email(),
554 'telephone' => $order->get_billing_phone(),
555 );
556 $markup['customer'] = array(
557 '@type' => 'Person',
558 'name' => $order->get_formatted_billing_full_name(),
559 );
560 $markup['merchant'] = array(
561 '@type' => 'Organization',
562 'name' => $shop_name,
563 'url' => $shop_url,
564 );
565 $markup['potentialAction'] = array(
566 '@type' => 'ViewAction',
567 'name' => 'View Order',
568 'url' => $order_url,
569 'target' => $order_url,
570 );
571
572 $this->set_data( apply_filters( 'woocommerce_structured_data_order', $markup, $sent_to_admin, $order ), true );
573 }
574 }
575