这通常是离题的,但我最近碰巧做了一些非常类似的事情,所以我可以分享。我处理这件事的方式是wp_insert_comment
(订单注释作为注释添加),检查注释是否为订单注释,并使用regex确定注释是否为跟踪号。如果是,则使用跟踪编号功能添加:
add_action(
\'wp_insert_comment\',
function( int $comment_id, WP_Comment $comment ) {
/**
* Only order notes will contain shipping information for a product.
*/
if ( \'order_note\' !== $comment->comment_type ) {
return;
}
/**
* Match note contents to UPS tracking number format.
* Regex taken from https://www.thetopsites.net/article/53619924.shtml
*/
preg_match(
\'/\\b(1Z ?[0-9A-Z]{3} ?[0-9A-Z]{3} ?[0-9A-Z]{2} ?[0-9A-Z]{4} ?[0-9A-Z]{3} ?[0-9A-Z]|[\\dT]\\d\\d\\d ?\\d\\d\\d\\d ?\\d\\d\\d)\\b/\',
$comment->comment_content,
$matches
);
/**
* Abort if the note is not a UPS tracking number.
*/
if ( empty( $matches ) ) {
return;
}
/**
* Add tracking details with details number extracted from order note.
*/
if ( function_exists( \'ast_insert_tracking_number\' ) ) {
ast_insert_tracking_number(
$comment->comment_post_ID,
$matches[0],
\'UPS\',
strtotime( $comment->comment_date_gmt ),
1
);
}
},
10,
2
);