Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Créer une table SQL WordPress personnalisée pour stocker les données de commission
<?php
/**
* Si inexistante, on créée la table SQL "commissions" après l'activation du thème
*/
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$commissions_table_name = $wpdb->prefix . 'commissions';
$commissions_sql = "CREATE TABLE IF NOT EXISTS $commissions_table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
type varchar(45) DEFAULT NULL,
amount decimal(10,2) DEFAULT NULL,
user_id mediumint(9) DEFAULT NULL,
order_id mediumint(9) DEFAULT NULL,
line_product_id mediumint(9) DEFAULT NULL,
line_product_rate decimal(10,2) DEFAULT NULL,
line_product_quantity mediumint(9) DEFAULT NULL,
line_subtotal decimal(10,2) DEFAULT NULL,
user_notified varchar(45) DEFAULT NULL,
time datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($commissions_sql);
<?php
/**
* Permet de savoir combien de "points" un utilisateur possède
* On compte combien de points il a gagné (récompense) et on soustrait le nombre de points qu'il a déjà utilisés (usage)
*/
function msk_get_customer_commission_balance($user_id) {
global $wpdb;
$commissions_table_name = $wpdb->prefix . 'commissions';
$commission_data = $wpdb->get_row(
$wpdb->prepare("
SELECT
IFNULL(sum(IF(type = %s, amount, 0)), 0) as user_gain,
IFNULL(sum(IF(type = %s, amount, 0)), 0) as user_use
FROM $commissions_table_name
WHERE user_id = %d
",
'gain',
'use',
$user_id
)
);
$commission_balance = ($commission_data->user_gain > $commission_data->user_use) ? ($commission_data->user_gain - $commission_data->user_use) : 0;
return array(
'balance' => $commission_balance,
'gain' => $commission_data->user_gain,
'use' => $commission_data->user_use
);
}
/**
* On récupère chaque ligne de commission (récompense ou usage)
*/
function msk_get_customer_commission_data($user_id) {
global $wpdb;
$commissions_table_name = $wpdb->prefix . 'commissions';
$commission = msk_get_customer_commission_balance($user_id);
$commission_data = $wpdb->get_results(
$wpdb->prepare("
SELECT id, type, amount, order_id, line_product_id, line_product_rate, line_product_quantity, time
FROM $commissions_table_name
WHERE user_id = %d
ORDER BY time DESC
",
$user_id
)
);
return array('points' => $commission, 'details' => $commission_data);
}
@harabidi

This comment has been minimized.

Copy link

harabidi commented Jul 11, 2017

bonjour,
merci bcp pour ce code très utile, j'aimerai juste savoir ou le mettre :-S

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.