From bc097553c6f90a2241dbeec7438f72168a45c154 Mon Sep 17 00:00:00 2001 From: Jon Koops Date: Fri, 26 Feb 2016 17:54:37 +0100 Subject: [PATCH] Add order_schema_markup action to output Schema markup for order emails. --- includes/class-wc-emails.php | 103 ++++++++++++++++++ templates/emails/admin-cancelled-order.php | 1 + templates/emails/admin-failed-order.php | 1 + templates/emails/admin-new-order.php | 1 + templates/emails/customer-completed-order.php | 1 + templates/emails/customer-invoice.php | 1 + templates/emails/customer-note.php | 1 + .../emails/customer-processing-order.php | 1 + templates/emails/customer-refunded-order.php | 1 + .../emails/plain/admin-cancelled-order.php | 1 + templates/emails/plain/admin-failed-order.php | 1 + templates/emails/plain/admin-new-order.php | 1 + .../emails/plain/customer-completed-order.php | 1 + templates/emails/plain/customer-invoice.php | 1 + templates/emails/plain/customer-note.php | 1 + .../plain/customer-processing-order.php | 1 + .../emails/plain/customer-refunded-order.php | 1 + 17 files changed, 119 insertions(+) diff --git a/includes/class-wc-emails.php b/includes/class-wc-emails.php index 5151d9b1e2a..b6665977fcd 100644 --- a/includes/class-wc-emails.php +++ b/includes/class-wc-emails.php @@ -109,6 +109,7 @@ class WC_Emails { add_action( 'woocommerce_email_header', array( $this, 'email_header' ) ); add_action( 'woocommerce_email_footer', array( $this, 'email_footer' ) ); add_action( 'woocommerce_email_order_details', array( $this, 'order_details' ), 10, 4 ); + add_action( 'woocommerce_email_order_details', array( $this, 'order_schema_markup' ), 20, 4 ); add_action( 'woocommerce_email_order_meta', array( $this, 'order_meta' ), 10, 3 ); add_action( 'woocommerce_email_customer_details', array( $this, 'customer_details' ), 10, 3 ); add_action( 'woocommerce_email_customer_details', array( $this, 'email_addresses' ), 20, 3 ); @@ -268,6 +269,108 @@ class WC_Emails { } } + /** + * Adds Schema.org markup for order in JSON-LD format. + * + * @since 2.6.0 + * @param mixed $order + * @param bool $sent_to_admin (default: false) + * @param bool $plain_text (default: false) + */ + public function order_schema_markup( $order, $sent_to_admin = false, $plain_text = false ) { + if ( $plain_text ) { + return; + } + + $accepted_offers = array(); + + foreach ( $order->get_items() as $item ) { + if ( ! apply_filters( 'woocommerce_order_item_visible', true, $item ) ) { + continue; + } + + $product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item ); + $is_visible = $product && $product->is_visible(); + + $item_offered = array( + '@type' => 'Product', + 'name' => apply_filters( 'woocommerce_order_item_name', $item['name'], $item, $is_visible ) + ); + + if ( $sku = $product->get_sku() ) { + $item_offered['sku'] = $sku; + } + + if ( $is_visible ) { + $item_offered['url'] = get_permalink( $product->get_id() ); + } + + if ( $image_id = $product->get_image_id() ) { + $item_offered['image'] = wp_get_attachment_image_url( $image_id, 'thumbnail' ); + } + + $accepted_offer = array( + '@type' => 'Offer', + 'itemOffered' => $item_offered, + 'price' => $order->get_line_subtotal( $item ), + 'priceCurrency' => $order->get_order_currency(), + 'eligibleQuantity' => array( + '@type' => 'QuantitativeValue', + 'value' => apply_filters( 'woocommerce_email_order_item_quantity', $item['qty'], $item ) + ) + ); + + $accepted_offers[] = $accepted_offer; + } + + $markup = array( + '@context' => 'http://schema.org', + '@type' => 'Order', + 'merchant' => array( + '@type' => 'Organization', + 'name' => get_bloginfo( 'name' ) + ), + 'orderNumber' => strval( $order->get_order_number() ), + 'acceptedOffer' => $accepted_offers, + 'url' => $order->get_view_order_url() + ); + + switch ( $order->get_status() ) { + case 'pending': + $markup['orderStatus'] = 'http://schema.org/OrderPaymentDue'; + break; + case 'processing': + $markup['orderStatus'] = 'http://schema.org/OrderProcessing'; + break; + case 'on-hold': + $markup['orderStatus'] = 'http://schema.org/OrderProblem'; + break; + case 'completed': + $markup['orderStatus'] = 'http://schema.org/OrderDelivered'; + break; + case 'cancelled': + $markup['orderStatus'] = 'http://schema.org/OrderCancelled'; + break; + case 'refunded': + $markup['orderStatus'] = 'http://schema.org/OrderReturned'; + break; + case 'failed': + $markup['orderStatus'] = 'http://schema.org/OrderProblem'; + break; + } + + if ( $sent_to_admin ) { + $markup['potentialAction'] = array( + '@type' => 'ViewAction', + 'target' => admin_url( 'post.php?post=' . $order->id . '&action=edit' ) + ); + } + + $markup = apply_filters( 'woocommerce_email_order_schema_markup', $markup, $sent_to_admin, $order ); + + echo ''; + } + /** * Add order meta to email templates. * diff --git a/templates/emails/admin-cancelled-order.php b/templates/emails/admin-cancelled-order.php index 835ceea18cd..0c638f89dd8 100644 --- a/templates/emails/admin-cancelled-order.php +++ b/templates/emails/admin-cancelled-order.php @@ -31,6 +31,7 @@ /** * @hooked WC_Emails::order_details() Shows the order details table. + * @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @since 2.5.0 */ do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); diff --git a/templates/emails/admin-failed-order.php b/templates/emails/admin-failed-order.php index 56ffaa60caf..98ed3322a38 100644 --- a/templates/emails/admin-failed-order.php +++ b/templates/emails/admin-failed-order.php @@ -31,6 +31,7 @@ /** * @hooked WC_Emails::order_details() Shows the order details table. + * @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @since 2.5.0 */ do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); diff --git a/templates/emails/admin-new-order.php b/templates/emails/admin-new-order.php index d9fb1c23bf0..8633d826351 100644 --- a/templates/emails/admin-new-order.php +++ b/templates/emails/admin-new-order.php @@ -31,6 +31,7 @@ /** * @hooked WC_Emails::order_details() Shows the order details table. + * @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @since 2.5.0 */ do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); diff --git a/templates/emails/customer-completed-order.php b/templates/emails/customer-completed-order.php index 8214bcd16e3..fddb57ee9b6 100644 --- a/templates/emails/customer-completed-order.php +++ b/templates/emails/customer-completed-order.php @@ -31,6 +31,7 @@ do_action( 'woocommerce_email_header', $email_heading, $email ); ?> /** * @hooked WC_Emails::order_details() Shows the order details table. + * @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @since 2.5.0 */ do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); diff --git a/templates/emails/customer-invoice.php b/templates/emails/customer-invoice.php index 828b6cf7065..ba121eeb0c2 100644 --- a/templates/emails/customer-invoice.php +++ b/templates/emails/customer-invoice.php @@ -33,6 +33,7 @@ do_action( 'woocommerce_email_header', $email_heading, $email ); ?> /** * @hooked WC_Emails::order_details() Shows the order details table. + * @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @since 2.5.0 */ do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); diff --git a/templates/emails/customer-note.php b/templates/emails/customer-note.php index 68b3896c094..b77b8671027 100644 --- a/templates/emails/customer-note.php +++ b/templates/emails/customer-note.php @@ -35,6 +35,7 @@ do_action( 'woocommerce_email_header', $email_heading, $email ); ?> /** * @hooked WC_Emails::order_details() Shows the order details table. + * @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @since 2.5.0 */ do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); diff --git a/templates/emails/customer-processing-order.php b/templates/emails/customer-processing-order.php index 59c7d698440..1721daae2a2 100644 --- a/templates/emails/customer-processing-order.php +++ b/templates/emails/customer-processing-order.php @@ -31,6 +31,7 @@ do_action( 'woocommerce_email_header', $email_heading, $email ); ?> /** * @hooked WC_Emails::order_details() Shows the order details table. + * @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @since 2.5.0 */ do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); diff --git a/templates/emails/customer-refunded-order.php b/templates/emails/customer-refunded-order.php index 9c6398da6dc..5e35e686bbd 100644 --- a/templates/emails/customer-refunded-order.php +++ b/templates/emails/customer-refunded-order.php @@ -38,6 +38,7 @@ do_action( 'woocommerce_email_header', $email_heading, $email ); ?> /** * @hooked WC_Emails::order_details() Shows the order details table. + * @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @since 2.5.0 */ do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); diff --git a/templates/emails/plain/admin-cancelled-order.php b/templates/emails/plain/admin-cancelled-order.php index fe890a1e7f4..ad1a9324e34 100644 --- a/templates/emails/plain/admin-cancelled-order.php +++ b/templates/emails/plain/admin-cancelled-order.php @@ -28,6 +28,7 @@ echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n" /** * @hooked WC_Emails::order_details() Shows the order details table. + * @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @since 2.5.0 */ do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); diff --git a/templates/emails/plain/admin-failed-order.php b/templates/emails/plain/admin-failed-order.php index 66d25c450b9..5c119672ae8 100644 --- a/templates/emails/plain/admin-failed-order.php +++ b/templates/emails/plain/admin-failed-order.php @@ -28,6 +28,7 @@ echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n" /** * @hooked WC_Emails::order_details() Shows the order details table. + * @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @since 2.5.0 */ do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); diff --git a/templates/emails/plain/admin-new-order.php b/templates/emails/plain/admin-new-order.php index 18b577a6f79..f191def1174 100644 --- a/templates/emails/plain/admin-new-order.php +++ b/templates/emails/plain/admin-new-order.php @@ -28,6 +28,7 @@ echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n" /** * @hooked WC_Emails::order_details() Shows the order details table. + * @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @since 2.5.0 */ do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); diff --git a/templates/emails/plain/customer-completed-order.php b/templates/emails/plain/customer-completed-order.php index 246ce300410..2c5a22b5136 100644 --- a/templates/emails/plain/customer-completed-order.php +++ b/templates/emails/plain/customer-completed-order.php @@ -28,6 +28,7 @@ echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n" /** * @hooked WC_Emails::order_details() Shows the order details table. + * @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @since 2.5.0 */ do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); diff --git a/templates/emails/plain/customer-invoice.php b/templates/emails/plain/customer-invoice.php index 62e8569bd1e..98266ef9d59 100644 --- a/templates/emails/plain/customer-invoice.php +++ b/templates/emails/plain/customer-invoice.php @@ -30,6 +30,7 @@ echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n" /** * @hooked WC_Emails::order_details() Shows the order details table. + * @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @since 2.5.0 */ do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); diff --git a/templates/emails/plain/customer-note.php b/templates/emails/plain/customer-note.php index 0d4d6ee7cee..de3e7d35cf3 100644 --- a/templates/emails/plain/customer-note.php +++ b/templates/emails/plain/customer-note.php @@ -36,6 +36,7 @@ echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n" /** * @hooked WC_Emails::order_details() Shows the order details table. + * @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @since 2.5.0 */ do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); diff --git a/templates/emails/plain/customer-processing-order.php b/templates/emails/plain/customer-processing-order.php index 68064a877a2..8b0a28568f3 100644 --- a/templates/emails/plain/customer-processing-order.php +++ b/templates/emails/plain/customer-processing-order.php @@ -28,6 +28,7 @@ echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n" /** * @hooked WC_Emails::order_details() Shows the order details table. + * @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @since 2.5.0 */ do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); diff --git a/templates/emails/plain/customer-refunded-order.php b/templates/emails/plain/customer-refunded-order.php index 243cf2f4101..de4a87bdcab 100644 --- a/templates/emails/plain/customer-refunded-order.php +++ b/templates/emails/plain/customer-refunded-order.php @@ -28,6 +28,7 @@ echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n" /** * @hooked WC_Emails::order_details() Shows the order details table. + * @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @since 2.5.0 */ do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );