From ff0926aa2d78d3bc62f328a8fc76aec0e56c0301 Mon Sep 17 00:00:00 2001 From: Kathy Darling Date: Wed, 4 Jan 2017 12:24:36 -0500 Subject: [PATCH 1/4] use wc_format_stock_quantity_for_display() to wrap stock quantity numbers for front end display. closes #12753. --- includes/class-wc-cart.php | 6 +++--- includes/wc-formatting-functions.php | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/includes/class-wc-cart.php b/includes/class-wc-cart.php index 5fa2d21b73e..91918480406 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() ) ) ); 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() ) ) ); } // 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() ), wc_format_stock_quantity_for_display( $products_qty_in_cart[ $product_data->get_id() ] ) ) ) ); } } diff --git a/includes/wc-formatting-functions.php b/includes/wc-formatting-functions.php index 1fb7b2cf3a7..be4954a9657 100644 --- a/includes/wc-formatting-functions.php +++ b/includes/wc-formatting-functions.php @@ -933,11 +933,11 @@ function wc_format_stock_for_display( $stock_amount, $show_backorder_notificatio 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 ) ); } break; case '' : - $display = sprintf( __( '%s in stock', 'woocommerce' ), $stock_amount ); + $display = sprintf( __( '%s in stock', 'woocommerce' ), wc_format_stock_quantity_for_display( $stock_amount ) ); break; } @@ -948,6 +948,16 @@ function wc_format_stock_for_display( $stock_amount, $show_backorder_notificatio return $display; } +/** + * Format the stock quantity ready for display. + * @since 2.7.0 + * @param int $stock_quantity + * @return string + */ +function wc_format_stock_quantity_for_display( $stock_quantity ) { + return apply_filters( 'woocommerce_format_stock_quantity', $stock_quantity ); +} + /** * Format a sale price for display. * @since 2.7.0 From 8d4844a31c0863f2c045b2813268cd3ef5283a00 Mon Sep 17 00:00:00 2001 From: Kathy Darling Date: Fri, 6 Jan 2017 14:39:02 -0500 Subject: [PATCH 2/4] space after parens --- includes/class-wc-cart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-wc-cart.php b/includes/class-wc-cart.php index 91918480406..9e5640c1909 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(), wc_format_stock_quantity_for_display($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() ) ) ); return $error; } From 11015c4b066ec0f8b8c944dc7f1fd42d6644e457 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 11 Jan 2017 12:17:18 +0000 Subject: [PATCH 3/4] Pass products around and change how wc_format_stock_for_display gets called so $product is available. --- includes/abstracts/abstract-wc-product.php | 2 +- includes/class-wc-cart.php | 6 +++--- includes/wc-formatting-functions.php | 21 ++++++++++++--------- 3 files changed, 16 insertions(+), 13 deletions(-) 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 9e5640c1909..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(), wc_format_stock_quantity_for_display( $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(), wc_format_stock_quantity_for_display( $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' ), wc_format_stock_quantity_for_display( $product_data->get_stock_quantity() ), wc_format_stock_quantity_for_display( $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 be4954a9657..ca29561e203 100644 --- a/includes/wc-formatting-functions.php +++ b/includes/wc-formatting-functions.php @@ -922,26 +922,27 @@ 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' ), wc_format_stock_quantity_for_display( $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' ), wc_format_stock_quantity_for_display( $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' ); } @@ -950,12 +951,14 @@ function wc_format_stock_for_display( $stock_amount, $show_backorder_notificatio /** * 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 ) { - return apply_filters( 'woocommerce_format_stock_quantity', $stock_quantity ); +function wc_format_stock_quantity_for_display( $stock_quantity, $product ) { + return apply_filters( 'woocommerce_format_stock_quantity', $stock_quantity, $product ); } /** From 3e31f2f361c1b95421bb9ce144cce526ed456363 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 11 Jan 2017 12:19:23 +0000 Subject: [PATCH 4/4] Syntax --- includes/wc-formatting-functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/wc-formatting-functions.php b/includes/wc-formatting-functions.php index ca29561e203..2e725ec630a 100644 --- a/includes/wc-formatting-functions.php +++ b/includes/wc-formatting-functions.php @@ -929,7 +929,7 @@ if ( ! function_exists( 'wc_make_numeric_postcode' ) ) { */ function wc_format_stock_for_display( $product ) { $display = __( 'In stock', 'woocommerce' ); - $stock_amount = $product->get_stock_quantity() + $stock_amount = $product->get_stock_quantity(); switch ( get_option( 'woocommerce_stock_format' ) ) { case 'low_amount' :