array('product', 'product_variation'),
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => -1
);
$my_query = new WP_Query($args);
if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();
if ($my_query->post->post_type == 'product_variation') :
$parent = $my_query->post->post_parent;
$_product = &new woocommerce_product_variation( $my_query->post->ID );
else :
$parent = $my_query->post->ID;
$_product = &new woocommerce_product( $my_query->post->ID );
endif;
if (!$_product->managing_stock()) continue;
$thisitem = '
'.$_product->stock.' |
'.$my_query->post->post_title.' |
';
if ($_product->total_stock<=$nostockamount) :
$outofstock[] = $thisitem;
continue;
endif;
if ($_product->total_stock<=$lowstockamount) $lowinstock[] = $thisitem;
endwhile; endif;
wp_reset_query();
if (sizeof($lowinstock)==0) :
$lowinstock[] = ''.__('No products are low in stock.', 'woothemes').' |
';
endif;
if (sizeof($outofstock)==0) :
$outofstock[] = ''.__('No products are out of stock.', 'woothemes').' |
';
endif;
?>
'.__('Next Month →', 'woo themes').'';
endif;
$sales_heading .= ''.__('← Previous Month', 'woo themes').''.__('Monthly Sales', 'woothemes').'';
wp_add_dashboard_widget('woocommmerce_dashboard_sales', $sales_heading, 'woocommmerce_dashboard_sales');
wp_add_dashboard_widget('woocommmerce_dashboard_recent_orders', __('WooCommerce recent orders', 'woothemes'), 'woocommmerce_dashboard_recent_orders');
wp_add_dashboard_widget('woocommmerce_dashboard_recent_reviews', __('WooCommerce recent reviews', 'woothemes'), 'woocommmerce_dashboard_recent_reviews');
}
/**
* Recent orders widget
*/
function woocommmerce_dashboard_recent_orders() {
$args = array(
'numberposts' => 8,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'shop_order',
'post_status' => 'publish'
);
$orders = get_posts( $args );
if ($orders) :
echo '';
foreach ($orders as $order) :
$this_order = &new woocommerce_order( $order->ID );
echo '
-
'.ucwords($this_order->status).' '.date_i18n('l jS \of F Y h:i:s A', strtotime($this_order->order_date)).'
'.sizeof($this_order->items).' '._n('item', 'items', sizeof($this_order->items), 'woothemes').' '.__('Total: ', 'woothemes').woocommerce_price($this_order->order_total).'
';
endforeach;
echo '
';
endif;
}
/**
* Recent reviews widget
*/
function woocommmerce_dashboard_recent_reviews() {
global $wpdb;
$comments = $wpdb->get_results("SELECT *, SUBSTRING(comment_content,1,100) AS comment_excerpt
FROM $wpdb->comments
LEFT JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID)
WHERE comment_approved = '1'
AND comment_type = ''
AND post_password = ''
AND post_type = 'product'
ORDER BY comment_date_gmt DESC
LIMIT 5" );
if ($comments) :
echo '';
else :
echo ''.__('There are no product reviews yet.', 'woothemes').'
';
endif;
}
/**
* Orders this month filter function
*/
function orders_this_month( $where = '' ) {
global $current_month_offset;
$month = $current_month_offset;
$year = (int) date('Y');
$first_day = strtotime("{$year}-{$month}-01");
$last_day = strtotime('-1 second', strtotime('+1 month', $first_day));
$after = date('Y-m-d', $first_day);
$before = date('Y-m-d', $last_day);
$where .= " AND post_date > '$after'";
$where .= " AND post_date < '$before'";
return $where;
}
/**
* Sales widget
*/
function woocommmerce_dashboard_sales() {
?>id!=='dashboard') return;
global $current_month_offset;
// Get orders to display in widget
add_filter( 'posts_where', 'orders_this_month' );
$args = array(
'numberposts' => -1,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'shop_order',
'post_status' => 'publish' ,
'suppress_filters' => false
);
$orders = get_posts( $args );
$order_counts = array();
$order_amounts = array();
// Blank date ranges to begin
$month = $current_month_offset;
$year = (int) date('Y');
$first_day = strtotime("{$year}-{$month}-01");
$last_day = strtotime('-1 second', strtotime('+1 month', $first_day));
if ((date('m') - $current_month_offset)==0) :
$up_to = date('d', strtotime('NOW'));
else :
$up_to = date('d', $last_day);
endif;
$count = 0;
while ($count < $up_to) :
$time = strtotime(date('Ymd', strtotime('+ '.$count.' DAY', $first_day))).'000';
$order_counts[$time] = 0;
$order_amounts[$time] = 0;
$count++;
endwhile;
if ($orders) :
foreach ($orders as $order) :
$order_data = &new woocommerce_order($order->ID);
if ($order_data->status=='cancelled' || $order_data->status=='refunded') continue;
$time = strtotime(date('Ymd', strtotime($order->post_date))).'000';
if (isset($order_counts[$time])) :
$order_counts[$time]++;
else :
$order_counts[$time] = 1;
endif;
if (isset($order_amounts[$time])) :
$order_amounts[$time] = $order_amounts[$time] + $order_data->order_total;
else :
$order_amounts[$time] = (float) $order_data->order_total;
endif;
endforeach;
endif;
remove_filter( 'posts_where', 'orders_this_month' );
/* Script variables */
$params = array(
'currency_symbol' => get_woocommerce_currency_symbol()
);
$order_counts_array = array();
foreach ($order_counts as $key => $count) :
$order_counts_array[] = array($key, $count);
endforeach;
$order_amounts_array = array();
foreach ($order_amounts as $key => $amount) :
$order_amounts_array[] = array($key, $amount);
endforeach;
$order_data = array( 'order_counts' => $order_counts_array, 'order_amounts' => $order_amounts_array );
$params['order_data'] = json_encode($order_data);
// Queue scripts
$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
wp_register_script( 'woocommerce_dashboard_sales', $woocommerce->plugin_url() . '/assets/js/admin/dashboard_sales'.$suffix.'.js', 'jquery', '1.0' );
wp_register_script( 'flot', $woocommerce->plugin_url() . '/assets/js/admin/jquery.flot'.$suffix.'.js', 'jquery', '1.0' );
wp_localize_script( 'woocommerce_dashboard_sales', 'params', $params );
wp_print_scripts('flot');
wp_print_scripts('woocommerce_dashboard_sales');
}
add_action('admin_footer', 'woocommmerce_dashboard_sales_js');