Pass products around and change how wc_format_stock_for_display gets called so $product is available.

This commit is contained in:
Mike Jolley 2017-01-11 12:17:18 +00:00
parent 8d4844a31c
commit 11015c4b06
3 changed files with 16 additions and 13 deletions

View File

@ -1869,7 +1869,7 @@ class WC_Product extends WC_Abstract_Legacy_Product {
} elseif ( $this->managing_stock() && $this->is_on_backorder( 1 ) ) { } elseif ( $this->managing_stock() && $this->is_on_backorder( 1 ) ) {
$availability = __( 'Available on backorder', 'woocommerce' ); $availability = __( 'Available on backorder', 'woocommerce' );
} elseif ( $this->managing_stock() ) { } 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 { } else {
$availability = ''; $availability = '';
} }

View File

@ -490,7 +490,7 @@ class WC_Cart {
*/ */
if ( ! $product->has_enough_stock( $product_qty_in_cart[ $product->get_stock_managed_by_id() ] ) ) { if ( ! $product->has_enough_stock( $product_qty_in_cart[ $product->get_stock_managed_by_id() ] ) ) {
/* translators: 1: product name 2: quantity in stock */ /* 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; return $error;
} }
@ -924,7 +924,7 @@ class WC_Cart {
if ( ! $product_data->has_enough_stock( $quantity ) ) { if ( ! $product_data->has_enough_stock( $quantity ) ) {
/* translators: 1: product name 2: quantity in stock */ /* 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 // Stock check - this time accounting for whats already in-cart
@ -936,7 +936,7 @@ class WC_Cart {
'<a href="%s" class="button wc-forward">%s</a> %s', '<a href="%s" class="button wc-forward">%s</a> %s',
wc_get_cart_url(), wc_get_cart_url(),
__( 'View Cart', 'woocommerce' ), __( 'View Cart', 'woocommerce' ),
sprintf( __( 'You cannot add that amount to the cart &mdash; 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 &mdash; 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 ) )
) ); ) );
} }
} }

View File

@ -922,26 +922,27 @@ if ( ! function_exists( 'wc_make_numeric_postcode' ) ) {
/** /**
* Format the stock amount ready for display based on settings. * Format the stock amount ready for display based on settings.
*
* @since 2.7.0 * @since 2.7.0
* @param int $stock_amount * @param WC_Product $product Product object for which the stock you need to format.
* @param boolean $show_backorder_notification
* @return string * @return string
*/ */
function wc_format_stock_for_display( $stock_amount, $show_backorder_notification = false ) { function wc_format_stock_for_display( $product ) {
$display = __( 'In stock', 'woocommerce' ); $display = __( 'In stock', 'woocommerce' );
$stock_amount = $product->get_stock_quantity()
switch ( get_option( 'woocommerce_stock_format' ) ) { switch ( get_option( 'woocommerce_stock_format' ) ) {
case 'low_amount' : case 'low_amount' :
if ( $stock_amount <= get_option( 'woocommerce_notify_low_stock_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; break;
case '' : 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; break;
} }
if ( $show_backorder_notification ) { if ( $product->backorders_allowed() && $product->backorders_require_notification() ) {
$display .= ' ' . __( '(can be backordered)', 'woocommerce' ); $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. * Format the stock quantity ready for display.
*
* @since 2.7.0 * @since 2.7.0
* @param int $stock_quantity * @param int $stock_quantity
* @param WC_Product $product so that we can pass through the filters.
* @return string * @return string
*/ */
function wc_format_stock_quantity_for_display( $stock_quantity ) { function wc_format_stock_quantity_for_display( $stock_quantity, $product ) {
return apply_filters( 'woocommerce_format_stock_quantity', $stock_quantity ); return apply_filters( 'woocommerce_format_stock_quantity', $stock_quantity, $product );
} }
/** /**