I've managed to get this together and now I'm trying to make the number that is being entered into the custom field (saved to the order) to be linked in the order email.
Here's the code:
// create the metabox
add_action( 'add_meta_boxes', 'bdev_add_postnord_meta_box' );
if ( ! function_exists( 'bdev_add_postnord_meta_box' ) )
{
function bdev_add_postnord_meta_box()
{
add_meta_box( 'postnord_field', __('PostNord Parcel ID','woocommerce'), 'bdev_add_postnord_for_tracking', 'shop_order', 'side', 'core' );
}
}
if ( ! function_exists( 'bdev_add_postnord_for_tracking' ) )
{
function bdev_add_postnord_for_tracking()
{
global $post;
$postnord_field_data = get_post_meta( $post->ID, '_postnord_field_data', true ) ? get_post_meta( $post->ID, '_postnord_field_data', true ) : '';
echo '<input type="hidden" name="postnord_meta_field_nonce" value="' . wp_create_nonce() . '">
<p style="border-bottom:solid 1px #eee;padding-bottom:13px;">
<input type="text" style="width:250px;";" name="postnord_data_name" placeholder="' . $postnord_field_data . '" value="' . $postnord_field_data . '"></p>';
}
}
// save input from metabox
add_action( 'save_post', 'bdev_save_postnord_data_to_order', 10, 1 );
if ( ! function_exists( 'bdev_save_postnord_data_to_order' ) )
{
function bdev_save_postnord_data_to_order( $post_id ) {
if ( ! isset( $_POST[ 'postnord_meta_field_nonce' ] ) ) {
return $post_id;
}
$nonce = $_REQUEST[ 'postnord_meta_field_nonce' ];
if ( ! wp_verify_nonce( $nonce ) ) {
return $post_id;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if ( 'page' == $_POST[ 'post_type' ] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
update_post_meta( $post_id, '_postnord_field_data', $_POST[ 'postnord_data_name' ] );
}
}
// print tracking info under shipping address
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'postnord_custom_field_display_admin_order_meta', 10, 1 );
function postnord_custom_field_display_admin_order_meta($order){
$postnord_id_field = get_post_meta( $order->id, '_postnord_field_data', true );
if ( ! empty( $postnord_id_field ) ) {
echo '<p><strong>'. __("PostNord Tracking ID", "woocommerce").':</strong> ' . get_post_meta( $order->id, '_postnord_field_data', true ) . '</p>';
}
}
// add information to order email
add_action( 'woocommerce_email_after_order_table', 'add_postnord_tracking_to_customer_complete_order_email', 20, 2 );
function add_postnord_tracking_to_customer_complete_order_email( $order, $sent_to_admin ) {
if ( ! $sent_to_admin ) {
echo '<h2>Track Your Order</h2>';
echo '<p><strong>'. __("PostNord Tracking ID", "woocommerce").':</strong> ' . get_post_meta( $order->id, '_postnord_field_data', true ) . '</p>';
echo '<p>You can track your parcel on the <a href="https://www.postnord.se/en/online-tools/tools/track/track-and-trace" target="_blank" rel="">PostNord website</a> or directly from <a href="#" target="_blank" rel="">our site</a>.<br><br>';
}
}
The number is saved to the order and I would like to link it so that the customer can click it. When clicked, it should open a URL followed by the tracking number, like this: https://example.com/tracking-page/trackingnr
Any help is appreciated.
I have revisited and simplified your code… There is some errors and mistakes like $order->id
that should be $order->get_id()
since Woocommerce 3.
In the last function your direct tracking Urls is now correctly set and you will have to just change https://example.com/
by the correct domain name and path.
The complete code:
// Add a the metabox to Order edit pages
add_action( 'add_meta_boxes', 'add_postnord_meta_box' );
function add_postnord_meta_box(){
add_meta_box( 'postnord_field', __('PostNord Parcel ID','woocommerce'), 'add_postnord_meta_box_content', 'shop_order', 'side', 'core' );
}
// The Metabow content
function add_postnord_meta_box_content(){
global $post;
$value = get_post_meta( $post->ID, '_postnord_field_data', true );
echo '<input type="hidden" name="postnord_meta_field_nonce" value="' . wp_create_nonce() . '">
<p style="border-bottom:solid 1px #eee;padding-bottom:13px;">
<input type="text" style="width:250px;";" name="postnord_data_name" value="'.$value.'"></p>';
}
// Save the field value from metabox content
add_action( 'save_post_shop_order', 'save_postnord_meta_box_field_value', 10, 1 );
function save_postnord_meta_box_field_value( $post_id ) {
if ( ! isset( $_POST[ 'postnord_meta_field_nonce' ] ) ) {
return $post_id;
}
$nonce = $_REQUEST[ 'postnord_meta_field_nonce' ];
if ( ! wp_verify_nonce( $nonce ) ) {
return $post_id;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if ( ! ( current_user_can( 'edit_shop_order', $post_id ) || current_user_can( 'edit_shop_order', $post_id ) ) ) {
return $post_id;
}
update_post_meta( $post_id, '_postnord_field_data', sanitize_text_field( $_POST[ 'postnord_data_name' ] ) );
}
// Display post north tracking info under shipping address
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'postnord_custom_field_display_admin_order_meta', 10, 1 );
function postnord_custom_field_display_admin_order_meta( $order ){
$postnord_value = $order->get_meta( '_postnord_field_data' );
if ( ! empty($postnord_value) ) {
echo '<p><strong>'. __("PostNord Tracking ID", "woocommerce").':</strong> ' . $postnord_value . '</p>';
}
}
// Display post north tracking info and urls on customer email
add_action( 'woocommerce_email_after_order_table', 'add_postnord_tracking_to_customer_complete_order_email', 20, 4 );
function add_postnord_tracking_to_customer_complete_order_email( $order, $sent_to_admin, $plain_text, $email ) {
if ( $sent_to_admin )
return; // Exit
$postnord_value = $order->get_meta( '_postnord_field_data' );
if ( ! empty($postnord_value) ) {
$postnord_url = 'https://www.postnord.se/en/online-tools/tools/track/track-and-trace';
$tracking_url = 'https://example.com/tracking-page/'.$postnord_value;
$title = __("Track Your Order","woocommerce");
$message = '<p><strong>'. __("PostNord Tracking ID", "woocommerce").':</strong> ' . $postnord_value . '</p>
<p>'. sprintf( __("You can track your parcel on the %s or directly from %s", "woocommerce"),
'<a href="'.$postnord_url.'" target="_blank">'.__("PostNord website", "woocommerce").'</a>',
'<a href="'.$tracking_url.'" target="_blank">'.__("our site", "woocommerce").'</a>.</p>');
echo '<style>
.tracking table {width: 100%; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif;
color: #737373; border: 1px solid #e4e4e4; margin-bottom:8px;}
.tracking table td{text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4;
padding: 12px; padding-bottom: 4px;}
</style>
<div class="tracking">
<h2>' . $title . '</h2>
<table cellspacing="0" cellpadding="6">
<tr><td>'.$message.'</td></tr>
</table></div><br>';
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
You will get the following, with the correct link with the tracking number in a clean table:
来源:https://stackoverflow.com/questions/52356373/set-a-tracking-number-link-on-customer-email-notifications-in-woocommerce-3