array( 'wc-completed', 'wc-processing' ), 'limit' => -1, 'date_created' => $first_day . '...' . $last_day, 'return' => 'objects', ); $orders = wc_get_orders( $args ); $total_sales = 0; $total_discount = 0; $total_items = 0; $order_count = count( $orders ); $products_sold = array(); $coupons_used = array(); $customers_revenue = array(); $unique_emails = array(); foreach ( $orders as $order ) { $order_total = $order->get_total(); $total_sales += $order_total; $total_discount += $order->get_total_discount(); $email = $order->get_billing_email(); $name = trim( $order->get_billing_first_name() . ' ' . $order->get_billing_last_name() ); $name = ! empty( $name ) ? $name : 'Klient (Brak danych)'; if ( ! empty( $email ) ) { if ( ! isset( $unique_emails[ $email ] ) ) { $unique_emails[ $email ] = $name; } if ( isset( $customers_revenue[ $email ] ) ) { $customers_revenue[ $email ]['revenue'] += $order_total; } else { $customers_revenue[ $email ] = array( 'name' => $name, 'revenue' => $order_total, ); } } foreach ( $order->get_items() as $item ) { $product_id = $item->get_product_id(); $qty = $item->get_quantity(); $total_items += $qty; if ( isset( $products_sold[ $product_id ] ) ) { $products_sold[ $product_id ] += $qty; } else { $products_sold[ $product_id ] = $qty; } } foreach ( $order->get_coupon_codes() as $coupon_code ) { if ( isset( $coupons_used[ $coupon_code ] ) ) { $coupons_used[ $coupon_code ]++; } else { $coupons_used[ $coupon_code ] = 1; } } } arsort( $products_sold ); $top_products = array_slice( $products_sold, 0, 5, true ); arsort( $coupons_used ); $top_coupons = array_slice( $coupons_used, 0, 5, true ); uasort( $customers_revenue, function( $a, $b ) { return $b['revenue'] <=> $a['revenue']; }); $top_customers = array_slice( $customers_revenue, 0, 5, true ); $new_customers_list = array(); foreach ( $unique_emails as $email => $name ) { $prev_orders = wc_get_orders( array( 'billing_email' => $email, 'status' => array( 'wc-completed', 'wc-processing' ), 'date_created' => '<' . $first_day, 'limit' => 1, 'return' => 'ids', ) ); if ( empty( $prev_orders ) ) { $new_customers_list[ $email ] = $name; } } $aov = $order_count > 0 ? $total_sales / $order_count : 0; return array( 'count' => $order_count, 'revenue' => $total_sales, 'aov' => $aov, 'total_items' => $total_items, 'total_discount' => $total_discount, 'new_customers' => count( $new_customers_list ), 'new_customers_list' => $new_customers_list, 'top_products' => $top_products, 'top_coupons' => $top_coupons, 'top_customers' => $top_customers, ); } function aion_calc_diff_html( $current, $previous ) { if ( $previous == 0 ) { $diff = $current > 0 ? 100 : 0; } else { $diff = ( ( $current - $previous ) / $previous ) * 100; } $color = $diff >= 0 ? '#46b450' : '#dc3232'; $sign = $diff > 0 ? '+' : ''; return '' . esc_html( $sign ) . esc_html( round( $diff, 1 ) ) . '%'; } function aion_calc_diff_html_inverted( $current, $previous ) { if ( $previous == 0 ) { $diff = $current > 0 ? 100 : 0; } else { $diff = ( ( $current - $previous ) / $previous ) * 100; } $color = $diff > 0 ? '#dc3232' : '#46b450'; $sign = $diff > 0 ? '+' : ''; return '' . esc_html( $sign ) . esc_html( round( $diff, 1 ) ) . '%'; } function aion_woodashboard_page() { if ( ! current_user_can( 'manage_woocommerce' ) ) { return; } $current_month = gmdate( 'n' ); $current_year = gmdate( 'Y' ); if ( isset( $_GET['aion_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['aion_nonce'] ) ), 'aion_woodashboard_filter' ) ) { $current_month = isset( $_GET['aion_month'] ) ? intval( $_GET['aion_month'] ) : $current_month; $current_year = isset( $_GET['aion_year'] ) ? intval( $_GET['aion_year'] ) : $current_year; } $prev_month_m = $current_month - 1 == 0 ? 12 : $current_month - 1; $prev_month_y = $current_month - 1 == 0 ? $current_year - 1 : $current_year; $prev_year_m = $current_month; $prev_year_y = $current_year - 1; $stats_current = aion_get_woo_stats( $current_month, $current_year ); $stats_mom = aion_get_woo_stats( $prev_month_m, $prev_month_y ); $stats_yoy = aion_get_woo_stats( $prev_year_m, $prev_year_y ); echo '
'; echo '
'; echo '

Aion Dashboard for WooCommerce

'; echo '
'; echo ''; wp_nonce_field( 'aion_woodashboard_filter', 'aion_nonce' ); echo ''; echo ''; echo ''; echo '
'; echo '
'; echo '
'; echo '
'; echo '

Liczba zamówień

'; echo '
' . esc_html( $stats_current['count'] ) . '
'; echo '
Miesiąc do miesiąca: ' . wp_kses_post( aion_calc_diff_html( $stats_current['count'], $stats_mom['count'] ) ) . '
'; echo '
Rok do roku: ' . wp_kses_post( aion_calc_diff_html( $stats_current['count'], $stats_yoy['count'] ) ) . '
'; echo '
'; echo '
'; echo '

Przychód brutto

'; echo '
' . wp_kses_post( wc_price( $stats_current['revenue'] ) ) . '
'; echo '
Miesiąc do miesiąca: ' . wp_kses_post( aion_calc_diff_html( $stats_current['revenue'], $stats_mom['revenue'] ) ) . '
'; echo '
Rok do roku: ' . wp_kses_post( aion_calc_diff_html( $stats_current['revenue'], $stats_yoy['revenue'] ) ) . '
'; echo '
'; echo '
'; echo '

Średnia wartość koszyka (AOV)

'; echo '
' . wp_kses_post( wc_price( $stats_current['aov'] ) ) . '
'; echo '
Miesiąc do miesiąca: ' . wp_kses_post( aion_calc_diff_html( $stats_current['aov'], $stats_mom['aov'] ) ) . '
'; echo '
Rok do roku: ' . wp_kses_post( aion_calc_diff_html( $stats_current['aov'], $stats_yoy['aov'] ) ) . '
'; echo '
'; echo '
'; echo '

Sprzedane produkty (szt.)

'; echo '
' . esc_html( $stats_current['total_items'] ) . '
'; echo '
Miesiąc do miesiąca: ' . wp_kses_post( aion_calc_diff_html( $stats_current['total_items'], $stats_mom['total_items'] ) ) . '
'; echo '
Rok do roku: ' . wp_kses_post( aion_calc_diff_html( $stats_current['total_items'], $stats_yoy['total_items'] ) ) . '
'; echo '
'; echo '
'; echo '

Wartość udzielonych rabatów

'; echo '
' . wp_kses_post( wc_price( $stats_current['total_discount'] ) ) . '
'; echo '
Miesiąc do miesiąca: ' . wp_kses_post( aion_calc_diff_html_inverted( $stats_current['total_discount'], $stats_mom['total_discount'] ) ) . '
'; echo '
Rok do roku: ' . wp_kses_post( aion_calc_diff_html_inverted( $stats_current['total_discount'], $stats_yoy['total_discount'] ) ) . '
'; echo '
'; echo '
'; echo '

Nowi klienci

'; echo '
' . esc_html( $stats_current['new_customers'] ) . '
'; echo '
Miesiąc do miesiąca: ' . wp_kses_post( aion_calc_diff_html( $stats_current['new_customers'], $stats_mom['new_customers'] ) ) . '
'; echo '
Rok do roku: ' . wp_kses_post( aion_calc_diff_html( $stats_current['new_customers'], $stats_yoy['new_customers'] ) ) . '
'; if ( ! empty( $stats_current['new_customers_list'] ) ) { echo '
'; echo '
    '; foreach ( $stats_current['new_customers_list'] as $email => $name ) { $customer_url = admin_url( 'admin.php?page=wc-admin&path=/customers&filter-search=' . urlencode( $email ) ); echo '
  • '; echo '
    '; echo '' . esc_html( $name ) . ''; echo '' . esc_html( $email ) . ''; echo '
    '; echo '
  • '; } echo '
'; echo '
'; } else { echo '
Brak nowych klientów.
'; } echo '
'; echo '
'; echo '

Najbardziej dochodowi klienci

'; if ( empty( $stats_current['top_customers'] ) ) { echo '

Brak danych w tym miesiącu.

'; } else { echo '
    '; foreach ( $stats_current['top_customers'] as $email => $data ) { $customer_url = admin_url( 'admin.php?page=wc-admin&path=/customers&filter-search=' . urlencode( $email ) ); echo '
  • '; echo '
    '; echo '' . esc_html( $data['name'] ) . ''; echo '' . esc_html( $email ) . ''; echo '
    '; echo '' . wp_kses_post( wc_price( $data['revenue'] ) ) . ''; echo '
  • '; } echo '
'; } echo '
'; echo '
'; echo '

Top 5 Produktów (wg sztuk)

'; if ( empty( $stats_current['top_products'] ) ) { echo '

Brak sprzedaży w tym miesiącu.

'; } else { echo '
    '; foreach ( $stats_current['top_products'] as $product_id => $qty ) { $product = wc_get_product( $product_id ); if ( $product ) { $edit_link = get_edit_post_link( $product_id ); $title_html = '' . esc_html( $product->get_name() ) . ''; } else { $title_html = 'Usunięty (#' . esc_html( $product_id ) . ')'; } echo '
  • ' . wp_kses_post( $title_html ) . ' ' . esc_html( $qty ) . ' szt.
  • '; } echo '
'; } echo '
'; echo '
'; echo '

Najpopularniejsze kupony

'; if ( empty( $stats_current['top_coupons'] ) ) { echo '

Brak użytych kuponów w tym miesiącu.

'; } else { echo '
    '; foreach ( $stats_current['top_coupons'] as $coupon_code => $uses ) { echo '
  • ' . esc_html( $coupon_code ) . ' ' . esc_html( $uses ) . ' użyć
  • '; } echo '
'; } echo '
'; echo '
'; echo '
'; }