New filter: stock check message
Adds a new filter to allow customization of the stock check message when a product is out of stock, but accounting for what's already in the cart. It mimics the existing woocommerce_cart_product_not_enough_stock_message filter.
This commit is contained in:
parent
30d7b2ce9d
commit
5b02c440d7
|
@ -1218,15 +1218,30 @@ class WC_Cart extends WC_Legacy_Cart {
|
|||
$products_qty_in_cart = $this->get_cart_item_quantities();
|
||||
|
||||
if ( isset( $products_qty_in_cart[ $product_data->get_stock_managed_by_id() ] ) && ! $product_data->has_enough_stock( $products_qty_in_cart[ $product_data->get_stock_managed_by_id() ] + $quantity ) ) {
|
||||
throw new Exception(
|
||||
sprintf(
|
||||
'<a href="%s" class="button wc-forward">%s</a> %s',
|
||||
wc_get_cart_url(),
|
||||
__( 'View cart', 'woocommerce' ),
|
||||
/* translators: 1: quantity in stock 2: current quantity */
|
||||
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_stock_managed_by_id() ], $product_data ) )
|
||||
)
|
||||
$stock_quantity = $product_data->get_stock_quantity();
|
||||
$stock_quantity_in_cart = $products_qty_in_cart[ $product_data->get_stock_managed_by_id() ];
|
||||
|
||||
$message = printf(
|
||||
'<a href="%s" class="button wc-forward">%s</a> %s',
|
||||
wc_get_cart_url(),
|
||||
__( 'View cart', 'woocommerce' ),
|
||||
/* translators: 1: quantity in stock 2: current quantity */
|
||||
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( $stock_quantity, $product_data ), wc_format_stock_quantity_for_display( $stock_quantity_in_cart, $product_data ) )
|
||||
);
|
||||
|
||||
/**
|
||||
* Filters message about product not having enough stock accounting for what's already in the cart.
|
||||
*
|
||||
* @param string $message Message.
|
||||
* @param WC_Product $product_data Product data.
|
||||
* @param int $stock_quantity Quantity remaining.
|
||||
* @param int $stock_quantity_in_cart
|
||||
*
|
||||
* @since 5.0.0
|
||||
*/
|
||||
$message = apply_filters( 'woocommerce_cart_product_not_enough_stock_already_in_cart_message', $message, $product_data, $stock_quantity, $stock_quantity_in_cart );
|
||||
|
||||
throw new Exception( $message );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue