WooCommerce: Display Sales to Admin @ FrontEnd

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 CustomizeWoo.com FREE
 * @sourcecode    https://businessbloomer.com/?p=19967
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.2.5
 */

function bbloomer_show_sales_to_admin_325() {
global $product, $post;
if ( current_user_can( 'administrator' ) ) {
echo '<div class="sales-admin"><b>SALES (admin-only):</b><ol>';
$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 '<li>' . $order->get_billing_first_name() . ' ' . $order->get_billing_last_name() . '</li>'; }   
 }            
} 
echo '</ol></div>';
}
}

add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_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 CustomizeWoo.com FREE
 * @sourcecode    https://businessbloomer.com/?p=19967
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 2.5.2
 */

function bbloomer_show_sales_to_admin() {
global $product, $post;
if ( current_user_can( 'administrator' ) ) {
echo '<div class="sales-admin"><b>SALES (admin-only):</b><ol>';
$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 '<li>' . $order->billing_first_name . ' ' . $order->billing_last_name . '</li>'; }   
  }            
} 
echo '</ol></div>';
}
}

add_action('woocommerce_after_shop_loop_item','bbloomer_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;
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Add Second Description @ Product Category Pages
    In terms of SEO, if you’re trying to rank your product category pages, you really need to make the most of the default WooCommerce product category “description” and “thumbnail”. Most themes, if compatible with WooCommerce, will show this content right below the product category name and above products. Nothing new so far. But what if […]
  • WooCommerce: Add Column to Orders Table @ WP Dashboard
    The WooCommerce Orders Table, which can be found under WP Dashboard > WooCommerce > Orders, provides us with 7 default columns: Order – Date – Status – Billing – Ship to – Total – Actions. This is used by shop managers to have an overview of all orders, before eventually clicking on a specific one. […]
  • WooCommerce: Display Custom Filters @ WP Dashboard > Products
    If you go to WordPress Dashboard > Products you will find default product admin filters such as “Select a category”, “Filter by product type”, “Filter by stock status”. What if you want to add more custom filters to let your shop managers find products easily? For example, you could add “Filter by product tag” (“product […]
  • WooCommerce: Hide/Show The WP Admin Bar
    In previous WooCommerce versions, new customers could access the WP Admin black bar after purchase. Now this seems fixed. Still, what about other user roles, and what if you want to override this default behavior? Well, here’s a quick snippet for you – feel free to use it in your own WooCommerce site. Enjoy!
  • WooCommerce: Calculate Sales by State
    You’re filing your tax returns and need to know how much you earned in each state… but then find out WooCommerce doesn’t give you this calculation by default within its reports! Don’t worry – today I’ll share a quick snippet so that you can calculate the amount you need in a second. Feel free to […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

4 thoughts on “WooCommerce: Display Sales to Admin @ FrontEnd

  1. How would this work where you have created a custom loop for the products?

    1. I guess it’s just a matter of changing the hook so it displays on custom loops as well

  2. this snippet is not working on woocommerce 3.2.4

    1. Abdul, thanks so much for your comment! You’re 100% right ๐Ÿ™‚ I just posted a new update for the latest version of Woo.

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *