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.

				
					<?php
/*
Plugin Name: WooCommerce Payment Methods Statistics
Description: Adds a submenu under WooCommerce for payment methods.
Version: 1.0
Author: <a href="https://josselynjayant.fr">Josselyn jayant</a> & <a href="https://wycan.fr">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">
            <tr>
                <th>Méthode de paiement</th>
                <th>Nombre de commandes</th>
            </tr>';

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

        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?

Display the total weight in the cart and order page of woocommerce

This code will allow you to know the weight of the order in the cart and order pages of woocommerce.

Custom CSS ID link to Elementor Nested Tabs

This script will allow us to run an URL directly to an Elementor tab

Create a dropdown from a Custom Post Type list

This code will allow us to display a drop-down menu which lists all the CPTs present in our ACF

Filter number of orders by country

Set up a submenu that allows us to filter a date range and display the number of orders depending on the country.

Filter Elementor Post Widgets by date of an ACF field

We will display the posts created with ACF and displayed with the Elementor Post widget using a date field entered via ACF

Plugin to attached your Terms of Sales to the WooCommerce email

With this little plugin we will be able to upload a file and choose which WooCommerce order status we will link it to!
Let's talk ?

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