Ashley
Ashley

Reputation: 21

WooCommerce cart.php display backorder message

I am trying to display a message under a product in the cart. If the product is on backorder it displays a backorder notification, so I want it to display another message when there is no backorder notification to display.

The code I've tried is:

// Backorder notification
if ( $_product->backorders_require_notification() && $_product->is_on_backorder( $cart_item['quantity'] ) ) {
    echo '<p class="backorder_notification">' . esc_html__( 'Available in 3-5 Working Days', 'teencode' ) '</p>';
}
else; {
    echo '<p class="backorder_notification">' . esc_html__( 'Available Next Day', 'teencode' ) '</p>';
}

However, when viewing the cart it's not displaying the message.

Where am I going wrong?

Upvotes: 1

Views: 1769

Answers (2)

pacmanito
pacmanito

Reputation: 104

Supposedly you've already forgot about this question 99 times, but I will provide an answer for those who will find it via search.

WC has a woocommerce_cart_item_backorder_notification filter which provides a simpler solution. You just have add to your functions.php following code:

function custom_cart_item_backorder_notification( $html, $product_id ){
    $html = '<p class="backorder_notification">' . esc_html__( 'Your backorder notification text', 'woocommerce' ) . '</p>';
    return $html;
}
add_filter( 'woocommerce_cart_item_backorder_notification', 'custom_cart_item_backorder_notification', 10, 2 );

Although this way you won't be able to show a notification for not backordered product.

Upvotes: 0

LoicTheAztec
LoicTheAztec

Reputation: 253738

May be you should try this custom function hooked in woocommerce_before_cart based on your code (conditions) to display your custom notifications. There was a lot of little mistakes in your code.

Here is that code:

add_action( 'woocommerce_before_cart', 'custom_backorders_notifications' );
function custom_backorders_notifications(){

    $backorders_notification = false;

    foreach( WC()->cart->get_cart() as $cart_item ):
        $product_id = $cart_item['product_id']; // get the product ID for the current cart item
        $item_qty = $cart_item['quantity']; // get the cart item quantity
        $product = wc_get_product($cart_item['product_id']); // get the $product OBJECT

        // Backorder notification with your existing (untested) condition
        if ( $product->backorders_require_notification() && $product->is_on_backorder( $item_qty ) ){
            $backorders_notification = true;
            break;
        }

    endforeach;

    if ( $backorders_notification )
        echo '<p class="backorder_notification">' . esc_html__( "Available in 3-5 Working Days", "teencode" ) . '</p>';
    else
        echo '<p class="backorder_notification">' . esc_html__( "Available Next Day", "teencode" ) . '</p>';
}

This code is tested and works.

Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.

Upvotes: 2

Related Questions