Render stock status as 'low' on stock report

This commit is contained in:
Mike Jolley 2019-05-08 15:25:46 +01:00
parent d95e69532e
commit 196543ef7f
3 changed files with 44 additions and 10 deletions

View File

@ -2,7 +2,7 @@
/**
* External dependencies
*/
import { __, _n } from '@wordpress/i18n';
import { __, _n, _x } from '@wordpress/i18n';
import { Component } from '@wordpress/element';
/**
@ -16,6 +16,7 @@ import { numberFormat } from '@woocommerce/number';
* Internal dependencies
*/
import ReportTable from 'analytics/components/report-table';
import { isLowStock } from './utils';
export default class StockReportTable extends Component {
constructor() {
@ -60,7 +61,16 @@ export default class StockReportTable extends Component {
const { stockStatuses } = wcSettings;
return products.map( product => {
const { id, manage_stock, name, parent_id, sku, stock_quantity, stock_status } = product;
const {
id,
manage_stock,
name,
parent_id,
sku,
stock_quantity,
stock_status,
low_stock_amount,
} = product;
const productDetailLink = getNewPath( persistedQuery, '/analytics/products', {
filter: 'single_product',
@ -73,7 +83,11 @@ export default class StockReportTable extends Component {
</Link>
);
const stockStatusLink = (
const stockStatusLink = isLowStock( stock_status, stock_quantity, low_stock_amount ) ? (
<Link href={ 'post.php?action=edit&post=' + ( parent_id || id ) } type="wp-admin">
{ _x( 'Low', 'Indication of a low quantity', 'woocommerce-admin' ) }
</Link>
) : (
<Link href={ 'post.php?action=edit&post=' + ( parent_id || id ) } type="wp-admin">
{ stockStatuses[ stock_status ] }
</Link>

View File

@ -0,0 +1,15 @@
/**
* Determine if a product or variation is in low stock.
*
* @format
* @param {number} threshold - The number at which stock is determined to be low.
* @returns {boolean} - Whether or not the stock is low.
*/
export function isLowStock( status, quantity, threshold ) {
if ( ! quantity ) {
// Sites that don't do inventory tracking will always return false.
return false;
}
return 'instock' === status && quantity <= threshold;
}

View File

@ -290,8 +290,13 @@ class WC_Admin_REST_Reports_Stock_Controller extends WC_REST_Reports_Controller
'stock_status' => $product->get_stock_status(),
'stock_quantity' => (float) $product->get_stock_quantity(),
'manage_stock' => $product->get_manage_stock(),
'low_stock_amount' => $product->get_low_stock_amount(),
);
if ( '' === $data['low_stock_amount'] ) {
$data['low_stock_amount'] = absint( max( get_option( 'woocommerce_notify_low_stock_amount' ), 1 ) );
}
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );