Home » WooCommerce: Display Sales to Admin @ FrontEnd

WooCommerce: Display Sales to Admin @ FrontEnd

by Tutor Aspire

Hello WooCommerce Customizers!

Today is episode #18 of my “18 snippets in 18 days” challenge… yes, I’ve made it! In the last 18 days I’ve added a good amount of WooCommerce code tutorials and I hope you appreciate the effort!

If you haven’t shared on social media or left a comment yet, it is time to spread the word and share the WooLove right now 🙂

Today’s goal is quite atypical, but I love that. A client needed to see on the frontend, almost in real time, who bought which product and their names. The sell Yoga classes via WooCommerce and simply needed to make sure who’s on what class!

WooCommerce: Show Sales to the Admin on the FrontEnd
WooCommerce: Show Sales to the Admin on the FrontEnd

WooCommerce 3.0+ Snippet: Display Sales to Admin @ FrontEnd Loop

/**
 * @snippet       Display Sales to Admin @ FrontEnd
 * @how-to        Get tutoraspire.com FREE
 * @sourcecode    https://tutoraspire.com/?p=19967
 * @author        Tutor Aspire
 * @testedwith    WooCommerce 3.2.5
 */

function tutoraspire_show_sales_to_admin_325() {
global $product, $post;
if ( current_user_can( 'administrator' ) ) {
echo '
SALES (admin-only):
    '; $orders = get_posts( array( 'post_type' => 'shop_order', 'post_status' => 'wc-completed' ) ); foreach ($orders as $order) { $order = new WC_Order( $order->ID ); $items = $order->get_items(); foreach( $items as $item ) { $product_id = $item['product_id']; if ( $post->ID == $product_id ) { echo '
  1. ' . $order->get_billing_first_name() . ' ' . $order->get_billing_last_name() . '
  2. '; } } } echo '
'; } } add_action( 'woocommerce_after_shop_loop_item', 'tutoraspire_show_sales_to_admin_325', 10 );

WooCommerce before 3.0 Snippet: Display Sales to Admin @ FrontEnd Loop

/**
 * @snippet       Display Sales to Admin @ FrontEnd
 * @how-to        Get tutoraspire.com FREE
 * @sourcecode    https://tutoraspire.com/?p=19967
 * @author        Tutor Aspire
 * @testedwith    WooCommerce 2.5.2
 */

function tutoraspire_show_sales_to_admin() {
global $product, $post;
if ( current_user_can( 'administrator' ) ) {
echo '
SALES (admin-only):
    '; $orders = get_posts( array( 'post_type' => 'shop_order', 'post_status' => 'wc-completed' ) ); foreach ($orders as $order) { $order = new WC_Order($order->ID); $items = $order->get_items(); foreach($items as $item) { $product_id = $item['product_id']; if ($post->ID == $product_id) { echo '
  1. ' . $order->billing_first_name . ' ' . $order->billing_last_name . '
  2. '; } } } echo '
'; } } add_action('woocommerce_after_shop_loop_item','tutoraspire_show_sales_to_admin', 10);

And a bit of CSS customization:

.sales-admin {
text-align:left;
margin: 5% auto;
padding: 0 5%;
border: 1px dashed red;
font-size: 14px;
}

You may also like