1 <?php
  2 /**
  3  * WooCommerce Customer Functions
  4  *
  5  * Functions for customers.
  6  *
  7  * @author      WooThemes
  8  * @category    Core
  9  * @package     WooCommerce/Functions
 10  * @version     2.1.0
 11  */
 12 
 13 if ( ! defined( 'ABSPATH' ) ) {
 14     exit; // Exit if accessed directly
 15 }
 16 
 17 /**
 18  * Prevent any user who cannot 'edit_posts' (subscribers, customers etc) from seeing the admin bar
 19  *
 20  * Note: get_option( 'woocommerce_lock_down_admin', true ) is a deprecated option here for backwards compat. Defaults to true.
 21  *
 22  * @access public
 23  * @param bool $show_admin_bar
 24  * @return bool
 25  */
 26 function wc_disable_admin_bar( $show_admin_bar ) {
 27     if ( apply_filters( 'woocommerce_disable_admin_bar', get_option( 'woocommerce_lock_down_admin', 'yes' ) === 'yes' ) && ! ( current_user_can( 'edit_posts' ) || current_user_can( 'manage_woocommerce' ) ) ) {
 28         $show_admin_bar = false;
 29     }
 30 
 31     return $show_admin_bar;
 32 }
 33 add_filter( 'show_admin_bar', 'wc_disable_admin_bar', 10, 1 );
 34 
 35 
 36 /**
 37  * Create a new customer
 38  *
 39  * @param  string $email
 40  * @param  string $username
 41  * @param  string $password
 42  * @return int|WP_Error on failure, Int (user ID) on success
 43  */
 44 function wc_create_new_customer( $email, $username = '', $password = '' ) {
 45 
 46     // Check the e-mail address
 47     if ( empty( $email ) || ! is_email( $email ) ) {
 48         return new WP_Error( 'registration-error', __( 'Please provide a valid email address.', 'woocommerce' ) );
 49     }
 50 
 51     if ( email_exists( $email ) ) {
 52         return new WP_Error( 'registration-error', __( 'An account is already registered with your email address. Please login.', 'woocommerce' ) );
 53     }
 54 
 55     // Handle username creation
 56     if ( 'no' === get_option( 'woocommerce_registration_generate_username' ) || ! empty( $username ) ) {
 57 
 58         $username = sanitize_user( $username );
 59 
 60         if ( empty( $username ) || ! validate_username( $username ) ) {
 61             return new WP_Error( 'registration-error', __( 'Please enter a valid account username.', 'woocommerce' ) );
 62         }
 63 
 64         if ( username_exists( $username ) )
 65             return new WP_Error( 'registration-error', __( 'An account is already registered with that username. Please choose another.', 'woocommerce' ) );
 66     } else {
 67 
 68         $username = sanitize_user( current( explode( '@', $email ) ) );
 69 
 70         // Ensure username is unique
 71         $append     = 1;
 72         $o_username = $username;
 73 
 74         while ( username_exists( $username ) ) {
 75             $username = $o_username . $append;
 76             $append ++;
 77         }
 78     }
 79 
 80     // Handle password creation
 81     if ( 'yes' === get_option( 'woocommerce_registration_generate_password' ) && empty( $password ) ) {
 82         $password = wp_generate_password();
 83         $password_generated = true;
 84 
 85     } elseif ( empty( $password ) ) {
 86         return new WP_Error( 'registration-error', __( 'Please enter an account password.', 'woocommerce' ) );
 87 
 88     } else {
 89         $password_generated = false;
 90     }
 91 
 92     // WP Validation
 93     $validation_errors = new WP_Error();
 94 
 95     do_action( 'woocommerce_register_post', $username, $email, $validation_errors );
 96 
 97     $validation_errors = apply_filters( 'woocommerce_registration_errors', $validation_errors, $username, $email );
 98 
 99     if ( $validation_errors->get_error_code() )
100         return $validation_errors;
101 
102     $new_customer_data = apply_filters( 'woocommerce_new_customer_data', array(
103         'user_login' => $username,
104         'user_pass'  => $password,
105         'user_email' => $email,
106         'role'       => 'customer'
107     ) );
108 
109     $customer_id = wp_insert_user( $new_customer_data );
110 
111     if ( is_wp_error( $customer_id ) ) {
112         return new WP_Error( 'registration-error', '<strong>' . __( 'ERROR', 'woocommerce' ) . '</strong>: ' . __( 'Couldn&#8217;t register you&hellip; please contact us if you continue to have problems.', 'woocommerce' ) );
113     }
114 
115     do_action( 'woocommerce_created_customer', $customer_id, $new_customer_data, $password_generated );
116 
117     return $customer_id;
118 }
119 
120 /**
121  * Login a customer (set auth cookie and set global user object)
122  *
123  * @param  int $customer_id
124  * @return void
125  */
126 function wc_set_customer_auth_cookie( $customer_id ) {
127     global $current_user;
128 
129     $current_user = get_user_by( 'id', $customer_id );
130 
131     wp_set_auth_cookie( $customer_id, true );
132 }
133 
134 /**
135  * Get past orders (by email) and update them
136  *
137  * @param  int $customer_id
138  * @return int
139  */
140 function wc_update_new_customer_past_orders( $customer_id ) {
141 
142     $customer = get_user_by( 'id', absint( $customer_id ) );
143 
144     $customer_orders = get_posts( array(
145         'numberposts' => -1,
146         'post_type'   => 'shop_order',
147         'post_status' => 'publish',
148         'fields'      => 'ids',
149         'meta_query' => array(
150             array(
151                 'key'     => '_customer_user',
152                 'value'   => array( 0, '' ),
153                 'compare' => 'IN'
154             ),
155             array(
156                 'key'     => '_billing_email',
157                 'value'   => $customer->user_email,
158             )
159         ),
160     ) );
161 
162     $linked = 0;
163     $complete = 0;
164 
165     if ( $customer_orders )
166         foreach ( $customer_orders as $order_id ) {
167             update_post_meta( $order_id, '_customer_user', $customer->ID );
168 
169             $order_status = wp_get_post_terms( $order_id, 'shop_order_status' );
170 
171             if ( $order_status ) {
172                 $order_status = current( $order_status );
173                 $order_status = sanitize_title( $order_status->slug );
174             }
175 
176             if ( $order_status == 'completed' )
177                 $complete ++;
178 
179             $linked ++;
180         }
181 
182     if ( $complete ) {
183         update_user_meta( $customer_id, 'paying_customer', 1 );
184         update_user_meta( $customer_id, '_order_count', '' );
185         update_user_meta( $customer_id, '_money_spent', '' );
186     }
187 
188     return $linked;
189 }
190 
191 /**
192  * Order Status completed - This is a paying customer
193  *
194  * @access public
195  * @param int $order_id
196  * @return void
197  */
198 function wc_paying_customer( $order_id ) {
199 
200     $order = new WC_Order( $order_id );
201 
202     if ( $order->user_id > 0 ) {
203         update_user_meta( $order->user_id, 'paying_customer', 1 );
204 
205         $old_spent = absint( get_user_meta( $order->user_id, '_money_spent', true ) );
206         update_user_meta( $order->user_id, '_money_spent', $old_spent + $order->order_total );
207 
208         $old_count = absint( get_user_meta( $order->user_id, '_order_count', true ) );
209         update_user_meta( $order->user_id, '_order_count', $old_count + 1 );
210     }
211 }
212 add_action( 'woocommerce_order_status_completed', 'wc_paying_customer' );
213 
214 
215 /**
216  * Checks if a user (by email) has bought an item
217  *
218  * @access public
219  * @param string $customer_email
220  * @param int $user_id
221  * @param int $product_id
222  * @return bool
223  */
224 function wc_customer_bought_product( $customer_email, $user_id, $product_id ) {
225     global $wpdb;
226 
227     $emails = array();
228 
229     if ( $user_id ) {
230         $user     = get_user_by( 'id', $user_id );
231         $emails[] = $user->user_email;
232     }
233 
234     if ( is_email( $customer_email ) ) {
235         $emails[] = $customer_email;
236     }
237 
238     if ( sizeof( $emails ) == 0 ) {
239         return false;
240     }
241 
242     $completed  = get_term_by( 'slug', 'completed', 'shop_order_status' );
243     $processing = get_term_by( 'slug', 'processing', 'shop_order_status' );
244 
245     return $wpdb->get_var(
246         $wpdb->prepare( "
247             SELECT COUNT( DISTINCT order_items.order_item_id )
248             FROM {$wpdb->prefix}woocommerce_order_items as order_items
249             LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS itemmeta ON order_items.order_item_id = itemmeta.order_item_id
250             LEFT JOIN {$wpdb->postmeta} AS postmeta ON order_items.order_id = postmeta.post_id
251             LEFT JOIN {$wpdb->term_relationships} AS rel ON order_items.order_id = rel.object_ID
252             WHERE
253                 rel.term_taxonomy_id IN ( %d, %d ) AND
254                 itemmeta.meta_value  = %s AND
255                 itemmeta.meta_key    IN ( '_variation_id', '_product_id' ) AND
256                 postmeta.meta_key    IN ( '_billing_email', '_customer_user' ) AND
257                 (
258                     postmeta.meta_value  IN ( '" . implode( "','", array_unique( $emails ) ) . "' ) OR
259                     (
260                         postmeta.meta_value = %s AND
261                         postmeta.meta_value > 0
262                     )
263                 )
264             ", $completed->term_taxonomy_id, $processing->term_taxonomy_id, $product_id, $user_id
265         )
266     );
267 }
268 
269 /**
270  * Checks if a user has a certain capability
271  *
272  * @access public
273  * @param array $allcaps
274  * @param array $caps
275  * @param array $args
276  * @return bool
277  */
278 function wc_customer_has_capability( $allcaps, $caps, $args ) {
279     if ( isset( $caps[0] ) ) {
280         switch ( $caps[0] ) {
281 
282             case 'view_order' :
283                 $user_id = $args[1];
284                 $order = new WC_Order( $args[2] );
285 
286                 if ( $user_id == $order->user_id ) {
287                     $allcaps['view_order'] = true;
288                 }
289 
290                 break;
291 
292             case 'pay_for_order' :
293                 $user_id = $args[1];
294                 $order_id = isset( $args[2] ) ? $args[2] : null;
295 
296                 // When no order ID, we assume it's a new order
297                 // and thus, customer can pay for it
298                 if ( ! $order_id ) {
299                     $allcaps['pay_for_order'] = true;
300 
301                     break;
302                 }
303 
304                 $order = new WC_Order( $order_id );
305                 if ( $user_id == $order->user_id || empty( $order->user_id ) ) {
306                     $allcaps['pay_for_order'] = true;
307                 }
308 
309                 break;
310 
311             case 'order_again' :
312                 $user_id = $args[1];
313                 $order = new WC_Order( $args[2] );
314 
315                 if ( $user_id == $order->user_id ) {
316                     $allcaps['order_again'] = true;
317                 }
318 
319                 break;
320 
321             case 'cancel_order' :
322                 $user_id = $args[1];
323                 $order = new WC_Order( $args[2] );
324 
325                 if ( $user_id == $order->user_id ) {
326                     $allcaps['cancel_order'] = true;
327                 }
328 
329                 break;
330 
331             case 'download_file' :
332                 $user_id = $args[1];
333                 $download = $args[2];
334 
335                 if ( $user_id == $download->user_id ) {
336                     $allcaps['download_file'] = true;
337                 }
338 
339                 break;
340         }
341     }
342 
343     return $allcaps;
344 }
345 
346 add_filter( 'user_has_cap', 'wc_customer_has_capability', 10, 3 );
347 
WooCommerce API documentation generated by ApiGen 2.8.0