class-wc-report-most-stocked.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. if ( ! class_exists( 'WC_Report_Stock' ) ) {
  6. require_once dirname( __FILE__ ) . '/class-wc-report-stock.php';
  7. }
  8. /**
  9. * WC_Report_Most_Stocked.
  10. *
  11. * @author WooThemes
  12. * @category Admin
  13. * @package WooCommerce/Admin/Reports
  14. * @version 2.1.0
  15. */
  16. class WC_Report_Most_Stocked extends WC_Report_Stock {
  17. /**
  18. * Get Products matching stock criteria.
  19. *
  20. * @param int $current_page
  21. * @param int $per_page
  22. */
  23. public function get_items( $current_page, $per_page ) {
  24. global $wpdb;
  25. $this->max_items = 0;
  26. $this->items = array();
  27. // Get products using a query - this is too advanced for get_posts :(
  28. $stock = absint( max( get_option( 'woocommerce_notify_low_stock_amount' ), 0 ) );
  29. $query_from = "FROM {$wpdb->posts} as posts
  30. INNER JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id
  31. INNER JOIN {$wpdb->postmeta} AS postmeta2 ON posts.ID = postmeta2.post_id
  32. WHERE 1=1
  33. AND posts.post_type IN ( 'product', 'product_variation' )
  34. AND posts.post_status = 'publish'
  35. AND postmeta2.meta_key = '_manage_stock' AND postmeta2.meta_value = 'yes'
  36. AND postmeta.meta_key = '_stock' AND CAST(postmeta.meta_value AS SIGNED) > '{$stock}'
  37. ";
  38. $query_from = apply_filters( 'woocommerce_report_most_stocked_query_from', $query_from );
  39. $this->items = $wpdb->get_results( $wpdb->prepare( "SELECT posts.ID as id, posts.post_parent as parent {$query_from} GROUP BY posts.ID ORDER BY CAST(postmeta.meta_value AS SIGNED) DESC LIMIT %d, %d;", ( $current_page - 1 ) * $per_page, $per_page ) );
  40. $this->max_items = $wpdb->get_var( "SELECT COUNT( DISTINCT posts.ID ) {$query_from};" );
  41. }
  42. }