I'm trying to display a custom notice, depending on wheater the order contains products which are in back-order or are all in stock.
I have modified code from this answer. It works, but only in the cart. I want it to show everywhere: in the cart, in checkout, admin, emails and so on.
this is what I have:
// Conditional function: Checking cart items stock
function is_mixed_stock_items(){
$enough_stock = $out_of_stock = false; // initializing
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
// Get an instance of the WC_Product Object
$product = $cart_item['data'];
// Get stock quantity
$stock_qty = $product->get_stock_quantity();
// get cart item quantity
$item_qty = $cart_item['quantity'];
if( ( $stock_qty - $item_qty ) >= 0 ){
$enough_stock = true; // enough stock
} else {
$out_of_stock = true; // not enough stock
}
}
// return true if stock is mixed and false if not
return $enough_stock && $out_of_stock ? true : false;
}
// Display a custom notice
add_action( 'woocommerce_before_cart_totals', 'display_delivery_notification' );
function display_delivery_notification() {
if( is_mixed_stock_items() ){
echo'<div class="cart_totals">Forventet levering: 2-10 hverdage</div>';
}
else {
echo'<div class="cart_totals">Levering: 1-2 hverdage</div>';
}
}