|
17 | 17 | }
|
18 | 18 |
|
19 | 19 | /**
|
20 |
| - * Add loyalty points on successful customer registration |
| 20 | + * Add loyalty points on successful customer registration. |
21 | 21 | *
|
22 |
| - * @param int $user_id |
23 |
| - * |
24 |
| - * @since 1.0.0 |
| 22 | + * @param int $user_id The ID of the registered user. |
| 23 | + * |
| 24 | + * @since 1.0.0 |
25 | 25 | * @return void
|
26 | 26 | */
|
27 | 27 | function clwc_customer_registration( $user_id ) {
|
28 |
| - // Check settings before adding any points. |
29 |
| - if ( 'on' == clwc_loyalty_points_activate() && 0 != clwc_earning_points_customer_registration() ) { |
30 |
| - // Get user data. |
| 28 | + error_log( 'Running clwc_customer_registration for user ID: ' . $user_id ); |
| 29 | + |
| 30 | + if ( clwc_loyalty_points_activate() && 0 != clwc_earning_points_customer_registration() ) { |
31 | 31 | $user_info = get_userdata( $user_id );
|
32 |
| - $user_name = ''; |
33 |
| - $user_email = ''; |
34 |
| - |
35 |
| - if ( $user_info ) { |
36 |
| - $user_email = $user_info->user_email; |
37 |
| - $user_name = $user_info->display_name; |
38 |
| - } |
39 |
| - |
40 |
| - // Get user's loyalty points. |
41 |
| - $old_points = get_user_meta( $user_id, 'clwc_loyalty_points', TRUE ); |
42 |
| - |
43 |
| - // Set empty variable to zero. |
44 |
| - if ( '' == $old_points ) { |
45 |
| - $old_points = 0; |
46 |
| - } |
47 |
| - |
48 |
| - // Add loyalty points for customer registration. |
| 32 | + $user_email = $user_info ? $user_info->user_email : ''; |
| 33 | + $user_name = $user_info ? $user_info->display_name : ''; |
| 34 | + |
| 35 | + $old_points = (int) get_user_meta( $user_id, 'clwc_loyalty_points', true ) ?: 0; |
49 | 36 | $new_points = $old_points + clwc_earning_points_customer_registration();
|
50 |
| - |
51 |
| - // Update customer loyalty points. |
| 37 | + |
52 | 38 | update_user_meta( $user_id, 'clwc_loyalty_points', $new_points, $old_points );
|
53 | 39 |
|
54 |
| - // Define a descriptive details string for the log entry. |
55 | 40 | $details = sprintf(
|
56 | 41 | esc_html__( 'Customer awarded %d loyalty points for registering an account.', 'customer-loyalty-for-woocommerce' ),
|
57 | 42 | clwc_earning_points_customer_registration()
|
58 | 43 | );
|
59 | 44 |
|
60 |
| - // Log the usage of customer loyalty points. |
61 |
| - clwc_insert_loyalty_log_entry( $user_id, $user_name, $user_email, $new_points, $details ); |
| 45 | + $log_result = clwc_insert_loyalty_log_entry( $user_id, $user_name, $user_email, $new_points, $details ); |
| 46 | + error_log( 'Loyalty log entry result: ' . print_r( $log_result, true ) ); |
| 47 | + } else { |
| 48 | + error_log( 'Loyalty points not activated or registration points not set.' ); |
62 | 49 | }
|
63 |
| - |
64 | 50 | }
|
65 | 51 | add_action( 'user_register', 'clwc_customer_registration', 10, 1 );
|
66 | 52 |
|
67 | 53 | /**
|
68 |
| - * Add loyalty points on order completion |
| 54 | + * Add loyalty points on order completion. |
69 | 55 | *
|
70 |
| - * @since 1.0.0 |
| 56 | + * @param int $order_id WooCommerce order ID. |
| 57 | + * |
| 58 | + * @since 1.0.0 |
71 | 59 | * @return void
|
72 | 60 | */
|
73 |
| -function clwc_customer_first_order() { |
74 |
| - // Check settings before adding any points. |
75 |
| - if ( 'on' == clwc_loyalty_points_activate() && 0 != clwc_earning_points_order_complete() ) { |
76 |
| - // Get user's loyalty points. |
77 |
| - $old_points = get_user_meta( get_current_user_id(), 'clwc_loyalty_points', TRUE ); |
78 |
| - |
79 |
| - // Set empty variable to zero. |
80 |
| - if ( '' == $old_points ) { |
81 |
| - $old_points = 0; |
82 |
| - } |
| 61 | +function clwc_customer_first_order( $order_id ) { |
| 62 | + if ( clwc_loyalty_points_activate() && 0 != clwc_earning_points_order_complete() ) { |
| 63 | + $order = wc_get_order( $order_id ); |
| 64 | + $user_id = $order->get_user_id(); |
83 | 65 |
|
84 |
| - // Add loyalty points for completing an order. |
85 |
| - $new_points = $old_points + clwc_earning_points_order_complete(); |
| 66 | + if ( $user_id ) { |
| 67 | + $old_points = (int) get_user_meta( $user_id, 'clwc_loyalty_points', true ) ?: 0; |
| 68 | + $earned_points = clwc_earning_points_order_complete(); |
| 69 | + $new_points = $old_points + $earned_points; |
86 | 70 |
|
87 |
| - // Update customer loyalty points. |
88 |
| - update_user_meta( get_current_user_id(), 'clwc_loyalty_points', $new_points, $old_points ); |
89 |
| - } |
| 71 | + update_user_meta( $user_id, 'clwc_loyalty_points', $new_points, $old_points ); |
| 72 | + |
| 73 | + $user_info = get_userdata( $user_id ); |
| 74 | + $user_name = $user_info ? $user_info->display_name : ''; |
| 75 | + $user_email = $user_info ? $user_info->user_email : ''; |
90 | 76 |
|
| 77 | + $details = sprintf( |
| 78 | + esc_html__( 'Customer awarded %d loyalty points for completing an order.', 'customer-loyalty-for-woocommerce' ), |
| 79 | + $earned_points |
| 80 | + ); |
| 81 | + clwc_insert_loyalty_log_entry( $user_id, $user_name, $user_email, $earned_points, $details ); |
| 82 | + } |
| 83 | + } |
91 | 84 | }
|
92 | 85 | add_action( 'woocommerce_thankyou', 'clwc_customer_first_order', 10 );
|
93 | 86 |
|
94 | 87 | /**
|
95 |
| - * Add loyalty points for every dollar spent |
| 88 | + * Add loyalty points for every dollar spent. |
96 | 89 | *
|
97 |
| - * @since 1.0.0 |
| 90 | + * @param int $order_id WooCommerce order ID. |
| 91 | + * |
| 92 | + * @since 1.0.0 |
98 | 93 | * @return void
|
99 | 94 | */
|
100 | 95 | function clwc_customer_money_spent( $order_id ) {
|
101 |
| - // Check settings before adding any points. |
102 |
| - if ( 'on' == clwc_loyalty_points_activate() && 0 != clwc_earning_points_money_spent() ) { |
103 |
| - // Get order data. |
104 |
| - $order = wc_get_order( $order_id ); |
105 |
| - |
106 |
| - // Get order total. |
107 |
| - $order_total = $order->get_total(); |
108 |
| - |
109 |
| - // Get order subtotal. |
110 |
| - if ( 'subtotal' === clwc_loyalty_points_redeem_points_calculation_type() ) { |
111 |
| - $order_total = $order->get_subtotal(); |
| 96 | + if ( clwc_loyalty_points_activate() && 0 != clwc_earning_points_money_spent() ) { |
| 97 | + $order = wc_get_order( $order_id ); |
| 98 | + $user_id = $order->get_user_id(); |
| 99 | + |
| 100 | + if ( $user_id ) { |
| 101 | + $order_total = ( 'order_subtotal' === clwc_loyalty_points_redeem_points_calculation_type() ) |
| 102 | + ? $order->get_subtotal() |
| 103 | + : $order->get_total(); |
| 104 | + |
| 105 | + $old_points = (int) get_user_meta( $user_id, 'clwc_loyalty_points', true ) ?: 0; |
| 106 | + $points_per_dollar = clwc_earning_points_money_spent(); |
| 107 | + $earned_points = round( $order_total * $points_per_dollar ); |
| 108 | + $new_points = $old_points + $earned_points; |
| 109 | + |
| 110 | + update_user_meta( $user_id, 'clwc_loyalty_points', $new_points, $old_points ); |
| 111 | + |
| 112 | + $user_info = get_userdata( $user_id ); |
| 113 | + $user_name = $user_info ? $user_info->display_name : ''; |
| 114 | + $user_email = $user_info ? $user_info->user_email : ''; |
| 115 | + |
| 116 | + $details = sprintf( |
| 117 | + esc_html__( 'Customer awarded %d loyalty points for spending $%s.', 'customer-loyalty-for-woocommerce' ), |
| 118 | + $earned_points, |
| 119 | + number_format( $order_total, 2 ) |
| 120 | + ); |
| 121 | + clwc_insert_loyalty_log_entry( $user_id, $user_name, $user_email, $earned_points, $details ); |
112 | 122 | }
|
113 |
| - |
114 |
| - // Get user's loyalty points. |
115 |
| - $old_points = get_user_meta( get_current_user_id(), 'clwc_loyalty_points', true ); |
116 |
| - |
117 |
| - // Set empty variable to zero. |
118 |
| - if ( '' == $old_points ) { |
119 |
| - $old_points = 0; |
120 |
| - } |
121 |
| - |
122 |
| - // Get money spent loyalty points. |
123 |
| - $money_spent = $order_total * clwc_earning_points_money_spent(); |
124 |
| - |
125 |
| - // Get new loyalty points total. |
126 |
| - $new_points = $old_points + round( $money_spent ); |
127 |
| - |
128 |
| - // Update customer loyalty points. |
129 |
| - update_user_meta( get_current_user_id(), 'clwc_loyalty_points', $new_points, $old_points ); |
130 | 123 | }
|
131 |
| - |
132 | 124 | }
|
133 | 125 | add_action( 'woocommerce_thankyou', 'clwc_customer_money_spent', 10 );
|
0 commit comments