Created
September 19, 2023 14:25
-
-
Save brandoncc/d7bf6d5cfc250d5514dd30394b9ca67a to your computer and use it in GitHub Desktop.
Generate a page of tables containing sales totals for all US sales in WooCommerce, grouped by year then state
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 | |
function format_as_currency( $amount ) { | |
return '$' . number_format( $amount, 2 ); | |
} | |
function get_sales_by_year( $orders ) { | |
$sales_by_year = array(); | |
foreach ( $orders as $order_id ) { | |
$order = wc_get_order( $order_id ); | |
$order_year = date( 'Y', strtotime( $order->get_date_paid() ) ); | |
$order_state = $order->get_billing_state(); | |
$order_total = $order->get_total(); | |
$order_taxes = $order->get_total_tax(); | |
$order_total_without_taxes = $order_total - $order_taxes; | |
if ( ! isset( $sales_by_year[ $order_year ] ) ) { | |
$sales_by_year[ $order_year ] = array(); | |
} | |
if ( ! isset( $sales_by_year[ $order_year ][ $order_state ] ) ) { | |
$sales_by_year[ $order_year ][ $order_state ] = array( | |
'total' => 0, | |
'taxes' => 0, | |
'total_without_taxes' => 0 | |
); | |
} | |
$sales_by_year[ $order_year ][ $order_state ]['total'] += $order_total; | |
$sales_by_year[ $order_year ][ $order_state ]['taxes'] += $order_taxes; | |
$sales_by_year[ $order_year ][ $order_state ]['total_without_taxes'] += $order_total_without_taxes; | |
} | |
return $sales_by_year; | |
} | |
function write_sales_by_year_to_html( $sales_by_year, $output_file ) { | |
$current_date = date( 'F j, Y' ); | |
$html = '<h1>Sales by year as of ' . $current_date . '</h1>'; | |
foreach ( $sales_by_year as $year => $states ) { | |
$html .= '<h2>' . $year . '</h2>'; | |
$html .= '<table>'; | |
$html .= '<thead>'; | |
$html .= '<tr>'; | |
$html .= '<th>State</th>'; | |
$html .= '<th>Total</th>'; | |
$html .= '<th>Taxes</th>'; | |
$html .= '<th>Total without taxes</th>'; | |
$html .= '</tr>'; | |
$html .= '</thead>'; | |
$html .= '<tbody>'; | |
foreach ( $states as $state => $sales ) { | |
$html .= '<tr>'; | |
$html .= '<td>' . $state . '</td>'; | |
$html .= '<td>' . format_as_currency($sales['total']) . '</td>'; | |
$html .= '<td>' . format_as_currency($sales['taxes']) . '</td>'; | |
$html .= '<td>' . format_as_currency($sales['total_without_taxes']) . '</td>'; | |
$html .= '</tr>'; | |
} | |
$html .= '</tbody>'; | |
$html .= '</table>'; | |
} | |
file_put_contents( $output_file, $html ); | |
} | |
function sort_sales_by_year( $sales_by_year ) { | |
$sorted_sales_by_year = array(); | |
foreach ( $sales_by_year as $year => $states ) { | |
ksort( $states ); | |
$sorted_sales_by_year[ $year ] = $states; | |
} | |
// sort years in reverse so the most recent year is first | |
krsort( $sorted_sales_by_year ); | |
return $sorted_sales_by_year; | |
} | |
$args = array( | |
'billing_country' => 'US', | |
'status' => array( 'wc-completed' ), | |
'type' => 'shop_order', | |
'limit' => -1, | |
'return' => 'ids' | |
); | |
$orders = wc_get_orders( $args ); | |
$sales_by_year = get_sales_by_year( $orders ); | |
$sales_by_year = sort_sales_by_year( $sales_by_year ); | |
write_sales_by_year_to_html( $sales_by_year, 'sales_by_year.html' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment