69
We’ve already seen how to display stock quantity and status on the Shop Page – today we’ll do something similar, but this time we’ll work on the Cart product table, so that we can visually display stock status and quantity to WooCommerce customers who are about to checkout.
Please note – in order for the snippet to work you must have “stock management” enabled, and also each single product in the cart must have “managing stock” checked and, if on backorder, “allow but notify customer” must be selected, otherwise you will see nothing. Enjoy!
PHP Snippet: Show Stock @ Cart Items
/**
* @snippet Display Stock @ WooCommerce Cart
* @how-to Get tutoraspire.com FREE
* @author Tutor Aspire, BusinessBloomer.com
* @testedwith WooCommerce 4.5
* @donate $9 https://tutoraspire.com
*/
add_action( 'woocommerce_after_cart_item_name', 'tutoraspire_stock_and_backorder_cart_item_title', 9999, 2 );
function tutoraspire_stock_and_backorder_cart_item_title( $cart_item, $cart_item_key ) {
$product = $cart_item['data'];
if ( $product->backorders_require_notification() && $product->is_on_backorder( $cart_item['quantity'] ) ) return;
echo wc_get_stock_html( $product );
}