diff --git a/includes/abstracts/abstract-wc-product.php b/includes/abstracts/abstract-wc-product.php index 62e937365e2..466ae3882b4 100644 --- a/includes/abstracts/abstract-wc-product.php +++ b/includes/abstracts/abstract-wc-product.php @@ -1869,7 +1869,7 @@ class WC_Product extends WC_Abstract_Legacy_Product { } elseif ( $this->managing_stock() && $this->is_on_backorder( 1 ) ) { $availability = __( 'Available on backorder', 'woocommerce' ); } elseif ( $this->managing_stock() ) { - $availability = wc_format_stock_for_display( $this->get_stock_quantity(), $this->backorders_allowed() && $this->backorders_require_notification() ); + $availability = wc_format_stock_for_display( $this ); } else { $availability = ''; } diff --git a/includes/class-wc-cart.php b/includes/class-wc-cart.php index 5fa2d21b73e..9d989615267 100644 --- a/includes/class-wc-cart.php +++ b/includes/class-wc-cart.php @@ -490,7 +490,7 @@ class WC_Cart { */ if ( ! $product->has_enough_stock( $product_qty_in_cart[ $product->get_stock_managed_by_id() ] ) ) { /* translators: 1: product name 2: quantity in stock */ - $error->add( 'out-of-stock', sprintf( __( 'Sorry, we do not have enough "%1$s" in stock to fulfill your order (%2$s in stock). Please edit your cart and try again. We apologise for any inconvenience caused.', 'woocommerce' ), $product->get_name(), $product->get_stock_quantity() ) ); + $error->add( 'out-of-stock', sprintf( __( 'Sorry, we do not have enough "%1$s" in stock to fulfill your order (%2$s in stock). Please edit your cart and try again. We apologise for any inconvenience caused.', 'woocommerce' ), $product->get_name(), wc_format_stock_quantity_for_display( $product->get_stock_quantity(), $product ) ) ); return $error; } @@ -924,7 +924,7 @@ class WC_Cart { if ( ! $product_data->has_enough_stock( $quantity ) ) { /* translators: 1: product name 2: quantity in stock */ - throw new Exception( sprintf( __( 'You cannot add that amount of "%1$s" to the cart because there is not enough stock (%2$s remaining).', 'woocommerce' ), $product_data->get_name(), $product_data->get_stock_quantity() ) ); + throw new Exception( sprintf( __( 'You cannot add that amount of "%1$s" to the cart because there is not enough stock (%2$s remaining).', 'woocommerce' ), $product_data->get_name(), wc_format_stock_quantity_for_display( $product_data->get_stock_quantity(), $product_data ) ) ); } // Stock check - this time accounting for whats already in-cart @@ -936,7 +936,7 @@ class WC_Cart { '%s %s', wc_get_cart_url(), __( 'View Cart', 'woocommerce' ), - sprintf( __( 'You cannot add that amount to the cart — we have %1$s in stock and you already have %2$s in your cart.', 'woocommerce' ), $product_data->get_stock_quantity(), $products_qty_in_cart[ $product_data->get_id() ] ) + sprintf( __( 'You cannot add that amount to the cart — we have %1$s in stock and you already have %2$s in your cart.', 'woocommerce' ), wc_format_stock_quantity_for_display( $product_data->get_stock_quantity(), $product_data ), wc_format_stock_quantity_for_display( $products_qty_in_cart[ $product_data->get_id() ], $product_data ) ) ) ); } } diff --git a/includes/wc-formatting-functions.php b/includes/wc-formatting-functions.php index ba720113d04..594d299d7ee 100644 --- a/includes/wc-formatting-functions.php +++ b/includes/wc-formatting-functions.php @@ -922,32 +922,45 @@ if ( ! function_exists( 'wc_make_numeric_postcode' ) ) { /** * Format the stock amount ready for display based on settings. + * * @since 2.7.0 - * @param int $stock_amount - * @param boolean $show_backorder_notification + * @param WC_Product $product Product object for which the stock you need to format. * @return string */ -function wc_format_stock_for_display( $stock_amount, $show_backorder_notification = false ) { - $display = __( 'In stock', 'woocommerce' ); +function wc_format_stock_for_display( $product ) { + $display = __( 'In stock', 'woocommerce' ); + $stock_amount = $product->get_stock_quantity(); switch ( get_option( 'woocommerce_stock_format' ) ) { case 'low_amount' : if ( $stock_amount <= get_option( 'woocommerce_notify_low_stock_amount' ) ) { - $display = sprintf( __( 'Only %s left in stock', 'woocommerce' ), $stock_amount ); + $display = sprintf( __( 'Only %s left in stock', 'woocommerce' ), wc_format_stock_quantity_for_display( $stock_amount, $product ) ); } break; case '' : - $display = sprintf( __( '%s in stock', 'woocommerce' ), $stock_amount ); + $display = sprintf( __( '%s in stock', 'woocommerce' ), wc_format_stock_quantity_for_display( $stock_amount, $product ) ); break; } - if ( $show_backorder_notification ) { + if ( $product->backorders_allowed() && $product->backorders_require_notification() ) { $display .= ' ' . __( '(can be backordered)', 'woocommerce' ); } return $display; } +/** + * Format the stock quantity ready for display. + * + * @since 2.7.0 + * @param int $stock_quantity + * @param WC_Product $product so that we can pass through the filters. + * @return string + */ +function wc_format_stock_quantity_for_display( $stock_quantity, $product ) { + return apply_filters( 'woocommerce_format_stock_quantity', $stock_quantity, $product ); +} + /** * Format a sale price for display. * @since 2.7.0