Created
March 9, 2022 13:35
-
-
Save danielcharrua/7bb660ab282460b131ca3f8768ddc85b to your computer and use it in GitHub Desktop.
Set SendCloud tracking on WooCommerce, my account > orders
This file contains hidden or 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 | |
/** | |
* Use this set of functions in functions.php with WooCommerce and SendCloud | |
* SendCloud plugin adds a note when tracking number is set | |
* | |
* If an order is found with the string 'SendCloud shipment' and has a URL, | |
* This will add the link to the tracking system to an order under my account > orders | |
* | |
*/ | |
// Get order notes | |
function charrua_get_private_order_notes($order_id) | |
{ | |
global $wpdb; | |
$table_perfixed = $wpdb->prefix . 'comments'; | |
$results = $wpdb->get_results(" | |
SELECT * | |
FROM $table_perfixed | |
WHERE `comment_post_ID` = $order_id | |
AND `comment_type` LIKE 'order_note' | |
"); | |
foreach ($results as $note) { | |
$order_note[] = array( | |
'note_id' => $note->comment_ID, | |
'note_date' => $note->comment_date, | |
'note_author' => $note->comment_author, | |
'note_content' => $note->comment_content, | |
); | |
} | |
return $order_note; | |
} | |
// Compare SendCloud order notes dates | |
function charrua_cmp_by_note_date($a, $b) | |
{ | |
$date_a = new DateTime($a["note_date"]); | |
$date_b = new DateTime($b["note_date"]); | |
return $date_a < $date_b; | |
} | |
// Filter the last note with 'SendCloud' | |
function charrua_filter_last_sendcloud_note($order_notes) | |
{ | |
if (!is_array($order_notes)) { | |
return false; | |
} | |
$sendcloud_notes = array(); | |
foreach ($order_notes as $note) { | |
if (strpos($note['note_content'], 'SendCloud shipment') !== false) { | |
//found | |
array_push($sendcloud_notes, $note); | |
} | |
} | |
if (count($sendcloud_notes) == 0) { | |
// no note found | |
return false; | |
} | |
if (count($sendcloud_notes) == 1) { | |
// one note found | |
return $sendcloud_notes[0]; | |
} | |
if (count($sendcloud_notes) > 1) { | |
// multiple notes found, order them | |
usort($sendcloud_notes, "charrua_cmp_by_note_date"); | |
return $sendcloud_notes[0]; | |
} | |
} | |
// Extract URL from SendCloud note | |
function charrua_extract_tracking_url($note) | |
{ | |
$regex = '/https?\:\/\/[^\" ]+/i'; | |
$string = $note['note_content']; | |
preg_match($regex, $string, $matches); | |
return $matches[0]; | |
} | |
// Add new column under my account > orders | |
function charrua_wc_add_my_account_orders_column($columns) | |
{ | |
$new_columns = array(); | |
foreach ($columns as $key => $name) { | |
$new_columns[$key] = $name; | |
// add sendcloud-tracking after order total column | |
if ('order-total' === $key) { //this is the line! | |
$new_columns['sendcloud-tracking'] = __('Seguimiento', 'woocommerce'); | |
} | |
} | |
return $new_columns; | |
} | |
add_filter('woocommerce_my_account_my_orders_columns', 'charrua_wc_add_my_account_orders_column'); | |
// Add sendcloud-tracking column content | |
function charrua_wc_custom_column_display($order) | |
{ | |
$order_notes = charrua_get_private_order_notes($order->ID); | |
$sendcloud_note = charrua_filter_last_sendcloud_note($order_notes); | |
if ($sendcloud_note) { | |
$tracking_url = charrua_extract_tracking_url($sendcloud_note); | |
echo "<a href='" . $tracking_url . "' target='_blank'><b>Ver seguimiento</b></a>"; | |
} else { | |
echo "--"; | |
} | |
} | |
add_action('woocommerce_my_account_my_orders_column_sendcloud-tracking', 'charrua_wc_custom_column_display'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment