|
17 | 17 | }
|
18 | 18 |
|
19 | 19 | /**
|
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. |
21 | 26 | *
|
22 | 27 | * @since 1.0.0
|
23 |
| - * @return string |
| 28 | + * @return void |
24 | 29 | */
|
25 | 30 | function clwc_payment_complete( $order_id ) {
|
26 |
| - // Get order details. |
27 | 31 | $order = wc_get_order( $order_id );
|
28 |
| - |
29 |
| - // Get user from order. |
30 | 32 | $user = $order->get_user();
|
31 | 33 |
|
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; |
35 | 39 | }
|
36 | 40 |
|
37 |
| - // Run code if user is attached to the order. |
38 | 41 | 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'; |
39 | 45 |
|
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; |
59 | 48 |
|
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(); |
62 | 51 |
|
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 = [ |
72 | 54 | 'post_title' => $coupon_code,
|
73 | 55 | 'post_content' => '',
|
74 | 56 | 'post_status' => 'publish',
|
75 | 57 | 'post_author' => 1,
|
76 | 58 | 'post_type' => 'shop_coupon'
|
77 |
| - ); |
78 |
| - |
79 |
| - // Filter the args. |
80 |
| - $coupon = apply_filters( 'clwc_create_coupon_args', $coupon ); |
| 59 | + ]; |
81 | 60 |
|
82 |
| - // Get newly create coupon's ID # |
83 | 61 | $new_coupon_id = wp_insert_post( $coupon );
|
84 |
| - |
85 |
| - // Add custom meta data to the newly created coupon. |
86 | 62 | 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 ); |
91 | 64 | 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. |
97 | 65 | update_post_meta( $order_id, 'clwc_customer_coupon_code', $coupon_code );
|
98 | 66 |
|
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 ); |
107 | 70 | } else {
|
108 |
| - // Add 1 to punches count. |
109 |
| - $card_punches = $card_punches + 1; |
| 71 | + $card_punches++; |
110 | 72 | }
|
111 | 73 |
|
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 ); |
114 | 75 | }
|
115 | 76 | }
|
116 | 77 | add_action( 'woocommerce_thankyou', 'clwc_payment_complete', 10 );
|
117 | 78 |
|
118 | 79 | /**
|
119 |
| - * Display Coupon Code for 10 punches to the Punch Card. |
| 80 | + * Display Coupon Code for X punches to the Punch Card. |
120 | 81 | *
|
121 | 82 | * @param object $order
|
122 | 83 | *
|
123 |
| - * @return string |
| 84 | + * @return void |
124 | 85 | */
|
125 | 86 | function clwc_order_customer_coupon_code( $order ) {
|
126 | 87 | global $woocommerce, $post;
|
|
0 commit comments