This code will allow you to create a submenu to have statistics on the payment methods used on your WooCommerce store.
Indeed, you will be able to filter according to a specific date range and obtain a table which will indicate the number of orders made and the associated payment methods.
Josselyn jayant & WYCAN
*/
add_action('admin_menu', 'add_wc_payment_stats_submenu');
function add_wc_payment_stats_submenu(){
add_submenu_page('woocommerce', 'Modes de paiement', 'Modes de paiement', 'manage_woocommerce', 'wc-payment-stats', 'display_wc_payment_stats');
}
function display_wc_payment_stats(){
global $wpdb;
echo 'Statistiques des modes de paiement
';
// Ajout d'une ligne de texte
echo 'Bien noter que seules les commandes qui ont le status "Terminée" seront prises en compte dans les statistiques.
';
echo ' ';
if(isset($_POST['start_date']) && isset($_POST['end_date'])){
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'] . ' 23:59:59';
// Récupérez les statistiques pour la plage de dates sélectionnée
$query = $wpdb->prepare("
SELECT pm1.meta_value as payment_method, COUNT(pm1.post_id) as total_orders
FROM $wpdb->postmeta pm1
JOIN $wpdb->postmeta pm2 ON pm1.post_id = pm2.post_id
WHERE pm1.meta_key = '_payment_method'
AND pm2.meta_key = '_completed_date'
AND pm2.meta_value >= %s AND pm2.meta_value <= %s
GROUP BY pm1.meta_value
", $start_date, $end_date);
$results = $wpdb->get_results($query);
echo '
Méthode de paiement
Nombre de commandes
';
foreach ($results as $result) {
echo '
' . esc_html($result->payment_method) . '
' . esc_html($result->total_orders) . '
';
}
echo '
';
}
}