
Afficher le total des produits WooCommerce
Code Snippet
Afficher le total des produits WooCommerce
function mon_site_afficher_total_produits() {
if ( ! post_type_exists( 'product' ) ) {
return '';
}
$product_counts = wp_count_posts( 'product' );
$total_products = isset( $product_counts->publish ) ? (int) $product_counts->publish : 0;
return esc_html( number_format_i18n( $total_products ) );
}
add_shortcode( 'total_produits', 'mon_site_afficher_total_produits' );
Rapport CA 3 Dernières Années – Dashboard WP
Code Snippet
Rapport CA 3 Dernières Années – Dashboard WP
// 1. Ajouter un widget personnalisé au tableau de bord WordPress
add_action( 'wp_dashboard_setup', 'ajouter_widget_statistiques_3_ans' );
function ajouter_widget_statistiques_3_ans() {
wp_add_dashboard_widget(
'statistiques_ventes_3_ans',
'📊 Bilan des Ventes (3 Dernières Années)',
'afficher_contenu_widget_statistiques'
);
}
// 2. Requête SQL directe et affichage des résultats
function afficher_contenu_widget_statistiques() {
global $wpdb;
$annee_actuelle = (int) date( 'Y' );
$annee_depart = $annee_actuelle - 2; // Inclus l'année en cours + les 2 précédentes
// Requête SQL compatible avec les tables WooCommerce modernes (HPOS) et classiques (wp_posts)
$sql = "
SELECT
YEAR(p.post_date) AS annee,
COUNT(p.ID) AS total_commandes,
SUM(pm.meta_value) AS chiffre_affaires
FROM {$wpdb->posts} p
INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id
WHERE p.post_type = 'shop_order'
AND p.post_status IN ('wc-completed', 'wc-processing')
AND pm.meta_key = '_order_total'
AND YEAR(p.post_date) >= %d
GROUP BY YEAR(p.post_date)
ORDER BY annee DESC
";
$resultats = $wpdb->get_results( $wpdb->prepare( $sql, $annee_depart ) );
if ( empty( $resultats ) ) {
echo '<p>Aucune commande terminée ou en cours de traitement trouvée pour ces 3 dernières années.</p>';
return;
}
echo '<table style="width:100%; border-collapse: collapse; text-align: left;">';
echo '<tr style="border-bottom: 2px solid #ccc; font-weight: bold;">
<th style="padding: 8px;">Année</th>
<th style="padding: 8px;">Commandes</th>
<th style="padding: 8px;">Total CA</th>
</tr>';
foreach ( $resultats as $ligne ) {
echo '<tr style="border-bottom: 1px solid #eee;">';
echo '<style>td { padding: 8px; }</style>';
echo '<td><strong>' . esc_html( $ligne->annee ) . '</strong></td>';
echo '<td>' . esc_html( number_format_i18n( $ligne->total_commandes ) ) . '</td>';
echo '<td>' . wc_price( $ligne->chiffre_affaires ) . '</td>';
echo '</tr>';
}
echo '</table>';
}
Définir un montant minimum de commande obligatoire
Code Snippet
Définir un montant minimum de commande obligatoire
// 1. Afficher l'avertissement sur la page panier et le checkout
add_action( 'woocommerce_before_cart', 'afficher_notice_montant_minimum' );
add_action( 'woocommerce_checkout_process', 'afficher_notice_montant_minimum' );
function afficher_notice_montant_minimum() {
$minimum = 100; // Montant minimum requis
if ( WC()->cart->subtotal < $minimum ) {
$message = sprintf(
'Le montant minimum de commande est de %s. Votre sous-total actuel est de %s.',
wc_price( $minimum ),
wc_price( WC()->cart->subtotal )
);
if ( is_cart() ) {
wc_print_notice( $message, 'error' );
} else {
wc_add_notice( $message, 'error' );
}
}
}
// 2. Retirer le bouton "Commander" de la page panier sous le seuil
add_action( 'woocommerce_proceed_to_checkout', 'masquer_bouton_commander_si_seuil_non_atteint', 1 );
function masquer_bouton_commander_si_seuil_non_atteint() {
$minimum = 100; // Montant minimum requis
if ( WC()->cart->subtotal < $minimum ) {
// Supprime l'action par défaut qui affiche le bouton "Valider la commande"
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
}
}
Bouton Woocommerce – Vider le panier
Code Snippet
Bouton Woocommerce – Vider le panier
// 1. Afficher le bouton sur la page panier
add_action( 'woocommerce_cart_actions', 'ajouter_bouton_vider_panier' );
function ajouter_bouton_vider_panier() {
$cart_url = wc_get_cart_url();
$clear_url = add_query_arg( 'empty-cart', 'true', $cart_url );
echo '<a class="button empty-cart-button" href="' . esc_url( $clear_url ) . '" onclick="return confirm(\'Es-tu sûr de vouloir vider ton panier ?\');">Vider le panier</a>';
}
// 2. Traiter la demande et vider le panier
add_action( 'template_redirect', 'executer_vidage_panier' );
function executer_vidage_panier() {
if ( is_cart() && isset( $_GET['empty-cart'] ) && $_GET['empty-cart'] === 'true' ) {
WC()->cart->empty_cart();
wc_add_notice( __( 'Le panier a été entièrement vidé.', 'woocommerce' ), 'notice' );
wp_safe_redirect( wc_get_cart_url() );
exit;
}
}

Ajouter un enqueue Google Analytics
Code Snippet
Ajouter un enqueue Google Analytics
add_action('wp_head', 'themespress_googleanalytics');
function themespress_googleanalytics() { ?>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-X"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXXX-X');
</script>
<?php }

Ajouter un Enqueue Font Awesome
Code Snippet
Ajouter un Enqueue Font Awesome
// Enqueue Font Awesome 5 in WordPress
function themespress_load_font_awesome() {
// You can find the current URL for the latest version here: https://fontawesome.com/start
wp_enqueue_style( 'font-awesome-free', 'https://use.fontawesome.com/releases/v5.6.3/css/all.css' );
}
add_action( 'wp_enqueue_scripts', 'themespress_load_font_awesome' );