Skip to content

Commit 102f8b6

Browse files
committed
👌 IMPROVE: Updated logging functionality
1 parent 4ba2d65 commit 102f8b6

5 files changed

+122
-116
lines changed

admin/class-clwc-admin.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,13 @@ public function __construct( $plugin_name, $version ) {
6565
* @since 1.0.0
6666
* @return void
6767
*/
68-
public function enqueue_styles() {
69-
//wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/clwc-admin.css', array(), $this->version, 'all' );
68+
public function enqueue_styles( $hook_suffix ) {
69+
// Only enqueue on your specific admin page.
70+
if ( 'woocommerce_page_clwc-customer-loyalty' !== $hook_suffix ) {
71+
return;
72+
}
73+
74+
wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/clwc-admin.css', [], $this->version, 'all' );
7075
}
7176

7277
/**
@@ -97,7 +102,6 @@ public function enqueue_scripts( $hook_suffix ) {
97102
] );
98103
}
99104

100-
101105
}
102106

103107
/**

admin/clwc-earning-loyalty-points.php

+35-26
Original file line numberDiff line numberDiff line change
@@ -19,54 +19,58 @@
1919
/**
2020
* Add loyalty points on successful customer registration.
2121
*
22-
* @param int $user_id The ID of the registered user.
22+
* Awards loyalty points to the user upon registration if the loyalty points
23+
* system is activated in the plugin settings.
2324
*
2425
* @since 1.0.0
26+
* @param int $user_id The ID of the registered user.
2527
* @return void
2628
*/
2729
function clwc_customer_registration( $user_id ) {
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() ) {
30+
$settings = get_option( 'clwc_loyalty_points_settings', [] );
31+
$registration_points = (int) ($settings['customer_registration'] ?? 0);
32+
33+
if ( clwc_loyalty_points_activate() && $registration_points ) {
3134
$user_info = get_userdata( $user_id );
3235
$user_email = $user_info ? $user_info->user_email : '';
3336
$user_name = $user_info ? $user_info->display_name : '';
34-
37+
3538
$old_points = (int) get_user_meta( $user_id, 'clwc_loyalty_points', true ) ?: 0;
36-
$new_points = $old_points + clwc_earning_points_customer_registration();
37-
39+
$new_points = $old_points + $registration_points;
40+
3841
update_user_meta( $user_id, 'clwc_loyalty_points', $new_points, $old_points );
3942

4043
$details = sprintf(
4144
esc_html__( 'Customer awarded %d loyalty points for registering an account.', 'customer-loyalty-for-woocommerce' ),
42-
clwc_earning_points_customer_registration()
45+
$registration_points
4346
);
4447

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.' );
48+
clwc_insert_loyalty_log_entry( $user_id, $user_name, $user_email, $new_points, $details );
4949
}
5050
}
5151
add_action( 'user_register', 'clwc_customer_registration', 10, 1 );
5252

5353
/**
5454
* Add loyalty points on order completion.
5555
*
56-
* @param int $order_id WooCommerce order ID.
56+
* Awards loyalty points to the user when an order is completed if the loyalty points
57+
* system is activated in the plugin settings.
5758
*
5859
* @since 1.0.0
60+
* @param int $order_id WooCommerce order ID.
5961
* @return void
6062
*/
6163
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+
$settings = get_option( 'clwc_loyalty_points_settings', [] );
65+
$order_complete_points = (int) ($settings['order_complete'] ?? 0);
66+
67+
if ( clwc_loyalty_points_activate() && $order_complete_points ) {
68+
$order = wc_get_order( $order_id );
6469
$user_id = $order->get_user_id();
6570

6671
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;
72+
$old_points = (int) get_user_meta( $user_id, 'clwc_loyalty_points', true ) ?: 0;
73+
$new_points = $old_points + $order_complete_points;
7074

7175
update_user_meta( $user_id, 'clwc_loyalty_points', $new_points, $old_points );
7276

@@ -76,9 +80,9 @@ function clwc_customer_first_order( $order_id ) {
7680

7781
$details = sprintf(
7882
esc_html__( 'Customer awarded %d loyalty points for completing an order.', 'customer-loyalty-for-woocommerce' ),
79-
$earned_points
83+
$order_complete_points
8084
);
81-
clwc_insert_loyalty_log_entry( $user_id, $user_name, $user_email, $earned_points, $details );
85+
clwc_insert_loyalty_log_entry( $user_id, $user_name, $user_email, $order_complete_points, $details );
8286
}
8387
}
8488
}
@@ -87,13 +91,19 @@ function clwc_customer_first_order( $order_id ) {
8791
/**
8892
* Add loyalty points for every dollar spent.
8993
*
90-
* @param int $order_id WooCommerce order ID.
94+
* Awards loyalty points to the user based on the total amount spent, if the loyalty points
95+
* system is activated and set up in the plugin settings.
9196
*
97+
* @param int $order_id WooCommerce order ID.
98+
*
9299
* @since 1.0.0
93100
* @return void
94101
*/
95102
function clwc_customer_money_spent( $order_id ) {
96-
if ( clwc_loyalty_points_activate() && 0 != clwc_earning_points_money_spent() ) {
103+
$settings = get_option( 'clwc_loyalty_points_settings', [] );
104+
$points_per_dollar = (int) ($settings['money_spent'] ?? 0);
105+
106+
if ( clwc_loyalty_points_activate() && $points_per_dollar ) {
97107
$order = wc_get_order( $order_id );
98108
$user_id = $order->get_user_id();
99109

@@ -102,10 +112,9 @@ function clwc_customer_money_spent( $order_id ) {
102112
? $order->get_subtotal()
103113
: $order->get_total();
104114

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;
115+
$old_points = (int) get_user_meta( $user_id, 'clwc_loyalty_points', true ) ?: 0;
116+
$earned_points = round( $order_total * $points_per_dollar );
117+
$new_points = $old_points + $earned_points;
109118

110119
update_user_meta( $user_id, 'clwc_loyalty_points', $new_points, $old_points );
111120

admin/clwc-woocommerce-settings.php

+30-69
Original file line numberDiff line numberDiff line change
@@ -17,110 +17,71 @@
1717
}
1818

1919
/**
20-
* Run specific tasks on payment complete in WooCommerce
20+
* Run specific tasks on payment completion in WooCommerce.
21+
*
22+
* This function checks if the rewards card is active and, if so, increments
23+
* the user's punches or issues a coupon if required punches are met.
24+
*
25+
* @param int $order_id WooCommerce order ID.
2126
*
2227
* @since 1.0.0
23-
* @return string
28+
* @return void
2429
*/
2530
function clwc_payment_complete( $order_id ) {
26-
// Get order details.
2731
$order = wc_get_order( $order_id );
28-
29-
// Get user from order.
3032
$user = $order->get_user();
3133

32-
// Bail if the rewards card is not activated.
33-
if ( 'on' !== clwc_rewards_card_activate() ) {
34-
return false;
34+
// Retrieve rewards card settings.
35+
$settings = get_option( 'clwc_rewards_card_settings', [] );
36+
37+
if ( '1' !== $settings['activate_rewards_card'] ) {
38+
return;
3539
}
3640

37-
// Run code if user is attached to the order.
3841
if ( $user ) {
42+
$required_punches = (int) ($settings['required_punches'] ?? 10);
43+
$coupon_amount = (float) ($settings['coupon_amount'] ?? 5.00);
44+
$discount_type = $settings['coupon_type'] ?? 'fixed_cart';
3945

40-
// Card punches.
41-
$card_punches = get_user_meta( $user->ID, 'clwc_rewards_card_punches', true );
42-
$old_punches = get_user_meta( $user->ID, 'clwc_rewards_card_punches', true );
43-
44-
// Set a default of zero.
45-
if ( '' == $card_punches ) {
46-
$card_punches = 0;
47-
}
48-
49-
// Rewards earned.
50-
$rewards_earned = get_user_meta( $user->ID, 'clwc_rewards_earned', true );
51-
$old_rewards = get_user_meta( $user->ID, 'clwc_rewards_earned', true );
52-
53-
// Set a default of zero.
54-
if ( '' == $rewards_earned ) {
55-
$rewards_earned = 0;
56-
}
57-
58-
$card_check = clwc_rewards_card_required_punches() - 1;
46+
$card_punches = (int) get_user_meta( $user->ID, 'clwc_rewards_card_punches', true ) ?: 0;
47+
$rewards_earned = (int) get_user_meta( $user->ID, 'clwc_rewards_earned', true ) ?: 0;
5948

60-
// Check if user needs new coupon.
61-
if ( $card_check == $card_punches ) {
49+
if ( $card_punches >= ($required_punches - 1) ) {
50+
$coupon_code = clwc_get_random_string();
6251

63-
/**
64-
* Create a coupon programatically
65-
*/
66-
$coupon_code = clwc_get_random_string(); // Code.
67-
$amount = clwc_rewards_card_coupon_amount(); // Amount.
68-
$discount_type = clwc_rewards_card_coupon_type(); // Type: fixed_cart, percent, fixed_product, percent_product.
69-
70-
// Coupon args.
71-
$coupon = array(
52+
// Create coupon.
53+
$coupon = [
7254
'post_title' => $coupon_code,
7355
'post_content' => '',
7456
'post_status' => 'publish',
7557
'post_author' => 1,
7658
'post_type' => 'shop_coupon'
77-
);
78-
79-
// Filter the args.
80-
$coupon = apply_filters( 'clwc_create_coupon_args', $coupon );
59+
];
8160

82-
// Get newly create coupon's ID #
8361
$new_coupon_id = wp_insert_post( $coupon );
84-
85-
// Add custom meta data to the newly created coupon.
8662
update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
87-
update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
88-
update_post_meta( $new_coupon_id, 'individual_use', 'yes' );
89-
update_post_meta( $new_coupon_id, 'product_ids', '' );
90-
update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
63+
update_post_meta( $new_coupon_id, 'coupon_amount', $coupon_amount );
9164
update_post_meta( $new_coupon_id, 'usage_limit', '1' );
92-
update_post_meta( $new_coupon_id, 'expiry_date', '' );
93-
update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
94-
update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
95-
96-
// Add coupon code to order meta data.
9765
update_post_meta( $order_id, 'clwc_customer_coupon_code', $coupon_code );
9866

99-
// Reset punches.
100-
$card_punches = 0;
101-
102-
// Add 1 to rewards earned.
103-
$rewards_earned = $rewards_earned + 1;
104-
105-
// Update user meta - rewards earned number.
106-
update_user_meta( $user->ID, 'clwc_rewards_earned', $rewards_earned, $old_rewards );
67+
$card_punches = 0; // Reset punches after coupon issue.
68+
$rewards_earned++;
69+
update_user_meta( $user->ID, 'clwc_rewards_earned', $rewards_earned );
10770
} else {
108-
// Add 1 to punches count.
109-
$card_punches = $card_punches + 1;
71+
$card_punches++;
11072
}
11173

112-
// Update user meta - punch card number.
113-
update_user_meta( $user->ID, 'clwc_rewards_card_punches', $card_punches, $old_punches );
74+
update_user_meta( $user->ID, 'clwc_rewards_card_punches', $card_punches );
11475
}
11576
}
11677
add_action( 'woocommerce_thankyou', 'clwc_payment_complete', 10 );
11778

11879
/**
119-
* Display Coupon Code for 10 punches to the Punch Card.
80+
* Display Coupon Code for X punches to the Punch Card.
12081
*
12182
* @param object $order
12283
*
123-
* @return string
84+
* @return void
12485
*/
12586
function clwc_order_customer_coupon_code( $order ) {
12687
global $woocommerce, $post;

admin/js/clwc-admin.js

+23-13
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
jQuery(document).ready(function($) {
2-
console.log("Loyalty Points script loaded."); // Debugging line
32
$('.clwc-loyalty-points').on('change', function() {
43
var $input = $(this);
54
var userID = $input.data('user-id');
65
var points = $input.val();
76

8-
console.log("User ID:", userID, "Points:", points); // Debugging line
9-
10-
// Disable the input while processing
7+
// Disable the input while processing.
118
$input.prop('disabled', true);
129

1310
$.ajax({
@@ -20,9 +17,8 @@ jQuery(document).ready(function($) {
2017
security: clwc_ajax.nonce,
2118
},
2219
success: function(response) {
23-
console.log(response); // Debugging line
2420
if (response.success) {
25-
// Optionally, provide visual feedback
21+
// Optionally, provide visual feedback.
2622
$input.css('border-color', 'green');
2723
} else {
2824
$input.css('border-color', 'red');
@@ -34,41 +30,55 @@ jQuery(document).ready(function($) {
3430
alert('An AJAX error occurred: ' + textStatus);
3531
},
3632
complete: function() {
37-
// Re-enable the input
33+
// Re-enable the input.
3834
$input.prop('disabled', false);
3935
}
4036
});
4137
});
4238

4339
var file_frame;
4440

41+
// Handle the Upload Image button click.
4542
$(document).on('click', '.clwc-upload-image-button', function(e) {
4643
e.preventDefault();
4744

48-
// If the media frame already exists, reopen it
45+
var $button = $(this);
46+
47+
// If the media frame already exists, reopen it.
4948
if (file_frame) {
5049
file_frame.open();
5150
return;
5251
}
5352

54-
// Create a new media frame
53+
// Create a new media frame.
5554
file_frame = wp.media.frames.file_frame = wp.media({
5655
title: 'Select or Upload an Image',
5756
button: {
5857
text: 'Use this image',
5958
},
60-
multiple: false // Set to true if you want multiple files
59+
multiple: false
6160
});
6261

6362
// When an image is selected, run a callback
6463
file_frame.on('select', function() {
6564
var attachment = file_frame.state().get('selection').first().toJSON();
66-
$('.clwc-upload-image-button').prev('input[type="hidden"]').val(attachment.id);
67-
$('.clwc-upload-image-button').prev().prev('img').attr('src', attachment.url).show();
65+
$button.siblings('.clwc-image-id').val(attachment.id);
66+
$button.siblings('.clwc-image-preview').attr('src', attachment.url).show();
67+
$button.siblings('.clwc-remove-image-button').show();
6868
});
6969

70-
// Finally, open the modal
70+
// Open the modal.
7171
file_frame.open();
7272
});
7373

74+
// Handle the Remove Image button click.
75+
$(document).on('click', '.clwc-remove-image-button', function(e) {
76+
e.preventDefault();
77+
var $button = $(this);
78+
79+
// Clear the hidden field and hide the preview image.
80+
$button.siblings('.clwc-image-id').val('');
81+
$button.siblings('.clwc-image-preview').attr('src', '').hide();
82+
$button.hide();
83+
});
7484
});

0 commit comments

Comments
 (0)