Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Add Order Origin Filter Field in WooCommerce >Admin Orders Page?

WooCommerce comes with a default column named ‘origin,’ displayed in its WooCommerce > Orders table. However, you need to enable a checkbox to make it appear as one of the screen options on the WooCommerce Orders page and to display it as one of the columns in the WooCommerce Orders table. 

How to Add Order Origin Filter Field in WooCommerce >Admin Orders Page? - Tyche Softwares

As a result, WooCommerce can capture UTM sources effectively when the appropriate links are used during the checkout process.

In this post, we will be adding a dropdown filter to the WooCommerce Orders page to view and filter orders based on the captured UTM sources. This will definitely help you to quickly filter the origin of orders, allowing you to effectively track the performance of your marketing channels, such as LinkedIn, Quora, YouTube, referral links, etc.

The code snippet works when HPOS is enabled in your WooCommerce store. If you want it to work in the WordPress post tables, you might need to change the hooks that are specific to the WordPress posts table.

Solution: Add Order Origin Filter Field in WooCommerce >Admin Orders Page

This solution focuses on adding a custom dropdown filter in the WooCommerce orders page to allow store owners to filter and view orders based on the UTM sources captured by WooCommerce.

// Add a custom dropdown filter to filter orders by UTM Source (HPOS)
function ts_hpos_custom_utm_source_dropdown_filter() {
    // Define the UTM source values you want to filter by. Match these values with your meta data.
    $utm_sources = array(
        'direct' => 'Direct',
        'unknown' => 'Unknown',
        'linkedin' => 'LinkedIn',
        'quora' => 'Quora',
        'referral' => 'Referral'
        // Add more UTM sources here if needed
    );

    echo '<select name="utm_source_filter" class="postform">';
    echo '<option value="">Filter by Source</option>'; // Default "Filter by" option

    $current_filter = isset($_GET['utm_source_filter']) ? sanitize_text_field($_GET['utm_source_filter']) : '';

    foreach ($utm_sources as $utm_key => $utm_label) {
        $selected = $current_filter === $utm_key ? 'selected' : '';
        echo '<option value="' . esc_attr($utm_key) . '" ' . $selected . '>' . esc_html($utm_label) . '</option>';
    }

    echo '</select>';
}
add_action('woocommerce_order_list_table_restrict_manage_orders', 'ts_hpos_custom_utm_source_dropdown_filter');

// Filter orders based on selected UTM Source (HPOS)
function ts_hpos_filter_orders_by_utm_source($query_args) {
    if (isset($_GET['utm_source_filter']) && !empty($_GET['utm_source_filter'])) {
        $selected_utm_source = sanitize_text_field($_GET['utm_source_filter']);
        
        // Initialize the meta query if not set
        $meta_query = isset($query_args['meta_query']) ? $query_args['meta_query'] : array();
        
        if ($selected_utm_source === 'unknown') {
            // Handle the case for 'unknown' UTM source
            $meta_query[] = array(
                'relation' => 'OR',
                array(
                    'key' => '_wc_order_attribution_utm_source',
                    'compare' => 'NOT EXISTS' // No value set for UTM Source (unknown)
                ),
                array(
                    'key' => '_wc_order_attribution_utm_source',
                    'value' => '', // Empty UTM Source value (unknown)
                    'compare' => '='
                )
            );
        } else {
            $meta_query[] = array(
                'key' => '_wc_order_attribution_utm_source',
                'value' => $selected_utm_source,
                'compare' => 'LIKE' // Use 'LIKE' to handle variations
            );
        }

        // Set the modified meta query
        $query_args['meta_query'] = $meta_query;
    }
    
    return $query_args;
}
add_filter('woocommerce_order_query_args', 'ts_hpos_filter_orders_by_utm_source');

Output

Imagine your store is receiving orders from different UTM sources like LinkedIn, Quora, referral links, direct traffic, or organic Google traffic. With the ‘Filter By Source’ dropdown, you can easily select any specific source and view the orders that came from that particular marketing channel or platform.

Add Order Origin Filter Field

If a user wants to filter orders placed specifically from the LinkedIn platform, they can easily filter those orders as shown below.

How to Add Order Origin Filter Field in WooCommerce >Admin Orders Page? - Tyche Softwares

Filters are a great way to sort and manage values in any columns of the Orders table. As shown in this post, you can easily add filters to the default columns provided by WooCommerce. You can also create custom columns and filter those as well. For example, you could add a payment column and set up a filter for payment gateways on the WooCommerce Orders page to make order management easier.



Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x