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 '
Brak danych w tym miesiącu.
'; } else { echo 'Brak sprzedaży w tym miesiącu.
'; } else { echo 'Brak użytych kuponów w tym miesiącu.
'; } else { echo '