• Resolved aikigino

    (@aikigino)


    Hi,

    is possible to insert the tracking number in the mail sent by woocommerce when the order status is changed in “completed”?

    Or where I can find documentation to implement this features via some Action or hook?

    Thanks a lot
    Virginio

Viewing 3 replies - 1 through 3 (of 3 total)
  • That’s a decent question. Four month no answer looks like one should not use that service!?

    Plugin Author Sendcloud

    (@sendcloudbv)

    Hi @aikigino and @separatereality ,

    We currently send the tracking number to the notes field once the label is created.

    Our users have been requesting to change this behavior so that the tracking number is also being sent as a custom meta field.

    The concerned Development Team has received the feature request and we will inform our users once this change is implemented.

    You can follow our releases note here:
    https://releaselog.sendcloud.com/changelog

    Best regards,

    Edson
    Sendcloud

    This code snippet is a combination of actions and filters in WordPress that performs several tasks related to tracking orders in WooCommerce.

    The add_action function is used to add an action hook when the order status changes, as well as when a post is loaded or edited. These hooks will trigger the check_private_order_notes function.

    In the check_private_order_notes function, it first checks if the current screen is a shop_order post type and then retrieves the order ID. It then retrieves private order notes for that order and searches for specific text strings using a regular expression. If a match is found, it extracts the tracking number and tracking URL and updates custom fields for that order with this information.

    The add_filter function is used to add a custom column called “Tracking Number” to the Orders page in the WordPress backend. The add_tracking_number_column function loops through the existing columns and adds the custom column after the “Order Status” column.

    Finally, the manage_shop_order_posts_custom_column action is used to display the tracking number and URL in the custom column. The show_tracking_number_in_column function retrieves the custom fields for each order and creates a link with the tracking number and URL.

    Overall, this code enables store owners to track orders more efficiently by adding a custom column with tracking information and updating custom fields with tracking numbers and URLs.

    // Diese Funktion wird ausgel?st, wenn sich der Bestellstatus ?ndert
    add_action('woocommerce_order_status_changed', 'check_private_order_notes', 10, 3);
    
    // Diese Funktion wird ausgel?st, wenn der Beitrag bearbeitet wird oder neu geladen wird
    add_action('load-post.php', 'check_private_order_notes');
    add_action('load-post-new.php', 'check_private_order_notes');
    add_action('edit_post', 'check_private_order_notes');
    
    function check_private_order_notes() {
        $screen = get_current_screen();
        if ($screen->post_type == 'shop_order' && $screen->base == 'post') {
            $order_id = $_GET['post'];
            $order = wc_get_order($order_id);
    
            // 1. Bestellnotizen abrufen und einer Variablen zuweisen
            $private_order_notes = wc_get_order_notes(['order_id' => $order_id, 'type' => 'internal', // Nur private Notizen abrufen
            ]);
            // 2. Durch die Notizen schlaufen und nach bestimmten Textzeichenfolgen suchen
            foreach ($private_order_notes as $note) {
                // 3. Tracking-Nummer und URL aus der Textzeichenfolge extrahieren
                if (preg_match('/shipment is: (\d+) and can be traced at: (https?:\/\/[^\s]+)/', $note->content, $matches)) {
                    $tracking_number = $matches[1];
                    $tracking_number_url = html_entity_decode($matches[2]);
                    // 4. Benutzerdefinierte Felder aktualisieren
                    update_post_meta($order_id, 'custom_tracking_number', $tracking_number);
                    update_post_meta($order_id, 'custom_tracking_number_url', $tracking_number_url);
                }
            }
        }
    }
    
    // 5. Benutzerdefinierte Spalte zur "Bestellungen" Seite im Backend hinzufügen
    add_filter('manage_edit-shop_order_columns', 'add_tracking_number_column');
    function add_tracking_number_column($columns) {
        $new_columns = [];
        foreach ($columns as $column_name => $column_info) {
            $new_columns[$column_name] = $column_info;
            if ($column_name == 'order_status') {
                $new_columns['custom_tracking_number'] = __('Tracking Number', 'woocommerce');
            }
        }
        return $new_columns;
    }
    // 6. Tracking-Nummer und URL in der benutzerdefinierten Spalte anzeigen
    add_action('manage_shop_order_posts_custom_column', 'show_tracking_number_in_column');
    function show_tracking_number_in_column($column) {
        global $post;
        if ($column == 'custom_tracking_number') {
            $tracking_number = get_post_meta($post->ID, 'custom_tracking_number', true);
            $tracking_number_url = get_post_meta($post->ID, 'custom_tracking_number_url', true);
            echo '<a href="' . $tracking_number_url . '">' . $tracking_number . '</a>';
        }
    }

    Depending on the carrier, you may need to modify the “regEx” used to split the Order note at specific points.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Add the tracking number to the woocomerce email’ is closed to new replies.