Skip to content

Instantly share code, notes, and snippets.

@jackgregory
Last active August 29, 2015 14:04
Show Gist options
  • Save jackgregory/c9912143cc4a8a13f4e5 to your computer and use it in GitHub Desktop.
Save jackgregory/c9912143cc4a8a13f4e5 to your computer and use it in GitHub Desktop.
WooCommerce - Add a low stock note to cart items
add_filter( 'woocommerce_cart_item_name', 'cart_low_stock_note', 10, 3 );
function cart_low_stock_note( $title, $cart_item, $cart_item_key ) {
if( $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$output = '';
$output.= $title;
// threshold
if( $_product->get_stock_quantity() <= 4 ) {
$output.= '<p class="stock-note">' . sprintf( __( 'Note: Only %d left in stock', 'woocommerce' ), $_product->get_stock_quantity() ) .'</p>';
}
return $output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment