WooCommerce:在前端为管理员显示购买者

< 1 min read

仿牌独立站出海专家


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 );