Set a Tracking Number link on customer email notifications in Woocommerce 3

折月煮酒 提交于 2019-12-06 16:13:58

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:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!