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