1

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>';
    }




}
tiny
  • 447
  • 4
  • 18
  • 1
    Well, it depends on where you would like this message to appear. For example, if you put this message under a product, it can be done via add_meta_data, or if you place it after the product title .. then this can also be done fairly easily. However, if you want to add a custom field with the notice, you will have to adjust multiple hook for each of the above pages, which requires some code, of course, and not 1 'global' adjustment – 7uc1f3r Feb 24 '20 at 23:55

0 Answers0