abstracts
5 years ago
admin
5 years ago
cli
5 years ago
customizer
5 years ago
data-stores
4 years ago
emails
5 years ago
export
5 years ago
gateways
4 years ago
import
5 years ago
integrations
5 years ago
interfaces
5 years ago
legacy
5 years ago
libraries
6 years ago
log-handlers
5 years ago
payment-tokens
5 years ago
queue
5 years ago
rest-api
5 years ago
shipping
5 years ago
shortcodes
5 years ago
theme-support
5 years ago
tracks
5 years ago
traits
5 years ago
walkers
5 years ago
wccom-site
5 years ago
widgets
5 years ago
class-wc-ajax.php
5 years ago
class-wc-api.php
5 years ago
class-wc-auth.php
5 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
5 years ago
class-wc-cart-fees.php
5 years ago
class-wc-cart-session.php
5 years ago
class-wc-cart-totals.php
5 years ago
class-wc-cart.php
5 years ago
class-wc-checkout.php
5 years ago
class-wc-cli.php
8 years ago
class-wc-comments.php
5 years ago
class-wc-countries.php
5 years ago
class-wc-coupon.php
5 years ago
class-wc-customer-download-log.php
5 years ago
class-wc-customer-download.php
5 years ago
class-wc-customer.php
5 years ago
class-wc-data-exception.php
8 years ago
class-wc-data-store.php
6 years ago
class-wc-datetime.php
5 years ago
class-wc-deprecated-action-hooks.php
8 years ago
class-wc-deprecated-filter-hooks.php
7 years ago
class-wc-discounts.php
5 years ago
class-wc-download-handler.php
5 years ago
class-wc-emails.php
5 years ago
class-wc-embed.php
5 years ago
class-wc-form-handler.php
5 years ago
class-wc-frontend-scripts.php
5 years ago
class-wc-geo-ip.php
5 years ago
class-wc-geolite-integration.php
6 years ago
class-wc-geolocation.php
5 years ago
class-wc-https.php
5 years ago
class-wc-install.php
4 years ago
class-wc-integrations.php
5 years ago
class-wc-log-levels.php
5 years ago
class-wc-logger.php
5 years ago
class-wc-meta-data.php
7 years ago
class-wc-order-factory.php
5 years ago
class-wc-order-item-coupon.php
5 years ago
class-wc-order-item-fee.php
5 years ago
class-wc-order-item-meta.php
5 years ago
class-wc-order-item-product.php
5 years ago
class-wc-order-item-shipping.php
5 years ago
class-wc-order-item-tax.php
5 years ago
class-wc-order-item.php
5 years ago
class-wc-order-query.php
5 years ago
class-wc-order-refund.php
5 years ago
class-wc-order.php
5 years ago
class-wc-payment-gateways.php
5 years ago
class-wc-payment-tokens.php
5 years ago
class-wc-post-data.php
5 years ago
class-wc-post-types.php
5 years ago
class-wc-privacy-background-process.php
5 years ago
class-wc-privacy-erasers.php
6 years ago
class-wc-privacy-exporters.php
6 years ago
class-wc-privacy.php
5 years ago
class-wc-product-attribute.php
5 years ago
class-wc-product-download.php
5 years ago
class-wc-product-external.php
5 years ago
class-wc-product-factory.php
5 years ago
class-wc-product-grouped.php
8 years ago
class-wc-product-query.php
5 years ago
class-wc-product-simple.php
5 years ago
class-wc-product-variable.php
5 years ago
class-wc-product-variation.php
5 years ago
class-wc-query.php
5 years ago
class-wc-rate-limiter.php
5 years ago
class-wc-regenerate-images-request.php
5 years ago
class-wc-regenerate-images.php
5 years ago
class-wc-register-wp-admin-settings.php
5 years ago
class-wc-rest-authentication.php
5 years ago
class-wc-rest-exception.php
5 years ago
class-wc-session-handler.php
5 years ago
class-wc-shipping-rate.php
5 years ago
class-wc-shipping-zone.php
5 years ago
class-wc-shipping-zones.php
5 years ago
class-wc-shipping.php
5 years ago
class-wc-shortcodes.php
5 years ago
class-wc-structured-data.php
5 years ago
class-wc-tax.php
5 years ago
class-wc-template-loader.php
5 years ago
class-wc-tracker.php
5 years ago
class-wc-validation.php
5 years ago
class-wc-webhook.php
5 years ago
class-woocommerce.php
4 years ago
wc-account-functions.php
5 years ago
wc-attribute-functions.php
5 years ago
wc-cart-functions.php
5 years ago
wc-conditional-functions.php
5 years ago
wc-core-functions.php
5 years ago
wc-coupon-functions.php
5 years ago
wc-deprecated-functions.php
5 years ago
wc-formatting-functions.php
5 years ago
wc-notice-functions.php
5 years ago
wc-order-functions.php
5 years ago
wc-order-item-functions.php
5 years ago
wc-page-functions.php
5 years ago
wc-product-functions.php
5 years ago
wc-rest-functions.php
5 years ago
wc-stock-functions.php
5 years ago
wc-template-functions.php
5 years ago
wc-template-hooks.php
5 years ago
wc-term-functions.php
5 years ago
wc-update-functions.php
4 years ago
wc-user-functions.php
5 years ago
wc-webhook-functions.php
5 years ago
wc-widget-functions.php
5 years ago
class-wc-structured-data.php
539 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' => $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 | } else { |
| 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 | $markup_offer = array( |
| 249 | '@type' => 'Offer', |
| 250 | 'price' => wc_format_decimal( $product->get_price(), wc_get_price_decimals() ), |
| 251 | 'priceValidUntil' => $price_valid_until, |
| 252 | 'priceSpecification' => array( |
| 253 | 'price' => wc_format_decimal( $product->get_price(), wc_get_price_decimals() ), |
| 254 | 'priceCurrency' => $currency, |
| 255 | 'valueAddedTaxIncluded' => wc_prices_include_tax() ? 'true' : 'false', |
| 256 | ), |
| 257 | ); |
| 258 | } |
| 259 | |
| 260 | $markup_offer += array( |
| 261 | 'priceCurrency' => $currency, |
| 262 | 'availability' => 'http://schema.org/' . ( $product->is_in_stock() ? 'InStock' : 'OutOfStock' ), |
| 263 | 'url' => $permalink, |
| 264 | 'seller' => array( |
| 265 | '@type' => 'Organization', |
| 266 | 'name' => $shop_name, |
| 267 | 'url' => $shop_url, |
| 268 | ), |
| 269 | ); |
| 270 | |
| 271 | $markup['offers'] = array( apply_filters( 'woocommerce_structured_data_product_offer', $markup_offer, $product ) ); |
| 272 | } |
| 273 | |
| 274 | if ( $product->get_rating_count() && wc_review_ratings_enabled() ) { |
| 275 | $markup['aggregateRating'] = array( |
| 276 | '@type' => 'AggregateRating', |
| 277 | 'ratingValue' => $product->get_average_rating(), |
| 278 | 'reviewCount' => $product->get_review_count(), |
| 279 | ); |
| 280 | |
| 281 | // Markup 5 most recent rating/review. |
| 282 | $comments = get_comments( |
| 283 | array( |
| 284 | 'number' => 5, |
| 285 | 'post_id' => $product->get_id(), |
| 286 | 'status' => 'approve', |
| 287 | 'post_status' => 'publish', |
| 288 | 'post_type' => 'product', |
| 289 | 'parent' => 0, |
| 290 | 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 291 | array( |
| 292 | 'key' => 'rating', |
| 293 | 'type' => 'NUMERIC', |
| 294 | 'compare' => '>', |
| 295 | 'value' => 0, |
| 296 | ), |
| 297 | ), |
| 298 | ) |
| 299 | ); |
| 300 | |
| 301 | if ( $comments ) { |
| 302 | $markup['review'] = array(); |
| 303 | foreach ( $comments as $comment ) { |
| 304 | $markup['review'][] = array( |
| 305 | '@type' => 'Review', |
| 306 | 'reviewRating' => array( |
| 307 | '@type' => 'Rating', |
| 308 | 'bestRating' => '5', |
| 309 | 'ratingValue' => get_comment_meta( $comment->comment_ID, 'rating', true ), |
| 310 | 'worstRating' => '1', |
| 311 | ), |
| 312 | 'author' => array( |
| 313 | '@type' => 'Person', |
| 314 | 'name' => get_comment_author( $comment ), |
| 315 | ), |
| 316 | 'reviewBody' => get_comment_text( $comment ), |
| 317 | 'datePublished' => get_comment_date( 'c', $comment ), |
| 318 | ); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | // Check we have required data. |
| 324 | if ( empty( $markup['aggregateRating'] ) && empty( $markup['offers'] ) && empty( $markup['review'] ) ) { |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | $this->set_data( apply_filters( 'woocommerce_structured_data_product', $markup, $product ) ); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Generates Review structured data. |
| 333 | * |
| 334 | * Hooked into `woocommerce_review_meta` action hook. |
| 335 | * |
| 336 | * @param WP_Comment $comment Comment data. |
| 337 | */ |
| 338 | public function generate_review_data( $comment ) { |
| 339 | $markup = array(); |
| 340 | $markup['@type'] = 'Review'; |
| 341 | $markup['@id'] = get_comment_link( $comment->comment_ID ); |
| 342 | $markup['datePublished'] = get_comment_date( 'c', $comment->comment_ID ); |
| 343 | $markup['description'] = get_comment_text( $comment->comment_ID ); |
| 344 | $markup['itemReviewed'] = array( |
| 345 | '@type' => 'Product', |
| 346 | 'name' => get_the_title( $comment->comment_post_ID ), |
| 347 | ); |
| 348 | |
| 349 | // Skip replies unless they have a rating. |
| 350 | $rating = get_comment_meta( $comment->comment_ID, 'rating', true ); |
| 351 | |
| 352 | if ( $rating ) { |
| 353 | $markup['reviewRating'] = array( |
| 354 | '@type' => 'Rating', |
| 355 | 'bestRating' => '5', |
| 356 | 'ratingValue' => $rating, |
| 357 | 'worstRating' => '1', |
| 358 | ); |
| 359 | } elseif ( $comment->comment_parent ) { |
| 360 | return; |
| 361 | } |
| 362 | |
| 363 | $markup['author'] = array( |
| 364 | '@type' => 'Person', |
| 365 | 'name' => get_comment_author( $comment->comment_ID ), |
| 366 | ); |
| 367 | |
| 368 | $this->set_data( apply_filters( 'woocommerce_structured_data_review', $markup, $comment ) ); |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Generates BreadcrumbList structured data. |
| 373 | * |
| 374 | * Hooked into `woocommerce_breadcrumb` action hook. |
| 375 | * |
| 376 | * @param WC_Breadcrumb $breadcrumbs Breadcrumb data. |
| 377 | */ |
| 378 | public function generate_breadcrumblist_data( $breadcrumbs ) { |
| 379 | $crumbs = $breadcrumbs->get_breadcrumb(); |
| 380 | |
| 381 | if ( empty( $crumbs ) || ! is_array( $crumbs ) ) { |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | $markup = array(); |
| 386 | $markup['@type'] = 'BreadcrumbList'; |
| 387 | $markup['itemListElement'] = array(); |
| 388 | |
| 389 | foreach ( $crumbs as $key => $crumb ) { |
| 390 | $markup['itemListElement'][ $key ] = array( |
| 391 | '@type' => 'ListItem', |
| 392 | 'position' => $key + 1, |
| 393 | 'item' => array( |
| 394 | 'name' => $crumb[0], |
| 395 | ), |
| 396 | ); |
| 397 | |
| 398 | if ( ! empty( $crumb[1] ) ) { |
| 399 | $markup['itemListElement'][ $key ]['item'] += array( '@id' => $crumb[1] ); |
| 400 | } elseif ( isset( $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'] ) ) { |
| 401 | $current_url = set_url_scheme( 'http://' . wp_unslash( $_SERVER['HTTP_HOST'] ) . wp_unslash( $_SERVER['REQUEST_URI'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 402 | |
| 403 | $markup['itemListElement'][ $key ]['item'] += array( '@id' => $current_url ); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | $this->set_data( apply_filters( 'woocommerce_structured_data_breadcrumblist', $markup, $breadcrumbs ) ); |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Generates WebSite structured data. |
| 412 | * |
| 413 | * Hooked into `woocommerce_before_main_content` action hook. |
| 414 | */ |
| 415 | public function generate_website_data() { |
| 416 | $markup = array(); |
| 417 | $markup['@type'] = 'WebSite'; |
| 418 | $markup['name'] = get_bloginfo( 'name' ); |
| 419 | $markup['url'] = home_url(); |
| 420 | $markup['potentialAction'] = array( |
| 421 | '@type' => 'SearchAction', |
| 422 | 'target' => home_url( '?s={search_term_string}&post_type=product' ), |
| 423 | 'query-input' => 'required name=search_term_string', |
| 424 | ); |
| 425 | |
| 426 | $this->set_data( apply_filters( 'woocommerce_structured_data_website', $markup ) ); |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Generates Order structured data. |
| 431 | * |
| 432 | * Hooked into `woocommerce_email_order_details` action hook. |
| 433 | * |
| 434 | * @param WP_Order $order Order data. |
| 435 | * @param bool $sent_to_admin Send to admin (default: false). |
| 436 | * @param bool $plain_text Plain text email (default: false). |
| 437 | */ |
| 438 | public function generate_order_data( $order, $sent_to_admin = false, $plain_text = false ) { |
| 439 | if ( $plain_text || ! is_a( $order, 'WC_Order' ) ) { |
| 440 | return; |
| 441 | } |
| 442 | |
| 443 | $shop_name = get_bloginfo( 'name' ); |
| 444 | $shop_url = home_url(); |
| 445 | $order_url = $sent_to_admin ? $order->get_edit_order_url() : $order->get_view_order_url(); |
| 446 | $order_statuses = array( |
| 447 | 'pending' => 'https://schema.org/OrderPaymentDue', |
| 448 | 'processing' => 'https://schema.org/OrderProcessing', |
| 449 | 'on-hold' => 'https://schema.org/OrderProblem', |
| 450 | 'completed' => 'https://schema.org/OrderDelivered', |
| 451 | 'cancelled' => 'https://schema.org/OrderCancelled', |
| 452 | 'refunded' => 'https://schema.org/OrderReturned', |
| 453 | 'failed' => 'https://schema.org/OrderProblem', |
| 454 | ); |
| 455 | |
| 456 | $markup_offers = array(); |
| 457 | foreach ( $order->get_items() as $item ) { |
| 458 | if ( ! apply_filters( 'woocommerce_order_item_visible', true, $item ) ) { |
| 459 | continue; |
| 460 | } |
| 461 | |
| 462 | $product = $item->get_product(); |
| 463 | $product_exists = is_object( $product ); |
| 464 | $is_visible = $product_exists && $product->is_visible(); |
| 465 | |
| 466 | $markup_offers[] = array( |
| 467 | '@type' => 'Offer', |
| 468 | 'price' => $order->get_line_subtotal( $item ), |
| 469 | 'priceCurrency' => $order->get_currency(), |
| 470 | 'priceSpecification' => array( |
| 471 | 'price' => $order->get_line_subtotal( $item ), |
| 472 | 'priceCurrency' => $order->get_currency(), |
| 473 | 'eligibleQuantity' => array( |
| 474 | '@type' => 'QuantitativeValue', |
| 475 | 'value' => apply_filters( 'woocommerce_email_order_item_quantity', $item->get_quantity(), $item ), |
| 476 | ), |
| 477 | ), |
| 478 | 'itemOffered' => array( |
| 479 | '@type' => 'Product', |
| 480 | 'name' => apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, $is_visible ), |
| 481 | 'sku' => $product_exists ? $product->get_sku() : '', |
| 482 | 'image' => $product_exists ? wp_get_attachment_image_url( $product->get_image_id() ) : '', |
| 483 | 'url' => $is_visible ? get_permalink( $product->get_id() ) : get_home_url(), |
| 484 | ), |
| 485 | 'seller' => array( |
| 486 | '@type' => 'Organization', |
| 487 | 'name' => $shop_name, |
| 488 | 'url' => $shop_url, |
| 489 | ), |
| 490 | ); |
| 491 | } |
| 492 | |
| 493 | $markup = array(); |
| 494 | $markup['@type'] = 'Order'; |
| 495 | $markup['url'] = $order_url; |
| 496 | $markup['orderStatus'] = isset( $order_statuses[ $order->get_status() ] ) ? $order_statuses[ $order->get_status() ] : ''; |
| 497 | $markup['orderNumber'] = $order->get_order_number(); |
| 498 | $markup['orderDate'] = $order->get_date_created()->format( 'c' ); |
| 499 | $markup['acceptedOffer'] = $markup_offers; |
| 500 | $markup['discount'] = $order->get_total_discount(); |
| 501 | $markup['discountCurrency'] = $order->get_currency(); |
| 502 | $markup['price'] = $order->get_total(); |
| 503 | $markup['priceCurrency'] = $order->get_currency(); |
| 504 | $markup['priceSpecification'] = array( |
| 505 | 'price' => $order->get_total(), |
| 506 | 'priceCurrency' => $order->get_currency(), |
| 507 | 'valueAddedTaxIncluded' => 'true', |
| 508 | ); |
| 509 | $markup['billingAddress'] = array( |
| 510 | '@type' => 'PostalAddress', |
| 511 | 'name' => $order->get_formatted_billing_full_name(), |
| 512 | 'streetAddress' => $order->get_billing_address_1(), |
| 513 | 'postalCode' => $order->get_billing_postcode(), |
| 514 | 'addressLocality' => $order->get_billing_city(), |
| 515 | 'addressRegion' => $order->get_billing_state(), |
| 516 | 'addressCountry' => $order->get_billing_country(), |
| 517 | 'email' => $order->get_billing_email(), |
| 518 | 'telephone' => $order->get_billing_phone(), |
| 519 | ); |
| 520 | $markup['customer'] = array( |
| 521 | '@type' => 'Person', |
| 522 | 'name' => $order->get_formatted_billing_full_name(), |
| 523 | ); |
| 524 | $markup['merchant'] = array( |
| 525 | '@type' => 'Organization', |
| 526 | 'name' => $shop_name, |
| 527 | 'url' => $shop_url, |
| 528 | ); |
| 529 | $markup['potentialAction'] = array( |
| 530 | '@type' => 'ViewAction', |
| 531 | 'name' => 'View Order', |
| 532 | 'url' => $order_url, |
| 533 | 'target' => $order_url, |
| 534 | ); |
| 535 | |
| 536 | $this->set_data( apply_filters( 'woocommerce_structured_data_order', $markup, $sent_to_admin, $order ), true ); |
| 537 | } |
| 538 | } |
| 539 |