From 196543ef7f9f6626b446fc7c6845c75eacf63513 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 8 May 2019 15:25:46 +0100 Subject: [PATCH] Render stock status as 'low' on stock report --- .../client/analytics/report/stock/table.js | 20 ++++++++++++++++--- .../client/analytics/report/stock/utils.js | 15 ++++++++++++++ ...wc-admin-rest-reports-stock-controller.php | 19 +++++++++++------- 3 files changed, 44 insertions(+), 10 deletions(-) create mode 100644 plugins/woocommerce-admin/client/analytics/report/stock/utils.js diff --git a/plugins/woocommerce-admin/client/analytics/report/stock/table.js b/plugins/woocommerce-admin/client/analytics/report/stock/table.js index 4ad49b2208b..c36cceb732f 100644 --- a/plugins/woocommerce-admin/client/analytics/report/stock/table.js +++ b/plugins/woocommerce-admin/client/analytics/report/stock/table.js @@ -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 { ); - const stockStatusLink = ( + const stockStatusLink = isLowStock( stock_status, stock_quantity, low_stock_amount ) ? ( + + { _x( 'Low', 'Indication of a low quantity', 'woocommerce-admin' ) } + + ) : ( { stockStatuses[ stock_status ] } diff --git a/plugins/woocommerce-admin/client/analytics/report/stock/utils.js b/plugins/woocommerce-admin/client/analytics/report/stock/utils.js new file mode 100644 index 00000000000..e966b61b240 --- /dev/null +++ b/plugins/woocommerce-admin/client/analytics/report/stock/utils.js @@ -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; +} diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-stock-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-stock-controller.php index 1ca78d3f805..74da6d37f91 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-stock-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-stock-controller.php @@ -283,15 +283,20 @@ class WC_Admin_REST_Reports_Stock_Controller extends WC_REST_Reports_Controller */ public function prepare_item_for_response( $product, $request ) { $data = array( - 'id' => $product->get_id(), - 'parent_id' => $product->get_parent_id(), - 'name' => $product->get_name(), - 'sku' => $product->get_sku(), - 'stock_status' => $product->get_stock_status(), - 'stock_quantity' => (float) $product->get_stock_quantity(), - 'manage_stock' => $product->get_manage_stock(), + 'id' => $product->get_id(), + 'parent_id' => $product->get_parent_id(), + 'name' => $product->get_name(), + 'sku' => $product->get_sku(), + '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 );