class-wc-report-out-of-stock.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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_Out_Of_Stock.
  10. *
  11. * @author WooThemes
  12. * @category Admin
  13. * @package WooCommerce/Admin/Reports
  14. * @version 2.1.0
  15. */
  16. class WC_Report_Out_Of_Stock extends WC_Report_Stock {
  17. /**
  18. * No items found text.
  19. */
  20. public function no_items() {
  21. _e( 'No out of stock products found.', 'woocommerce' );
  22. }
  23. /**
  24. * Get Products matching stock criteria.
  25. *
  26. * @param int $current_page
  27. * @param int $per_page
  28. */
  29. public function get_items( $current_page, $per_page ) {
  30. global $wpdb;
  31. $this->max_items = 0;
  32. $this->items = array();
  33. // Get products using a query - this is too advanced for get_posts :(
  34. $stock = absint( max( get_option( 'woocommerce_notify_no_stock_amount' ), 0 ) );
  35. $query_from = apply_filters(
  36. 'woocommerce_report_out_of_stock_query_from', "FROM {$wpdb->posts} as posts
  37. INNER JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id
  38. INNER JOIN {$wpdb->postmeta} AS postmeta2 ON posts.ID = postmeta2.post_id
  39. WHERE 1=1
  40. AND posts.post_type IN ( 'product', 'product_variation' )
  41. AND posts.post_status = 'publish'
  42. AND postmeta2.meta_key = '_manage_stock' AND postmeta2.meta_value = 'yes'
  43. AND postmeta.meta_key = '_stock' AND CAST(postmeta.meta_value AS SIGNED) <= '{$stock}'
  44. "
  45. );
  46. $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 posts.post_title DESC LIMIT %d, %d;", ( $current_page - 1 ) * $per_page, $per_page ) );
  47. $this->max_items = $wpdb->get_var( "SELECT COUNT( DISTINCT posts.ID ) {$query_from};" );
  48. }
  49. }