Show 'out of stock' marked products in reports, and remove sort by stock - stock values vary so much this is unreliable and impossible to sort correctly. Closes #2380.

This commit is contained in:
Mike Jolley 2013-02-08 17:35:26 +00:00
parent a7fdba79fb
commit 9402a572d7
2 changed files with 48 additions and 31 deletions

View File

@ -290,7 +290,6 @@ add_action('manage_product_posts_custom_column', 'woocommerce_custom_product_col
*/ */
function woocommerce_custom_product_sort($columns) { function woocommerce_custom_product_sort($columns) {
$custom = array( $custom = array(
'is_in_stock' => 'inventory',
'price' => 'price', 'price' => 'price',
'featured' => 'featured', 'featured' => 'featured',
'sku' => 'sku', 'sku' => 'sku',
@ -313,12 +312,6 @@ add_filter( 'manage_edit-product_sortable_columns', 'woocommerce_custom_product_
*/ */
function woocommerce_custom_product_orderby( $vars ) { function woocommerce_custom_product_orderby( $vars ) {
if (isset( $vars['orderby'] )) : if (isset( $vars['orderby'] )) :
if ( 'inventory' == $vars['orderby'] ) :
$vars = array_merge( $vars, array(
'meta_key' => '_stock',
'orderby' => 'meta_value_num'
) );
endif;
if ( 'price' == $vars['orderby'] ) : if ( 'price' == $vars['orderby'] ) :
$vars = array_merge( $vars, array( $vars = array_merge( $vars, array(
'meta_key' => '_price', 'meta_key' => '_price',

View File

@ -1981,7 +1981,8 @@ function woocommerce_stock_overview() {
'terms' => array('simple'), 'terms' => array('simple'),
'operator' => 'IN' 'operator' => 'IN'
) )
) ),
'fields' => 'id=>parent'
); );
$low_stock_products = (array) get_posts($args); $low_stock_products = (array) get_posts($args);
@ -2000,15 +2001,16 @@ function woocommerce_stock_overview() {
), ),
array( array(
'key' => '_stock', 'key' => '_stock',
'value' => array('', false, null), 'value' => array( '', false, null ),
'compare' => 'NOT IN' 'compare' => 'NOT IN'
) )
) ),
'fields' => 'id=>parent'
); );
$low_stock_variations = (array) get_posts($args); $low_stock_variations = (array) get_posts($args);
// Finally, get low stock variable products (where stock is set for the parent) // Get low stock variable products (where stock is set for the parent)
$args = array( $args = array(
'post_type' => array('product'), 'post_type' => array('product'),
'post_status' => 'publish', 'post_status' => 'publish',
@ -2033,14 +2035,31 @@ function woocommerce_stock_overview() {
'terms' => array('variable'), 'terms' => array('variable'),
'operator' => 'IN' 'operator' => 'IN'
) )
) ),
'fields' => 'id=>parent'
); );
$low_stock_variable_products = (array) get_posts($args); $low_stock_variable_products = (array) get_posts($args);
// Merge results // Get products marked out of stock
$low_in_stock = apply_filters( 'woocommerce_reports_stock_overview_products', array_merge( $low_stock_products, $low_stock_variations, $low_stock_variable_products ) ); $args = array(
'post_type' => array( 'product' ),
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_stock_status',
'value' => 'outofstock'
)
),
'fields' => 'id=>parent'
);
$out_of_stock_status_products = (array) get_posts($args);
// Merge results
$low_in_stock = apply_filters( 'woocommerce_reports_stock_overview_products', $low_stock_products + $low_stock_variations + $low_stock_variable_products + $out_of_stock_status_products );
?> ?>
<div id="poststuff" class="woocommerce-reports-wrap halved"> <div id="poststuff" class="woocommerce-reports-wrap halved">
<div class="woocommerce-reports-left"> <div class="woocommerce-reports-left">
@ -2050,22 +2069,23 @@ function woocommerce_stock_overview() {
<?php <?php
if ( $low_in_stock ) { if ( $low_in_stock ) {
echo '<ul class="stock_list">'; echo '<ul class="stock_list">';
foreach ( $low_in_stock as $product ) { foreach ( $low_in_stock as $product_id => $parent ) {
$stock = (int) get_post_meta( $product->ID, '_stock', true ); $stock = (int) get_post_meta( $product_id, '_stock', true );
$sku = get_post_meta( $product->ID, '_sku', true ); $sku = get_post_meta( $product_id, '_sku', true );
if ( $stock <= $nostockamount ) continue; if ( $stock <= $nostockamount || in_array( $product_id, array_keys( $out_of_stock_status_products ) ) )
continue;
$title = esc_html__( $product->post_title ); $title = esc_html__( get_the_title( $product_id ) );
if ( $sku ) if ( $sku )
$title .= ' (' . __( 'SKU', 'woocommerce' ) . ': ' . esc_html( $sku ) . ')'; $title .= ' (' . __( 'SKU', 'woocommerce' ) . ': ' . esc_html( $sku ) . ')';
if ( $product->post_type=='product' ) if ( get_post_type( $product_id ) == 'product' )
$product_url = admin_url( 'post.php?post=' . $product->ID . '&action=edit' ); $product_url = admin_url( 'post.php?post=' . $product_id . '&action=edit' );
else else
$product_url = admin_url( 'post.php?post=' . $product->post_parent . '&action=edit' ); $product_url = admin_url( 'post.php?post=' . $parent . '&action=edit' );
printf( '<li><a href="%s"><small>' . _n('%d in stock', '%d in stock', $stock, 'woocommerce') . '</small> %s</a></li>', $product_url, $stock, $title ); printf( '<li><a href="%s"><small>' . _n('%d in stock', '%d in stock', $stock, 'woocommerce') . '</small> %s</a></li>', $product_url, $stock, $title );
@ -2085,23 +2105,27 @@ function woocommerce_stock_overview() {
<?php <?php
if ( $low_in_stock ) { if ( $low_in_stock ) {
echo '<ul class="stock_list">'; echo '<ul class="stock_list">';
foreach ( $low_in_stock as $product ) { foreach ( $low_in_stock as $product_id => $parent ) {
$stock = (int) get_post_meta( $product->ID, '_stock', true ); $stock = get_post_meta( $product_id, '_stock', true );
$sku = get_post_meta( $product->ID, '_sku', true ); $sku = get_post_meta( $product_id, '_sku', true );
if ( $stock > $nostockamount ) continue; if ( $stock > $nostockamount && ! in_array( $product_id, array_keys( $out_of_stock_status_products ) ) )
continue;
$title = esc_html__( $product->post_title ); $title = esc_html__( get_the_title( $product_id ) );
if ( $sku ) if ( $sku )
$title .= ' (' . __( 'SKU', 'woocommerce' ) . ': ' . esc_html( $sku ) . ')'; $title .= ' (' . __( 'SKU', 'woocommerce' ) . ': ' . esc_html( $sku ) . ')';
if ( $product->post_type=='product' ) if ( get_post_type( $product_id ) == 'product' )
$product_url = admin_url( 'post.php?post=' . $product->ID . '&action=edit' ); $product_url = admin_url( 'post.php?post=' . $product_id . '&action=edit' );
else else
$product_url = admin_url( 'post.php?post=' . $product->post_parent . '&action=edit' ); $product_url = admin_url( 'post.php?post=' . $parent . '&action=edit' );
if ( $stock == '' )
printf( '<li><a href="%s"><small>' . __('Marked out of stock', 'woocommerce') . '</small> %s</a></li>', $product_url, $title );
else
printf( '<li><a href="%s"><small>' . _n('%d in stock', '%d in stock', $stock, 'woocommerce') . '</small> %s</a></li>', $product_url, $stock, $title ); printf( '<li><a href="%s"><small>' . _n('%d in stock', '%d in stock', $stock, 'woocommerce') . '</small> %s</a></li>', $product_url, $stock, $title );
} }