|
4 | 4 | *
|
5 | 5 | * @package CLWC
|
6 | 6 | * @subpackage CLWC/admin
|
7 |
| - * @author Devio Digital <[email protected]> |
8 |
| - * @license GPL-2.0+ http://www.gnu.org/licenses/gpl-2.0.txt |
9 |
| - * @link https://www.deviodigital.com |
10 | 7 | * @since 1.0.0
|
11 | 8 | */
|
12 | 9 | function clwc_dashboard_shortcode() {
|
13 |
| - |
14 |
| - global $woocommerce; |
15 |
| - |
16 |
| - // Set empty vars. |
17 |
| - $coupon_codes = ''; |
18 |
| - //$clwc_order_ids = ''; |
19 |
| - |
20 |
| - // Check if user is logged in. |
21 | 10 | if ( is_user_logged_in() ) {
|
22 |
| - // Get the user ID. |
23 | 11 | $user_id = get_current_user_id();
|
24 | 12 |
|
25 |
| - // Get the user object. |
26 |
| - //$user_meta = get_userdata( $user_id ); |
27 |
| - |
28 |
| - // Get loyalty points. |
29 |
| - $loyalty_points = get_user_meta( $user_id, 'clwc_loyalty_points', true ); |
30 |
| - |
31 |
| - // Set to zero if customer has no points. |
32 |
| - if ( ! $loyalty_points ) { |
33 |
| - $loyalty_points = 0; |
34 |
| - } |
35 |
| - |
36 |
| - // Redeemable points minimum. |
| 13 | + // Get user loyalty points. |
| 14 | + $loyalty_points = get_user_meta( $user_id, 'clwc_loyalty_points', true ) ?: 0; |
37 | 15 | $redeem_points_min = clwc_loyalty_points_redeem_points_minimum();
|
38 |
| - // Set empty variable. |
39 |
| - $redeem_points = ''; |
40 | 16 |
|
41 |
| - // Set redeem points variable if availabe. |
| 17 | + echo '<h4 class="clwc-loyalty-points">' . esc_attr__( 'My Loyalty Points', 'customer-loyalty-for-woocommerce' ) . '</h4>'; |
| 18 | + echo '<table class="clwc-dashboard"><tbody>'; |
| 19 | + echo '<tr><td><strong>' . esc_attr__('Loyalty Points', 'customer-loyalty-for-woocommerce') . '</strong></td><td class="clwc-loyalty-points-total">' . esc_attr( $loyalty_points ) . '</td></tr>'; |
42 | 20 | if ( $redeem_points_min && $loyalty_points >= $redeem_points_min ) {
|
43 |
| - // Create new coupon when user redeem's loyalty points. |
44 |
| - if ( isset( $_POST ) && null !== filter_input( INPUT_POST, 'clwc_redeem_points' ) ) { |
45 |
| - $coupon_code = clwc_get_random_string(); // Code. |
46 |
| - $amount = clwc_loyalty_points_redeem_points_value(); // Amount. |
47 |
| - |
48 |
| - // Coupon args. |
49 |
| - $coupon_args = array( |
50 |
| - 'post_title' => $coupon_code, |
51 |
| - 'post_content' => '', |
52 |
| - 'post_status' => 'publish', |
53 |
| - 'post_author' => $user_id, |
54 |
| - 'post_type' => 'shop_coupon' |
55 |
| - ); |
56 |
| - |
57 |
| - // Filter args. |
58 |
| - $coupon_args = apply_filters( 'clwc_redeem_points_coupon_args', $coupon_args, $coupon_code, $user_id ); |
59 |
| - |
60 |
| - // Get newly create coupon's ID # |
61 |
| - $new_coupon_id = wp_insert_post( $coupon_args ); |
62 |
| - |
63 |
| - // Add custom meta data to the newly created coupon. |
64 |
| - update_post_meta( $new_coupon_id, 'discount_type', 'fixed_cart' ); |
65 |
| - update_post_meta( $new_coupon_id, 'coupon_amount', $amount ); |
66 |
| - update_post_meta( $new_coupon_id, 'individual_use', 'yes' ); |
67 |
| - update_post_meta( $new_coupon_id, 'product_ids', '' ); |
68 |
| - update_post_meta( $new_coupon_id, 'exclude_product_ids', '' ); |
69 |
| - update_post_meta( $new_coupon_id, 'usage_limit', '1' ); |
70 |
| - update_post_meta( $new_coupon_id, 'expiry_date', '' ); |
71 |
| - update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' ); |
72 |
| - update_post_meta( $new_coupon_id, 'free_shipping', 'no' ); |
73 |
| - |
74 |
| - // Reduce required points from user's loyalty points. |
75 |
| - $new_loyalty_points = $loyalty_points - $redeem_points_min; |
76 |
| - |
77 |
| - // Update user meta with the updated loyalty points amount. |
78 |
| - update_user_meta( $user_id, 'clwc_loyalty_points', $new_loyalty_points, $loyalty_points ); |
79 |
| - |
80 |
| - // Apply new coupon to the cart automatically. |
81 |
| - if ( ! $woocommerce->cart->add_discount( sanitize_text_field( $coupon_code ) ) ) { |
82 |
| - wc_print_notices(); |
83 |
| - } |
84 |
| - |
85 |
| - // Redirect to cart when discount applied. |
86 |
| - wp_safe_redirect( apply_filters( 'clwc_redeem_points_redirect_url', wc_get_cart_url() ) ); |
87 |
| - //exit; |
88 |
| - } |
89 |
| - |
90 |
| - // Redeem loyalty points. |
91 |
| - $redeem_button = '<form class="clwc-redeem-points" name="clwc_redeem_loyalty_points" method="post"> |
92 |
| - <input type="submit" class="button clwc-button" name="clwc_redeem_points" value="' . esc_attr__( 'Redeem', 'customer-loyalty-for-woocommerce' ) . '" />' |
93 |
| - . wp_nonce_field( 'clwc-redeem-points' ) . |
94 |
| - '</form>'; |
95 |
| - |
96 |
| - // Redeem loyalty points. |
97 |
| - $redeem_points = '<tr><td><strong>' . esc_attr__( 'Redeem Points', 'customer-loyalty-for-woocommerce' ) . '</strong></td><td>' . $redeem_button . '</td></tr>'; |
98 |
| - } |
99 |
| - |
100 |
| - // Coupons args. |
101 |
| - $args = array( |
102 |
| - 'posts_per_page' => -1, |
103 |
| - 'orderby' => 'title', |
104 |
| - 'order' => 'asc', |
105 |
| - 'post_type' => 'shop_coupon', |
106 |
| - 'post_status' => 'publish', |
107 |
| - ); |
108 |
| - |
109 |
| - // Filter the coupons args. |
110 |
| - $args = apply_filters( 'clwc_customer_coupons_args', $args ); |
111 |
| - |
112 |
| - // Get all coupons. |
113 |
| - $customer_coupons = get_posts( $args ); |
114 |
| - |
115 |
| - // Loop through coupons. |
116 |
| - foreach ( $customer_coupons as $customer_coupon ) { |
117 |
| - if ( $user_id == $customer_coupon->post_author ) { |
118 |
| - |
119 |
| - // Get coupon object. |
120 |
| - $coupon = new WC_Coupon( $customer_coupon->post_name ); |
121 |
| - |
122 |
| - // Get coupon data. |
123 |
| - $coupon_data = array( |
124 |
| - 'id' => $customer_coupon->ID, |
125 |
| - 'usage_limit' => ( ! empty( $customer_coupon->usage_limit ) ) ? $customer_coupon->usage_limit : null, |
126 |
| - 'usage_count' => (int) $customer_coupon->usage_count, |
127 |
| - 'amount' => wc_format_decimal( $coupon->get_amount(), 2 ), |
128 |
| - ); |
129 |
| - |
130 |
| - // How many uses are left for this coupon? |
131 |
| - $usage_left = $coupon_data['usage_limit'] - $coupon_data['usage_count']; |
132 |
| - |
133 |
| - // Set is_coupon_active var. |
134 |
| - if ( $usage_left > 0 ) { |
135 |
| - $is_coupon_active = '<span class="clwc-available-coupon">' . esc_attr__( 'Available', 'customer-loyalty-for-woocommerce' ) . '</span>'; |
136 |
| - $coupon_class = ''; |
137 |
| - } else { |
138 |
| - $is_coupon_active = ''; |
139 |
| - $coupon_class = ' class="clwc-inactive-coupon" '; |
140 |
| - } |
141 |
| - |
142 |
| - $coupon_codes .= '<tr><td ' . $coupon_class . '><strong>' . $customer_coupon->post_title . '</strong> - ' . wc_price( $coupon_data['amount'] ) . '</td><td>' . $is_coupon_active . '</td></tr>'; |
143 |
| - } |
144 |
| - } |
145 |
| - |
146 |
| - // Display lotalty points if activated in the admin settings. |
147 |
| - if ( 'on' == clwc_loyalty_points_activate() ) { |
148 |
| - // Table loyalty points. |
149 |
| - echo '<h4 class="clwc-loyalty-points">' . esc_attr__( 'My Loyalty Points', 'customer-loyalty-for-woocommerce' ) . '</h4>'; |
150 |
| - |
151 |
| - do_action( 'clwc_customer_dashboard_loyalty_points_table_before' ); |
152 |
| - |
153 |
| - echo '<table class="clwc-dashboard">'; |
154 |
| - echo '<tbody>'; |
155 |
| - |
156 |
| - do_action( 'clwc_customer_dashboard_loyalty_points_table_tbody_top' ); |
157 |
| - |
158 |
| - echo '<tr><td><strong>' . esc_attr__( 'Loyalty Points', 'customer-loyalty-for-woocommerce' ) . '</strong></td><td>' . esc_attr( $loyalty_points ) . '</td></tr>'; |
159 |
| - echo $redeem_points; |
160 |
| - |
161 |
| - do_action( 'clwc_customer_dashboard_loyalty_points_table_tbody_bottom' ); |
162 |
| - |
163 |
| - echo '</tbody>'; |
164 |
| - echo '</table>'; |
165 |
| - |
166 |
| - do_action( 'clwc_customer_dashboard_loyalty_points_table_after' ); |
167 |
| - |
168 |
| - } |
169 |
| - |
170 |
| - // Get all customer orders. |
171 |
| - $customer_orders = get_posts( array( |
172 |
| - 'numberposts' => -1, |
173 |
| - 'meta_key' => '_customer_user', |
174 |
| - 'meta_value' => $user_id, |
175 |
| - 'post_type' => wc_get_order_types(), |
176 |
| - 'post_status' => array_keys( wc_get_order_statuses() ), |
177 |
| - ) ); |
178 |
| - |
179 |
| - /** |
180 |
| - * Add coupon codes to CLWC Dashboard |
181 |
| - * |
182 |
| - * Checks to see if there's coupons created and added to a customer's order, then it |
183 |
| - * also checks to see if the coupon is active or not, and displays these details to the |
184 |
| - * customer in the Rewards Card table. |
185 |
| - */ |
186 |
| - foreach ( $customer_orders as $clwc_order ) { |
187 |
| - // Get CLWC customer coupon code - if any. |
188 |
| - $coupon_added = get_post_meta( $clwc_order->ID, 'clwc_customer_coupon_code', TRUE ); |
189 |
| - |
190 |
| - // Add coupon code to output if it's active. |
191 |
| - if ( $coupon_added ) { |
192 |
| - |
193 |
| - // Get coupon data. |
194 |
| - $coupon = new WC_Coupon( $coupon_added ); |
195 |
| - //$coupon_post = get_post( $coupon->get_id() ); |
196 |
| - $coupon_data = array( |
197 |
| - 'id' => $coupon->get_id(), |
198 |
| - 'usage_limit' => ( ! empty( $coupon->get_usage_limit() ) ) ? $coupon->get_usage_limit() : null, |
199 |
| - 'usage_count' => (int) $coupon->get_usage_count(), |
200 |
| - 'amount' => wc_format_decimal( $coupon->get_amount(), 2 ), |
201 |
| - ); |
202 |
| - |
203 |
| - // How many uses are left for this coupon? |
204 |
| - $usage_left = $coupon_data['usage_limit'] - $coupon_data['usage_count']; |
205 |
| - |
206 |
| - // Set is_coupon_active var. |
207 |
| - if ( $usage_left > 0 ) { |
208 |
| - $is_coupon_active = '<span class="clwc-available-coupon">' . esc_attr__( 'Available', 'customer-loyalty-for-woocommerce' ) . '</span>'; |
209 |
| - $coupon_class = ''; |
210 |
| - } else { |
211 |
| - $is_coupon_active = ''; |
212 |
| - $coupon_class = ' class="clwc-inactive-coupon" '; |
213 |
| - } |
214 |
| - |
215 |
| - $coupon_codes .= '<tr><td ' . $coupon_class . '><strong>' . $coupon_added . '</strong> - ' . wc_price( $coupon_data['amount'] ) . '</td><td>' . $is_coupon_active . '</td></tr>'; |
216 |
| - } |
217 |
| - } |
218 |
| - |
219 |
| - // Set message when no coupons are available. |
220 |
| - if ( '' == $coupon_codes ) { |
221 |
| - $coupon_codes .= '<tr><td class="clwc-no-coupons">' . apply_filters( 'clwc_no_coupons_message', esc_attr__( 'You do not have any coupons available', 'customer-loyalty-for-woocommerce' ) ) . '</td></tr>'; |
222 |
| - } |
223 |
| - |
224 |
| - // Get rewards card punches. |
225 |
| - $rewards_card_punches = get_user_meta( $user_id, 'clwc_rewards_card_punches', TRUE ); |
226 |
| - |
227 |
| - // Set to zero if customer has no punches. |
228 |
| - if ( ! $rewards_card_punches ) { |
229 |
| - $rewards_card_punches = 0; |
230 |
| - } |
231 |
| - |
232 |
| - // Get rewards earned. |
233 |
| - $rewards_earned = get_user_meta( $user_id, 'clwc_rewards_earned', TRUE ); |
234 |
| - |
235 |
| - // Set to zero if customer has no earned rewards. |
236 |
| - if ( ! $rewards_earned ) { |
237 |
| - $rewards_earned = 0; |
238 |
| - } |
239 |
| - |
240 |
| - // Display rewards card if it's activated in admin settings. |
241 |
| - if ( 'on' == clwc_rewards_card_activate() ) { |
242 |
| - // Table rewards card. |
243 |
| - echo '<h4 class="clwc-rewards-card">' . esc_attr__( 'My Rewards Card', 'customer-loyalty-for-woocommerce' ) . '</h4>'; |
244 |
| - |
245 |
| - do_action( 'clwc_customer_dashboard_rewards_card_table_before' ); |
246 |
| - |
247 |
| - echo '<table class="clwc-dashboard rewards-card">'; |
248 |
| - echo '<tbody>'; |
249 |
| - |
250 |
| - do_action( 'clwc_customer_dashboard_rewards_card_table_tbody_top' ); |
251 |
| - |
252 |
| - echo '<tr><td><strong>' . esc_attr__( 'Rewards Card Punches', 'customer-loyalty-for-woocommerce' ) . '</strong></td><td>' . esc_attr( $rewards_card_punches ) . '</td></tr>'; |
253 |
| - echo '<tr><td><strong>' . esc_attr__( 'Rewards Earned', 'customer-loyalty-for-woocommerce' ) . '</strong></td><td>' . esc_attr( $rewards_earned ) . '</td></tr>'; |
254 |
| - |
255 |
| - do_action( 'clwc_customer_dashboard_rewards_card_table_tbody_bottom' ); |
256 |
| - |
257 |
| - echo '</tbody>'; |
258 |
| - echo '</table>'; |
259 |
| - |
260 |
| - do_action( 'clwc_customer_dashboard_rewards_card_table_after' ); |
261 |
| - |
| 21 | + echo '<tr><td><strong>' . esc_attr__( 'Redeem Points', 'customer-loyalty-for-woocommerce' ) . '</strong></td><td>'; |
| 22 | + echo '<button id="clwc-redeem-points" class="button clwc-button">' . esc_attr__( 'Redeem', 'customer-loyalty-for-woocommerce' ) . '</button>'; |
| 23 | + echo '</td></tr>'; |
262 | 24 | }
|
| 25 | + echo '</tbody></table>'; |
263 | 26 |
|
264 |
| - // Display coupons if rewards card or loyalty points are active. |
265 |
| - if ( 'on' == clwc_rewards_card_activate() || 'on' == clwc_loyalty_points_activate() ) { |
266 |
| - // My coupons. |
267 |
| - echo '<h4 class="clwc-rewards-coupons">' . esc_attr__( 'My Coupons', 'customer-loyalty-for-woocommerce' ) . '</h4>'; |
268 |
| - |
269 |
| - do_action( 'clwc_customer_dashboard_coupons_table_before' ); |
270 |
| - |
271 |
| - echo '<table class="clwc-dashboard rewards-coupons">'; |
272 |
| - echo '<tbody>'; |
273 |
| - |
274 |
| - do_action( 'clwc_customer_dashboard_coupons_table_tbody_top' ); |
275 |
| - |
276 |
| - echo $coupon_codes; |
277 |
| - |
278 |
| - do_action( 'clwc_customer_dashboard_coupons_table_tbody_bottom' ); |
279 |
| - |
280 |
| - echo '</tbody>'; |
281 |
| - echo '</table>'; |
282 |
| - |
283 |
| - do_action( 'clwc_customer_dashboard_coupons_table_after' ); |
| 27 | + // Display existing coupons |
| 28 | + echo '<h4 class="clwc-rewards-coupons">' . esc_attr__( 'My Coupons', 'customer-loyalty-for-woocommerce' ) . '</h4>'; |
| 29 | + echo '<table class="clwc-dashboard rewards-coupons" id="clwc-coupons-table"><tbody>'; |
| 30 | + echo clwc_get_user_coupons_html( $user_id ); |
| 31 | + echo '</tbody></table>'; |
| 32 | + } else { |
| 33 | + echo wp_login_form(); |
| 34 | + } |
| 35 | +} |
| 36 | +add_shortcode( 'clwc_dashboard', 'clwc_dashboard_shortcode' ); |
284 | 37 |
|
285 |
| - } |
| 38 | +/** |
| 39 | + * Helper function to retrieve and render user coupons as HTML rows. |
| 40 | + * |
| 41 | + * @param int $user_id User ID. |
| 42 | + * @return string HTML of coupon rows. |
| 43 | + */ |
| 44 | +function clwc_get_user_coupons_html( $user_id ) { |
| 45 | + $args = [ |
| 46 | + 'posts_per_page' => -1, |
| 47 | + 'orderby' => 'date', |
| 48 | + 'order' => 'DESC', |
| 49 | + 'post_type' => 'shop_coupon', |
| 50 | + 'post_status' => 'publish', |
| 51 | + 'author' => $user_id, |
| 52 | + ]; |
| 53 | + |
| 54 | + $customer_coupons = get_posts( $args ); |
| 55 | + $output = ''; |
| 56 | + |
| 57 | + foreach ( $customer_coupons as $customer_coupon ) { |
| 58 | + $coupon = new WC_Coupon( $customer_coupon->post_name ); |
| 59 | + $amount = wc_price( $coupon->get_amount() ); |
| 60 | + $usage_left = $coupon->get_usage_limit() - $coupon->get_usage_count(); |
| 61 | + $is_coupon_active = ( $usage_left > 0 ) ? '<span class="clwc-available-coupon">Available</span>' : ''; |
| 62 | + $output .= '<tr><td><strong>' . $customer_coupon->post_title . '</strong> - ' . $amount . '</td><td>' . $is_coupon_active . '</td></tr>'; |
| 63 | + } |
286 | 64 |
|
287 |
| - } else { |
288 |
| - // Display login form. |
289 |
| - apply_filters( 'clwc_customer_dashboard_login_form', wp_login_form() ); |
| 65 | + if ( empty( $output ) ) { |
| 66 | + $output = '<tr><td class="clwc-no-coupons">' . esc_attr__( 'You do not have any coupons available', 'customer-loyalty-for-woocommerce' ) . '</td></tr>'; |
290 | 67 | }
|
| 68 | + |
| 69 | + return $output; |
291 | 70 | }
|
292 | 71 | add_shortcode( 'clwc_dashboard', 'clwc_dashboard_shortcode' );
|
0 commit comments