繁体   English   中英

在Woocommerce 3中的客户电子邮件通知上设置跟踪编号链接

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

我设法将它们组合在一起,现在我试图将要输入到自定义字段(保存到订单)中的数字链接到订单电子邮件中。

这是代码:

// 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>';
}
}

该编号已保存到订单中,我想将其链接起来,以便客户可以单击它。 单击后,它将打开一个URL,后跟跟踪编号,如下所示: https : //example.com/tracking-page/trackingnr

任何帮助表示赞赏。

我重新审视并简化了您的代码...自Woocommerce 3起,存在一些错误和错误,例如$order->id应该是$order->get_id()

在上一个功能中,您的直接跟踪Urls现在已正确设置,您只需更改https://example.com/的正确域名和路径即可。

完整的代码:

// 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>';
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中。 经过测试和工作。

您将获得以下内容,并且在干净的表中具有正确的链接以及跟踪号:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM