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     /**
 19      * __construct function.
 20      *
 21      * @access public
 22      */
 23     public function __construct(){
 24         parent::__construct( array(
 25             'singular'  => __( 'Stock', 'woocommerce' ),
 26             'plural'    => __( 'Stock', 'woocommerce' ),
 27             'ajax'      => false
 28         ) );
 29     }
 30 
 31     /**
 32      * No items found text
 33      */
 34     public function no_items() {
 35         _e( 'No products found.', 'woocommerce' );
 36     }
 37 
 38     /**
 39      * Don't need this
 40      */
 41     public function display_tablenav( $position ) {
 42         if ( $position != 'top' )
 43             parent::display_tablenav( $position );
 44     }
 45 
 46     /**
 47      * Output the report
 48      */
 49     public function output_report() {
 50         $this->prepare_items();
 51         echo '<div id="poststuff" class="woocommerce-reports-wide">';
 52         $this->display();
 53         echo '</div>';
 54     }
 55 
 56     /**
 57      * column_default function.
 58      *
 59      * @access public
 60      * @param mixed $item
 61      * @param mixed $column_name
 62      */
 63     function column_default( $item, $column_name ) {
 64         global $woocommerce, $wpdb, $product;
 65 
 66         if ( ! $product || $product->id !== $item->id )
 67             $product = get_product( $item->id );
 68 
 69         switch( $column_name ) {
 70             case 'product' :
 71                 if ( $sku = $product->get_sku() )
 72                     echo $sku . ' - ';
 73 
 74                 echo $product->get_title();
 75 
 76                 // Get variation data
 77                 if ( $product->is_type( 'variation' ) ) {
 78                     $list_attributes = array();
 79                     $attributes = $product->get_variation_attributes();
 80 
 81                     foreach ( $attributes as $name => $attribute ) {
 82                         $list_attributes[] = wc_attribute_label( str_replace( 'attribute_', '', $name ) ) . ': <strong>' . $attribute . '</strong>';
 83                     }
 84 
 85                    echo '<div class="description">' . implode( ', ', $list_attributes ) . '</div>';
 86                 }
 87             break;
 88             case 'parent' :
 89                 if ( $item->parent )
 90                     echo get_the_title( $item->parent );
 91                 else
 92                     echo '-';
 93             break;
 94             case 'stock_status' :
 95                 if ( $product->is_in_stock() ) {
 96                     echo '<mark class="instock">' . __( 'In stock', 'woocommerce' ) . '</mark>';
 97                 } else {
 98                     echo '<mark class="outofstock">' . __( 'Out of stock', 'woocommerce' ) . '</mark>';
 99                 }
100             break;
101             case 'stock_level' :
102                 echo $product->get_stock_quantity();
103             break;
104             case 'wc_actions' :
105                 ?><p>
106                     <?php
107                         $actions = array();
108                         $action_id = $product->is_type( 'variation' ) ? $item->parent : $item->id;
109 
110                         $actions['edit'] = array(
111                             'url'       => admin_url( 'post.php?post=' . $action_id . '&action=edit' ),
112                             'name'      => __( 'Edit', 'woocommerce' ),
113                             'action'    => "edit"
114                         );
115 
116                         if ( $product->is_visible() )
117                             $actions['view'] = array(
118                                 'url'       => get_permalink( $action_id ),
119                                 'name'      => __( 'View', 'woocommerce' ),
120                                 'action'    => "view"
121                             );
122 
123                         $actions = apply_filters( 'woocommerce_admin_stock_report_product_actions', $actions, $product );
124 
125                         foreach ( $actions as $action ) {
126                             printf( '<a class="button tips %s" href="%s" data-tip="%s ' . __( 'product', 'woocommerce' ) . '">%s</a>', $action['action'], esc_url( $action['url'] ), esc_attr( $action['name'] ), esc_attr( $action['name'] ) );
127                         }
128                     ?>
129                 </p><?php
130             break;
131         }
132     }
133 
134     /**
135      * get_columns function.
136      *
137      * @access public
138      */
139     function get_columns(){
140         $columns = array(
141             'product'      => __( 'Product', 'woocommerce' ),
142             'parent'       => __( 'Parent', 'woocommerce' ),
143             'stock_level'  => __( 'Units in stock', 'woocommerce' ),
144             'stock_status' => __( 'Stock status', 'woocommerce' ),
145             'wc_actions'   => __( 'Actions', 'woocommerce' ),
146         );
147 
148         return $columns;
149     }
150 
151     /**
152      * prepare_items function.
153      *
154      * @access public
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              = 20;
160 
161         $this->get_items( $current_page, $per_page );
162 
163         /**
164          * Pagination
165          */
166         $this->set_pagination_args( array(
167             'total_items' => $this->max_items,
168             'per_page'    => $per_page,
169             'total_pages' => ceil( $this->max_items / $per_page )
170         ) );
171     }
172 }
WooCommerce API documentation generated by ApiGen 2.8.0