Skip to content
This repository was archived by the owner on Oct 4, 2019. It is now read-only.

Commit de465e7

Browse files
author
Justin Shreve
authored
Overwrite order URL for admins in order details emails (#34)
* Overwrites the order details template, to update the admin view order link to point to WordPress.com. * Properly handle URLs with paths in them
1 parent c35fe79 commit de465e7

File tree

5 files changed

+142
-3
lines changed

5 files changed

+142
-3
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Order details table shown in emails.
4+
*
5+
* This matches version 3.0.0 of the email-order-details template.
6+
*/
7+
8+
if ( ! defined( 'ABSPATH' ) ) {
9+
exit;
10+
}
11+
12+
$text_align = is_rtl() ? 'right' : 'left';
13+
14+
do_action( 'woocommerce_email_before_order_table', $order, $sent_to_admin, $plain_text, $email ); ?>
15+
16+
<?php if ( ! $sent_to_admin ) : ?>
17+
<h2><?php printf( __( 'Order #%s', 'wc-api-dev' ), $order->get_order_number() ); ?></h2>
18+
<?php else : ?>
19+
<h2><a class="link" href="<?php echo esc_url( wc_api_dev_email_get_wpcom_order_link( $order->get_id() ) ); ?>"><?php printf( __( 'Order #%s', 'wc-api-dev' ), $order->get_order_number() ); ?></a> (<?php printf( '<time datetime="%s">%s</time>', $order->get_date_created()->format( 'c' ), wc_format_datetime( $order->get_date_created() ) ); ?>)</h2>
20+
<?php endif; ?>
21+
22+
<table class="td" cellspacing="0" cellpadding="6" style="width: 100%; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;" border="1">
23+
<thead>
24+
<tr>
25+
<th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php _e( 'Product', 'wc-api-dev' ); ?></th>
26+
<th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php _e( 'Quantity', 'wc-api-dev' ); ?></th>
27+
<th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php _e( 'Price', 'wc-api-dev' ); ?></th>
28+
</tr>
29+
</thead>
30+
<tbody>
31+
<?php echo wc_get_email_order_items( $order, array(
32+
'show_sku' => $sent_to_admin,
33+
'show_image' => false,
34+
'image_size' => array( 32, 32 ),
35+
'plain_text' => $plain_text,
36+
'sent_to_admin' => $sent_to_admin,
37+
) ); ?>
38+
</tbody>
39+
<tfoot>
40+
<?php
41+
if ( $totals = $order->get_order_item_totals() ) {
42+
$i = 0;
43+
foreach ( $totals as $total ) {
44+
$i++;
45+
?><tr>
46+
<th class="td" scope="row" colspan="2" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo $total['label']; ?></th>
47+
<td class="td" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo $total['value']; ?></td>
48+
</tr><?php
49+
}
50+
}
51+
?>
52+
</tfoot>
53+
</table>
54+
55+
<?php do_action( 'woocommerce_email_after_order_table', $order, $sent_to_admin, $plain_text, $email ); ?>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Order details table shown in emails.
4+
*
5+
* This matches version 2.5.0 of the email-order-details plain template.
6+
*/
7+
8+
if ( ! defined( 'ABSPATH' ) ) {
9+
exit;
10+
}
11+
12+
do_action( 'woocommerce_email_before_order_table', $order, $sent_to_admin, $plain_text, $email );
13+
14+
echo strtoupper( sprintf( __( 'Order number: %s', 'wc-api-dev' ), $order->get_order_number() ) ) . "\n";
15+
echo wc_format_datetime( $order->get_date_created() ) . "\n";
16+
echo "\n" . wc_get_email_order_items( $order, array(
17+
'show_sku' => $sent_to_admin,
18+
'show_image' => false,
19+
'image_size' => array( 32, 32 ),
20+
'plain_text' => true,
21+
'sent_to_admin' => $sent_to_admin,
22+
) );
23+
24+
echo "==========\n\n";
25+
26+
if ( $totals = $order->get_order_item_totals() ) {
27+
foreach ( $totals as $total ) {
28+
echo $total['label'] . "\t " . $total['value'] . "\n";
29+
}
30+
}
31+
32+
if ( $sent_to_admin ) {
33+
echo "\n" . sprintf( __( 'View order: %s', 'wc-api-dev' ), wc_api_dev_email_get_wpcom_order_link( $order->get_id() ) ) . "\n";
34+
}
35+
36+
do_action( 'woocommerce_email_after_order_table', $order, $sent_to_admin, $plain_text, $email );
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Provides a hotfix that points the admin order URL to WordPress.com instead of wp-admin.
4+
*
5+
* @since 0.8.3
6+
*/
7+
8+
if ( ! defined( 'ABSPATH' ) ) {
9+
exit;
10+
}
11+
12+
function wc_api_dev_email_get_wpcom_order_link( $order_id ) {
13+
$strip_http = '/.*?:\/\//i';
14+
$site_slug = preg_replace( $strip_http, '', get_home_url() );
15+
$site_slug = str_replace( '/', '::', $site_slug );
16+
17+
return 'https://wordpress.com/store/order/' . $site_slug . '/' . absint( $order_id );
18+
}
19+
20+
function wc_api_dev_email_order_url( $markup, $sent_to_admin, $order ) {
21+
$order_url = $sent_to_admin ? wc_api_dev_email_get_wpcom_order_link( $order->get_id() ) : $order->get_view_order_url();
22+
23+
$markup['url'] = $order_url;
24+
$markup['potentialAction'] = array(
25+
'@type' => 'ViewAction',
26+
'name' => 'View Order',
27+
'url' => $order_url,
28+
'target' => $order_url,
29+
);
30+
31+
return $markup;
32+
}
33+
add_filter( 'woocommerce_structured_data_order', 'wc_api_dev_email_order_url', 10, 3 );
34+
35+
function wc_api_dev_email_order_details_get_template( $located, $template_name, $args, $template_path, $default_path ) {
36+
if ( 'emails/email-order-details.php' === $template_name ) {
37+
return dirname( __FILE__ ) . '/email-templates/email-order-details.php';
38+
}
39+
if ( 'emails/plain/email-order-details.php' === $template_name ) {
40+
return dirname( __FILE__ ) . '/email-templates/plain/email-order-details.php';
41+
}
42+
return $located;
43+
}
44+
add_filter( 'wc_get_template', 'wc_api_dev_email_order_details_get_template', 10, 5 );

readme.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Contributors: automattic, woothemes
33
Tags: woocommerce, rest-api, api
44
Requires at least: 4.6
55
Tested up to: 4.8
6-
Stable tag: 0.8.2
6+
Stable tag: 0.8.3
77
License: GPLv2 or later
88
License URI: http://www.gnu.org/licenses/gpl-2.0.html
99

@@ -25,6 +25,9 @@ This section describes how to install the plugin and get it working.
2525

2626
== Changelog ==
2727

28+
= 0.8.3 =
29+
* Fix - Update order link in order details emails.
30+
2831
= 0.8.2 =
2932
* Fix - Don't auto enable the PayPal payment method.
3033

wc-api-dev.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: WooCommerce API Dev
44
* Plugin URI: https://woocommerce.com/
55
* Description: A feature plugin providing a bleeding edge version of the WooCommerce REST API.
6-
* Version: 0.8.2
6+
* Version: 0.8.3
77
* Author: Automattic
88
* Author URI: https://woocommerce.com
99
* Requires at least: 4.4
@@ -33,7 +33,7 @@ class WC_API_Dev {
3333
/**
3434
* Current version of the API plugin.
3535
*/
36-
const CURRENT_VERSION = '0.8.2';
36+
const CURRENT_VERSION = '0.8.3';
3737

3838
/**
3939
* Minimum version needed to run this version of the API.
@@ -131,6 +131,7 @@ public function includes() {
131131
if ( defined( 'WC_API_DEV_ENABLE_HOTFIXES' ) && true === WC_API_DEV_ENABLE_HOTFIXES ) {
132132
include_once( dirname( __FILE__ ) . '/hotfixes/wc-api-dev-jetpack-hotfixes.php' );
133133
include_once( dirname( __FILE__ ) . '/hotfixes/wc-api-dev-email-site-title.php' );
134+
include_once( dirname( __FILE__ ) . '/hotfixes/wc-api-dev-email-order-url.php' );
134135
include_once( dirname( __FILE__ ) . '/hotfixes/wc-api-dev-cheque-defaults.php' );
135136
include_once( dirname( __FILE__ ) . '/hotfixes/wc-api-dev-paypal-defaults.php' );
136137
}

0 commit comments

Comments
 (0)