· 7 years ago · Nov 09, 2018, 05:24 PM
1// taken from my checkout.php
2
3if (is_user_logged_in() && in_array( 'customer', (array) $user->roles) && $_COOKIE['is_ordering'] = true) {
4
5 echo $_COOKIE['game_title'];
6 echo $_COOKIE['total_price'];
7 echo $_COOKIE['is_ordering'];
8
9 $user_id = get_current_user_id();
10
11 global $wpdb;
12
13 $table_name = $wpdb->prefix.'purchases';
14
15 // check if purchases table exists
16 if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
17 $charset_collate = $wpdb->get_charset_collate();
18
19 $sql = "CREATE TABLE $table_name (
20 id mediumint(9) NOT NULL AUTO_INCREMENT,
21 customer_id INT NOT NULL,
22 price float NOT NULL,
23 billing_terms text NOT NULL,
24 title text NOT NULL,
25 purchase_date DATETIME NOT NULL,
26 last_payment DATETIME NOT NULL,
27 UNIQUE KEY id (id)
28 ) $charset_collate;";
29 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
30 dbDelta( $sql );
31 }
32
33 $wpdb->show_errors();
34
35 $charset_collate = $wpdb->get_charset_collate();
36 $sql = "INSERT INTO $table_name (
37 id, customer_id, price, term, title, purchase_date ) VALUES (
38 '', $user_id, $total_price, $billing_term, $game_title, $current_date
39 )
40 )";
41
42 // add new bill item for user
43 $wpdb->query( $wpdb->prepare(
44 "INSERT INTO $table_name
45 (id, customer_id, price, billing_terms, title, purchase_date, last_payment)
46 VALUES ( %s , %s , %s , %s , %s, %s , %s)",
47 array(
48 '',
49 $user_id,
50 $total_price,
51 $billing_terms,
52 $game_title,
53 $current_date,
54 $current_date
55 )
56 ));
57 // get rid of cookies
58 kill_cookie('is_ordering');
59 kill_cookie('total_price');
60 kill_cookie('game_title');
61 kill_cookie('billing_terms');
62
63 }
64
65
66//inside functions.php
67 function kill_cookie($cookie_name) {
68 if (isset($_COOKIE[$cookie_name])) {
69 echo $cookie_name;
70 unset($_COOKIE[$cookie_name]);
71 setcookie($cookie_name, '', time() - 3600, '/'); // empty value and old timestamp
72 }
73 }