Add order_schema_markup action to output Schema markup for order emails.

This commit is contained in:
Jon Koops 2016-02-26 17:54:37 +01:00
parent 256be9b028
commit bc097553c6
17 changed files with 119 additions and 0 deletions

View File

@ -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 '<script type="application/ld+json">' . wp_json_encode( $markup ) . '</script>';
}
/**
* Add order meta to email templates.
*

View File

@ -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 );

View File

@ -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 );

View File

@ -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 );

View File

@ -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 );

View File

@ -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 );

View File

@ -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 );

View File

@ -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 );

View File

@ -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 );

View File

@ -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 );

View File

@ -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 );

View File

@ -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 );

View File

@ -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 );

View File

@ -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 );

View File

@ -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 );

View File

@ -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 );

View File

@ -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 );