Advertisement
Guest User

Ap

a guest
Oct 2nd, 2019
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.59 KB | None | 0 0
  1. // Функция проверки куплен ли товар по id
  2.  
  3. function has_bought_multi( $user_id = 0,  $product_ids = 0, $days = 0 ) {
  4. global $wpdb;
  5. $customer_id = $user_id == 0 || $user_id == '' ? get_current_user_id() : $user_id;
  6. $statuses      = array_map( 'esc_sql', wc_get_is_paid_statuses() );
  7. $date = date('Y-m-d H:i:s', strtotime("-$days day") );
  8.  
  9. if ( is_array( $product_ids ) )
  10. $product_ids = implode(',', $product_ids);
  11.  
  12. if ( $product_ids !=  ( 0 || '' ) )
  13. $query_line = "AND woim.meta_value IN ($product_ids)";
  14. else
  15. $query_line = "AND woim.meta_value != 0";
  16.  
  17. // Count the number of products
  18. $product_count_query = $wpdb->get_col( "SELECT COUNT(woim.meta_value) FROM {$wpdb->prefix}posts AS p
  19. INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
  20. INNER JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON p.ID = woi.order_id
  21. INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
  22. WHERE p.post_status IN ( 'wc-" . implode( "','wc-", $statuses ) . "' )
  23. AND p.post_date > '$date'
  24. AND pm.meta_key = '_customer_user'
  25. AND pm.meta_value = $customer_id
  26. AND woim.meta_key IN ( '_product_id', '_variation_id' )
  27. $query_line
  28. " );
  29. // Set the count in a string
  30. $count = reset($product_count_query);
  31. // Return a boolean value if count is higher than 0
  32. return $count > 0 ? true : false;
  33. }
  34.  
  35.  
  36. // Вывод в шаблоне, работает
  37.  
  38. if( has_bought_multi( '', 6678, 360 ) ){
  39.           echo 'Товар куплен';
  40.          print_r ($count);
  41.           } else {
  42.           echo 'Товар не куплен';
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement