Adds a submenu under WooCommerce for payment methods statistics

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.

Plugin Name: WooCommerce Payment Methods Statistics
Description: Adds a submenu under WooCommerce for payment methods.
Version: 1.0
Author: <a href="">Josselyn jayant</a> & <a href="">WYCAN</a>

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 '<h1>Statistiques des modes de paiement</h1>';

    // Ajout d'une ligne de texte
    echo '<p>Bien noter que seules les commandes qui ont le status "Terminée" seront prises en compte dans les statistiques.</p>';

    echo '<form method="post" style="margin-bottom: 20px;">';  // Ajout d'une marge en bas pour l'espacement
    echo 'Date de début: <input type="date" name="start_date">';
    echo 'Date de fin: <input type="date" name="end_date">';
    echo '<input type="submit" value="Afficher les statistiques">';
    echo '</form>';

    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 '<table border="1" cellpadding="10">
                <th>Méthode de paiement</th>
                <th>Nombre de commandes</th>

        foreach ($results as $result) {
            echo '<tr>
                <td>' . esc_html($result->payment_method) . '</td>
                <td>' . esc_html($result->total_orders) . '</td>

        echo '</table>';

Want to leave a comment ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to see more code snippets?
Let's talk ?

A question about WordPress?
A web project to be outsourced to a freelancer?
I am your man!