Become a Sales Superstar: The Ultimate Guide to Tracking Sales with WordPress (for Shriners and Beyond!)

Nobles, are you ready to take your fundraising to the next level? Want to know exactly who is bringing in the big bucks (and maybe spark a little friendly competition)? This guide isn’t just for circus tickets – it works for anything you sell online with WooCommerce!
Whether it’s fezzes, Shriner merchandise, or even those delicious Vidalia onions, this system will help you track sales and recognize the efforts of your team. Gather ’round, because we’re about to unlock the secrets of tracking your WooCommerce sales like a true WordPress wizard!
The Foundation: Custom Meta Fields and QR Codes
Before we get to the exciting part (the plugin!), let’s lay the groundwork. We’ll be using two key ingredients:
- Custom Meta Fields: These are like secret tags you can attach to each WooCommerce order. We’ll use one to store the name of the person who referred the sale (this could be a Shriner, a volunteer, or anyone!).
- QR Codes: Each person gets a unique QR code that links to your product page with their name attached. For example,
https://yourwebsite.com/product/awesome-fez?referrer=John+Doe
. When someone buys a fez through this link, “John Doe” is automatically saved with their order.
Setting Up the Custom Meta Field
Don’t worry, this is easier than it sounds! Just add this code snippet to your theme’s functions.php
file (or use a code snippets plugin):
function register_referrer_meta_field() {
add_meta_box(
'referrer_meta_box', // Meta box ID
'Referrer', // Meta box title
'referrer_meta_box_callback', // Callback function to display the meta box
'shop_order', // Post type (WooCommerce orders)
'side', // Position of the meta box (side or bottom)
'high' // Priority of the meta box
);
}
add_action( 'add_meta_boxes', 'register_referrer_meta_field' );
function referrer_meta_box_callback( $post ) {
$referrer = get_post_meta( $post->ID, 'referrer', true );
?>
<label for="referrer">Referrer:</label>
<input type="text" name="referrer" id="referrer" value="<?php echo esc_attr($referrer);?>" />
<?php
}
function save_referrer_meta( $post_id ) {
if ( isset( $_POST['referrer'] ) ) {
$referrer = sanitize_text_field( $_POST['referrer'] );
update_post_meta( $post_id, 'referrer', $referrer );
}
}
add_action( 'save_post', 'save_referrer_meta', 10, 2 );
This code creates a new field in your WooCommerce orders where you can manually enter the referrer’s name. But the real magic happens when someone uses a QR code – the referrer’s name is automatically filled in!
Introducing the Sales Tracking Plugin
Now for the star of the show! This plugin (we can call it something like “WooCommerce Sales Tracker”) takes all that referral data and turns it into beautiful reports. Here’s the code (adapted from the previous version):
<?php
/*
Plugin Name: WooCommerce Sales Tracker
Description: Displays a dashboard widget and public page with referrer sales data.
Version: 1.2
Author: Sammy Brence
Author URI: https://yourwebsite.com
*/
// Function to display the sales data (used for both the dashboard widget and the public page)
function sales_tracker_display_data() {
global $wpdb;
// Retrieve the saved date range settings (you can configure these in the WordPress dashboard)
$start_date = get_option('sales_tracker_start_date', '2024-09-11');
$end_date = get_option('sales_tracker_end_date', '2025-05-01');
$results = $wpdb->get_results( $wpdb->prepare( "
SELECT
CASE
WHEN pm.meta_value IS NULL OR pm.meta_value = '' THEN 'Referred from Social Media'
ELSE pm.meta_value
END AS referrer,
SUM(CASE
WHEN im.meta_key = '_line_total' THEN im.meta_value
ELSE 0
END) AS total_purchase_price
FROM {$wpdb->prefix}posts o
LEFT JOIN {$wpdb->prefix}postmeta pm ON o.ID = pm.post_id AND pm.meta_key = 'referrer'
JOIN {$wpdb->prefix}woocommerce_order_items oi ON o.ID = oi.order_id
JOIN {$wpdb->prefix}woocommerce_order_itemmeta im ON oi.order_item_id = im.order_item_id
WHERE o.post_type = 'shop_order'
AND o.post_status IN ('wc-completed', 'wc-processing')
AND DATE(o.post_date) BETWEEN %s AND %s
GROUP BY referrer
", $start_date, $end_date ) );
// Table output
echo '<div class="sales-tracker-table">';
echo '<table style="width:100%; border-collapse: collapse;">';
echo '<thead><tr>';
echo '<th style="text-align: left; border: 1px solid #ddd; padding: 8px;">Referrer</th>';
echo '<th style="text-align: right; border: 1px solid #ddd; padding: 8px;">Total Purchase Price</th>';
echo '</tr></thead>';
echo '<tbody>';
$grand_total = 0;
foreach($results as $row) {
echo '<tr>';
echo '<td style="border: 1px solid #ddd; padding: 8px;">'. esc_html($row->referrer). '</td>';
echo '<td style="text-align: right; border: 1px solid #ddd; padding: 8px;">'. number_format($row->total_purchase_price, 2). '</td>';
echo '</tr>';
$grand_total += $row->total_purchase_price;
}
echo '</tbody></table>';
// Grand Total below the table
echo '<p style="text-align: left; font-weight: bold;">Grand Total: '. number_format($grand_total, 2). '</p>';
echo '</div>';
}
// Function to display the dashboard widget
function sales_tracker_dashboard_widget() {
sales_tracker_display_data(); // Call the function to display the data
}
// Function to set up the dashboard widget
function sales_tracker_dashboard_setup() {
wp_add_dashboard_widget( 'sales_tracker_dashboard', 'Sales Tracker', 'sales_tracker_dashboard_widget' );
}
add_action( 'wp_dashboard_setup', 'sales_tracker_dashboard_setup' );
// Function to add the admin menu
function sales_tracker_admin_menu() {
add_menu_page(
'Sales Tracker',
'Sales Tracking',
'manage_options',
'sales-tracker-dashboard',
'sales_tracker_dashboard_widget',
'dashicons-chart-bar',
4
);
// Add a submenu page for settings
add_submenu_page(
'sales-tracker-dashboard', // Parent slug
'Sales Tracker Settings', // Page title
'Settings', // Menu title
'manage_options', // Capability
'sales-tracker-settings', // Menu slug
'sales_tracker_settings_page' // Function to display the settings page
);
}
add_action( 'admin_menu', 'sales_tracker_admin_menu' );
// Function to display the settings page
function sales_tracker_settings_page() {
?>
<div class="wrap">
<h1>Sales Tracker Settings</h1>
<form method="post" action="options.php">
<?php
settings_fields('sales_tracker_settings_group');
do_settings_sections('sales-tracker-settings');
submit_button();
?>
</form>
</div>
<?php
}
// Function to register and display the settings fields
function sales_tracker_settings_init() {
register_setting('sales_tracker_settings_group', 'sales_tracker_start_date');
register_setting('sales_tracker_settings_group', 'sales_tracker_end_date');
add_settings_section(
'sales_tracker_date_range_section',
'Date Range',
'sales_tracker_date_range_section_callback',
'sales-tracker-settings'
);
add_settings_field(
'sales_tracker_start_date',
'Start Date',
'sales_tracker_start_date_callback',
'sales-tracker-settings',
'sales_tracker_date_range_section'
);
add_settings_field(
'sales_tracker_end_date',
'End Date',
'sales_tracker_end_date_callback',
'sales-tracker-settings',
'sales_tracker_date_range_section'
);
}
add_action('admin_init', 'sales_tracker_settings_init');
// Callback functions for settings sections and fields
function sales_tracker_date_range_section_callback() {
echo '<p>Select the date range for the sales report.</p>';
}
function sales_tracker_start_date_callback() {
$start_date = get_option('sales_tracker_start_date');
echo '<input type="date" name="sales_tracker_start_date" value="'. esc_attr($start_date). '" />';
}
function sales_tracker_end_date_callback() {
$end_date = get_option('sales_tracker_end_date');
echo '<input type="date" name="sales_tracker_end_date" value="'. esc_attr($end_date). '" />';
}
// Create a shortcode to display the data publicly
add_shortcode( 'public_sales_tracker', 'sales_tracker_display_data' ); ?>
Using the Plugin
- Installation: Copy the code above and save it as a
.php
file (e.g.,woocommerce-sales-tracker.php
). Upload this file to your WordPress site’s/wp-content/plugins/
directory. Then, activate the plugin in your WordPress dashboard. - Dashboard Widget: You’ll now see a “Sales Tracker” widget on your dashboard, showing the total sales by each referrer.
- Public Page: To display the sales data on a public page, simply use the shortcode
[public_sales_tracker]
. You can put this on any page or post. - Settings: The plugin also adds a settings page (under “Sales Tracking”) where you can adjust the date range for the reports.
Extra Tips for Sales Success
- Promote those QR codes! Put them on flyers, posters, social media – anywhere potential customers will see them.
- Make it a contest: Who can sell the most? Offer a prize to the top-performing person.
- Celebrate success: Publicly acknowledge those who are bringing in the most sales.
With this setup, you’ll have a powerful system for tracking your WooCommerce sales and recognizing the efforts of your team. Let’s make this your most successful fundraising (or sales) campaign yet!