2013-07-09 10:48:56 +00:00
< ? php
2014-09-20 19:58:32 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
2013-07-09 10:48:56 +00:00
exit ; // Exit if accessed directly
2014-09-20 19:58:32 +00:00
}
2013-07-09 10:48:56 +00:00
2014-09-20 19:58:32 +00:00
if ( ! class_exists ( 'WC_Report_Stock' ) ) {
2013-07-09 10:48:56 +00:00
require_once ( 'class-wc-report-stock.php' );
2014-09-20 19:58:32 +00:00
}
2013-07-09 10:48:56 +00:00
/**
2014-02-14 13:02:37 +00:00
* WC_Report_Low_In_Stock
2013-07-09 10:48:56 +00:00
*
2014-08-31 07:19:13 +00:00
* @ author WooThemes
* @ category Admin
* @ package WooCommerce / Admin / Reports
2014-02-14 13:02:37 +00:00
* @ version 2.1 . 0
2013-07-09 10:48:56 +00:00
*/
class WC_Report_Low_In_Stock extends WC_Report_Stock {
2014-08-31 07:19:13 +00:00
/**
* No items found text
*/
public function no_items () {
_e ( 'No low in stock products found.' , 'woocommerce' );
}
/**
* Get Products matching stock criteria
*/
public function get_items ( $current_page , $per_page ) {
global $wpdb ;
2013-07-09 10:48:56 +00:00
$this -> max_items = 0 ;
$this -> items = array ();
// Get products using a query - this is too advanced for get_posts :(
$stock = absint ( max ( get_option ( 'woocommerce_notify_low_stock_amount' ), 1 ) );
$nostock = absint ( max ( get_option ( 'woocommerce_notify_no_stock_amount' ), 0 ) );
$query_from = " FROM { $wpdb -> posts } as posts
INNER JOIN { $wpdb -> postmeta } AS postmeta ON posts . ID = postmeta . post_id
INNER JOIN { $wpdb -> postmeta } AS postmeta2 ON posts . ID = postmeta2 . post_id
WHERE 1 = 1
2014-10-21 13:50:15 +00:00
AND posts . post_type IN ( 'product' , 'product_variation' )
AND posts . post_status = 'publish'
AND postmeta2 . meta_key = '_manage_stock' AND postmeta2 . meta_value = 'yes'
AND postmeta . meta_key = '_stock' AND CAST ( postmeta . meta_value AS SIGNED ) <= '{$stock}'
2014-10-24 10:42:02 +00:00
AND postmeta . meta_key = '_stock' AND CAST ( postmeta . meta_value AS SIGNED ) > '{$nostock}'
2013-07-09 10:48:56 +00:00
" ;
2015-01-13 13:34:48 +00:00
$query_from = apply_filters ( 'woocommerce_report_low_in_stock_query_from' , $query_from );
2013-07-09 10:48:56 +00:00
$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 ) );
$this -> max_items = $wpdb -> get_var ( " SELECT COUNT( DISTINCT posts.ID ) { $query_from } ; " );
2014-08-31 07:19:13 +00:00
}
}