Skip to content

Commit 08040f0

Browse files
committed
👌 IMPROVE: Updated points triggers to utilize the new setings
1 parent 930bcc5 commit 08040f0

File tree

2 files changed

+376
-176
lines changed

2 files changed

+376
-176
lines changed

admin/clwc-earning-loyalty-points.php

+70-78
Original file line numberDiff line numberDiff line change
@@ -17,117 +17,109 @@
1717
}
1818

1919
/**
20-
* Add loyalty points on successful customer registration
20+
* Add loyalty points on successful customer registration.
2121
*
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
2525
* @return void
2626
*/
2727
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() ) {
3131
$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;
4936
$new_points = $old_points + clwc_earning_points_customer_registration();
50-
51-
// Update customer loyalty points.
37+
5238
update_user_meta( $user_id, 'clwc_loyalty_points', $new_points, $old_points );
5339

54-
// Define a descriptive details string for the log entry.
5540
$details = sprintf(
5641
esc_html__( 'Customer awarded %d loyalty points for registering an account.', 'customer-loyalty-for-woocommerce' ),
5742
clwc_earning_points_customer_registration()
5843
);
5944

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.' );
6249
}
63-
6450
}
6551
add_action( 'user_register', 'clwc_customer_registration', 10, 1 );
6652

6753
/**
68-
* Add loyalty points on order completion
54+
* Add loyalty points on order completion.
6955
*
70-
* @since 1.0.0
56+
* @param int $order_id WooCommerce order ID.
57+
*
58+
* @since 1.0.0
7159
* @return void
7260
*/
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();
8365

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;
8670

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 : '';
9076

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+
}
9184
}
9285
add_action( 'woocommerce_thankyou', 'clwc_customer_first_order', 10 );
9386

9487
/**
95-
* Add loyalty points for every dollar spent
88+
* Add loyalty points for every dollar spent.
9689
*
97-
* @since 1.0.0
90+
* @param int $order_id WooCommerce order ID.
91+
*
92+
* @since 1.0.0
9893
* @return void
9994
*/
10095
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 );
112122
}
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 );
130123
}
131-
132124
}
133125
add_action( 'woocommerce_thankyou', 'clwc_customer_money_spent', 10 );

0 commit comments

Comments
 (0)