class-wc-report-stock.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. if ( ! class_exists( 'WP_List_Table' ) ) {
  6. require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
  7. }
  8. /**
  9. * WC_Report_Stock.
  10. *
  11. * @author WooThemes
  12. * @category Admin
  13. * @package WooCommerce/Admin/Reports
  14. * @version 2.1.0
  15. */
  16. class WC_Report_Stock extends WP_List_Table {
  17. /**
  18. * Max items.
  19. *
  20. * @var int
  21. */
  22. protected $max_items;
  23. /**
  24. * Constructor.
  25. */
  26. public function __construct() {
  27. parent::__construct(
  28. array(
  29. 'singular' => 'stock',
  30. 'plural' => 'stock',
  31. 'ajax' => false,
  32. )
  33. );
  34. }
  35. /**
  36. * No items found text.
  37. */
  38. public function no_items() {
  39. _e( 'No products found.', 'woocommerce' );
  40. }
  41. /**
  42. * Don't need this.
  43. *
  44. * @param string $position
  45. */
  46. public function display_tablenav( $position ) {
  47. if ( 'top' !== $position ) {
  48. parent::display_tablenav( $position );
  49. }
  50. }
  51. /**
  52. * Output the report.
  53. */
  54. public function output_report() {
  55. $this->prepare_items();
  56. echo '<div id="poststuff" class="woocommerce-reports-wide">';
  57. $this->display();
  58. echo '</div>';
  59. }
  60. /**
  61. * Get column value.
  62. *
  63. * @param mixed $item
  64. * @param string $column_name
  65. */
  66. public function column_default( $item, $column_name ) {
  67. global $product;
  68. if ( ! $product || $product->get_id() !== $item->id ) {
  69. $product = wc_get_product( $item->id );
  70. }
  71. if ( ! $product ) {
  72. return;
  73. }
  74. switch ( $column_name ) {
  75. case 'product':
  76. if ( $sku = $product->get_sku() ) {
  77. echo esc_html( $sku ) . ' - ';
  78. }
  79. echo esc_html( $product->get_name() );
  80. // Get variation data.
  81. if ( $product->is_type( 'variation' ) ) {
  82. echo '<div class="description">' . wp_kses_post( wc_get_formatted_variation( $product, true ) ) . '</div>';
  83. }
  84. break;
  85. case 'parent':
  86. if ( $item->parent ) {
  87. echo esc_html( get_the_title( $item->parent ) );
  88. } else {
  89. echo '-';
  90. }
  91. break;
  92. case 'stock_status':
  93. if ( $product->is_on_backorder() ) {
  94. $stock_html = '<mark class="onbackorder">' . __( 'On backorder', 'woocommerce' ) . '</mark>';
  95. } elseif ( $product->is_in_stock() ) {
  96. $stock_html = '<mark class="instock">' . __( 'In stock', 'woocommerce' ) . '</mark>';
  97. } else {
  98. $stock_html = '<mark class="outofstock">' . __( 'Out of stock', 'woocommerce' ) . '</mark>';
  99. }
  100. echo apply_filters( 'woocommerce_admin_stock_html', $stock_html, $product );
  101. break;
  102. case 'stock_level':
  103. echo esc_html( $product->get_stock_quantity() );
  104. break;
  105. case 'wc_actions':
  106. ?><p>
  107. <?php
  108. $actions = array();
  109. $action_id = $product->is_type( 'variation' ) ? $item->parent : $item->id;
  110. $actions['edit'] = array(
  111. 'url' => admin_url( 'post.php?post=' . $action_id . '&action=edit' ),
  112. 'name' => __( 'Edit', 'woocommerce' ),
  113. 'action' => 'edit',
  114. );
  115. if ( $product->is_visible() ) {
  116. $actions['view'] = array(
  117. 'url' => get_permalink( $action_id ),
  118. 'name' => __( 'View', 'woocommerce' ),
  119. 'action' => 'view',
  120. );
  121. }
  122. $actions = apply_filters( 'woocommerce_admin_stock_report_product_actions', $actions, $product );
  123. foreach ( $actions as $action ) {
  124. printf(
  125. '<a class="button tips %1$s" href="%2$s" data-tip="%3$s">%4$s</a>',
  126. esc_attr( $action['action'] ),
  127. esc_url( $action['url'] ),
  128. sprintf( esc_attr__( '%s product', 'woocommerce' ), $action['name'] ),
  129. esc_html( $action['name'] )
  130. );
  131. }
  132. ?>
  133. </p>
  134. <?php
  135. break;
  136. }
  137. }
  138. /**
  139. * Get columns.
  140. *
  141. * @return array
  142. */
  143. public function get_columns() {
  144. $columns = array(
  145. 'product' => __( 'Product', 'woocommerce' ),
  146. 'parent' => __( 'Parent', 'woocommerce' ),
  147. 'stock_level' => __( 'Units in stock', 'woocommerce' ),
  148. 'stock_status' => __( 'Stock status', 'woocommerce' ),
  149. 'wc_actions' => __( 'Actions', 'woocommerce' ),
  150. );
  151. return $columns;
  152. }
  153. /**
  154. * Prepare customer list items.
  155. */
  156. public function prepare_items() {
  157. $this->_column_headers = array( $this->get_columns(), array(), $this->get_sortable_columns() );
  158. $current_page = absint( $this->get_pagenum() );
  159. $per_page = apply_filters( 'woocommerce_admin_stock_report_products_per_page', 20 );
  160. $this->get_items( $current_page, $per_page );
  161. /**
  162. * Pagination.
  163. */
  164. $this->set_pagination_args(
  165. array(
  166. 'total_items' => $this->max_items,
  167. 'per_page' => $per_page,
  168. 'total_pages' => ceil( $this->max_items / $per_page ),
  169. )
  170. );
  171. }
  172. }