Last active
November 23, 2022 15:34
-
-
Save damiencarbery/56bb90e6371dc3dab3c94e48e588f9dc to your computer and use it in GitHub Desktop.
Convert WooCommerce order note to custom field - Parse order notes to extract a shipping tracking number, creating a custom field if found.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Convert WooCommerce order note to custom field | |
Plugin URI: https://www.damiencarbery.com/ | |
Description: Parse order notes to extract a shipping tracking number, creating a custom field if found. | |
Author: Damien Carbery | |
Version: 0.2 | |
*/ | |
add_action( 'woocommerce_order_note_added', 'dcwd_parse_order_note', 10, 2 ); | |
function dcwd_parse_order_note( $comment_id, $order ) { | |
$comment = get_comment( $comment_id, ARRAY_A ); | |
$tracking_str = 'Tracking number: '; | |
// If $tracking_str found in the comment then extract the tracking number | |
// and add it to a custom field. | |
if ( false !== strpos( $comment[ 'comment_content' ], $tracking_str ) ) { | |
$tracking_number = substr( $comment[ 'comment_content' ], strlen( $tracking_str ) ); | |
//error_log( 'Tracking found: ' . $tracking_number ); | |
update_post_meta( $order->get_id(), 'tracking_number', $tracking_number ); | |
update_post_meta( $order->get_id(), 'tracking_url', 'https://www.royalmail.com/track-your-item#/' . $tracking_number ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment