Created
July 16, 2025 11:18
-
-
Save mehrshaddarzi/2bc8fe0dde5c2eab6bb867ba848a3805 to your computer and use it in GitHub Desktop.
Create New Report Page WooCommerce By Select Box "woocommerce-checkout-field-editor-pro" plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Advanced_WC_Report { | |
public function __construct() { | |
add_action('admin_menu', array($this, 'add_admin_menu')); | |
add_action('admin_head', array($this, 'add_admin_styles')); | |
} | |
// اضافه کردن منوی گزارش | |
public function add_admin_menu() { | |
add_submenu_page( | |
'woocommerce', | |
'گزارشهای نحوه آشنایی', | |
'گزارشهای نحوه آشنایی', | |
'manage_woocommerce', | |
'advanced-wc-report', | |
array($this, 'render_report_page') | |
); | |
} | |
// استایلهای صفحه | |
public function add_admin_styles() { | |
$current_screen = get_current_screen(); | |
if ($current_screen->id === 'woocommerce_page_advanced-wc-report') { | |
echo '<style> | |
.report-container { | |
margin-top: 20px; | |
background: #fff; | |
padding: 20px; | |
border-radius: 4px; | |
box-shadow: 0 1px 1px rgba(0,0,0,0.04); | |
} | |
.report-filters { | |
display: flex; | |
gap: 20px; | |
margin-bottom: 20px; | |
align-items: center; | |
} | |
.report-filters select { | |
min-width: 200px; | |
padding: 2px; | |
height: 40px; | |
} | |
.report-results table { | |
width: 100%; | |
border-collapse: collapse; | |
} | |
.report-results th, .report-results td { | |
padding: 12px; | |
text-align: right; | |
border-bottom: 1px solid #f0f0f0; | |
} | |
.report-results th { | |
background: #f8f8f8; | |
font-weight: 600; | |
} | |
.report-results tr:nth-child(even) { | |
background-color: #f9f9f9; | |
} | |
.no-data { | |
text-align: center; | |
padding: 20px; | |
color: #666; | |
} | |
</style>'; | |
} | |
} | |
// صفحه گزارش | |
public function render_report_page() { | |
$periods = array( | |
'1' => '1 روز گذشته', | |
'7' => '7 روز گذشته', | |
'30' => '30 روز گذشته', | |
'60' => '60 روز گذشته', | |
'90' => '90 روز گذشته', | |
'120' => '120 روز گذشته', | |
'365' => '365 روز گذشته' | |
); | |
$report_types = array( | |
'how_to_know' => 'نحوه آشنایی', | |
'source' => 'مبدا' | |
); | |
$selected_period = isset($_GET['period']) ? sanitize_text_field($_GET['period']) : '30'; | |
$selected_report_type = isset($_GET['report_type']) ? sanitize_text_field($_GET['report_type']) : 'how_to_know'; | |
$report_data = $this->generate_report($selected_period, $selected_report_type); | |
$options = $this->get_options($selected_report_type); | |
?> | |
<div class="wrap"> | |
<h1>گزارشهای پیشرفته ووکامرس</h1> | |
<div class="report-container"> | |
<form method="get" action="" class="report-filters"> | |
<input type="hidden" name="page" value="advanced-wc-report"> | |
<div> | |
<label for="report_type">نوع گزارش:</label> | |
<select name="report_type" id="report_type" onchange="this.form.submit()"> | |
<?php foreach ($report_types as $value => $label): ?> | |
<option value="<?php echo esc_attr($value); ?>" <?php selected($selected_report_type, $value); ?>> | |
<?php echo esc_html($label); ?> | |
</option> | |
<?php endforeach; ?> | |
</select> | |
</div> | |
<div> | |
<label for="period">بازه زمانی:</label> | |
<select name="period" id="period" onchange="this.form.submit()"> | |
<?php foreach ($periods as $value => $label): ?> | |
<option value="<?php echo esc_attr($value); ?>" <?php selected($selected_period, $value); ?>> | |
<?php echo esc_html($label); ?> | |
</option> | |
<?php endforeach; ?> | |
</select> | |
</div> | |
</form> | |
<div class="report-results"> | |
<table> | |
<thead> | |
<tr> | |
<th><?php echo esc_html($report_types[$selected_report_type]); ?></th> | |
<th>تعداد سفارشات</th> | |
<th>درصد</th> | |
</tr> | |
</thead> | |
<tbody> | |
<?php if (!empty($report_data)): ?> | |
<?php foreach ($report_data as $key => $count): ?> | |
<tr> | |
<td><?php echo isset($options[$key]) ? esc_html($options[$key]) : esc_html($key); ?></td> | |
<td><?php echo esc_html($count); ?></td> | |
<td><?php echo round(($count / array_sum($report_data)) * 100, 2); ?>%</td> | |
</tr> | |
<?php endforeach; ?> | |
<?php else: ?> | |
<tr> | |
<td colspan="3" class="no-data">هیچ دادهای برای نمایش وجود ندارد</td> | |
</tr> | |
<?php endif; ?> | |
</tbody> | |
</table> | |
</div> | |
</div> | |
</div> | |
<?php | |
} | |
// تولید گزارش | |
private function generate_report($days, $report_type) { | |
global $wpdb; | |
$date = date('Y-m-d', strtotime("-{$days} days")); | |
$results = array(); | |
// دریافت سفارشات در بازه زمانی مشخص | |
$orders = $wpdb->get_results($wpdb->prepare( | |
"SELECT ID FROM {$wpdb->posts} | |
WHERE post_type = 'shop_order' | |
AND post_date >= %s | |
AND post_status IN ('wc-completed', 'wc-processing')", | |
$date | |
)); | |
// جمعآوری دادهها بر اساس نوع گزارش | |
foreach ($orders as $order) { | |
$meta_value = ''; | |
if ($report_type === 'how_to_know') { | |
$meta_value = get_post_meta($order->ID, '_billing_', true); | |
} elseif ($report_type === 'source') { | |
$meta_value = get_post_meta($order->ID, '_wc_order_attribution_utm_source', true); | |
} | |
if (!empty($meta_value)) { | |
if (!isset($results[$meta_value])) { | |
$results[$meta_value] = 0; | |
} | |
$results[$meta_value]++; | |
} | |
} | |
// مرتب سازی بر اساس تعداد | |
arsort($results); | |
return $results; | |
} | |
// دریافت گزینهها بر اساس نوع گزارش | |
private function get_options($report_type) { | |
if ($report_type === 'how_to_know') { | |
$fields_billing = get_option('wc_fields_billing', array()); | |
return isset($fields_billing['billing_']['options']) ? $fields_billing['billing_']['options'] : array(); | |
} | |
// برای مبدا (source) هیچ گزینه پیشفرضی نداریم و مقادیر مستقیماً از دیتابیس خوانده میشوند | |
return array(); | |
} | |
} | |
new Advanced_WC_Report(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment