5

I've been trying to change my layout for the empty-cart message. I've removed the action, and try to replace it.

I'd like to change the htm structure output from:

<p class="empty-cart"></p> 

to:

<div class="col-12 offset-md-1 col-md-10"><p class="empty-cart"></p></div>

My actual code (in functions.php file of my theme):

/** Change the output for empty-cart within a div */
remove_action( 'wc_empty_cart_message', 'wc_empty_cart_message', 10 );
add_action( 'wc_empty_cart_message', 'wc_empty_cart_message', 10 );

function custom_wc_empty_cart_message() {
echo '<div class="col-12 offset-md-1 col-md-10"><p class="cart-empty">'
. wp_kses_post( apply_filters( 'wc_empty_cart_message', __( 'Your cart is currently empty.', 'woocommerce' ) ) ) . '</p></div>';
}

But this code doesn't work. Does anyone has a suggestions on how to make this work?

LoicTheAztec
  • 184,753
  • 20
  • 224
  • 275
Onno V.
  • 97
  • 1
  • 12

1 Answers1

11

Here below is the correct way to make it work:

remove_action( 'woocommerce_cart_is_empty', 'wc_empty_cart_message', 10 );
add_action( 'woocommerce_cart_is_empty', 'custom_empty_cart_message', 10 );

function custom_empty_cart_message() {
    $html  = '<div class="col-12 offset-md-1 col-md-10"><p class="cart-empty">';
    $html .= wp_kses_post( apply_filters( 'wc_empty_cart_message', __( 'Your cart is currently empty.', 'woocommerce' ) ) );
    echo $html . '</p></div>';
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

To remove empty cart message use just:

remove_action( 'woocommerce_cart_is_empty', 'wc_empty_cart_message', 10 );
LoicTheAztec
  • 184,753
  • 20
  • 224
  • 275
  • Thanks! @LoicTheAztec I've tried the code, but when i put it in my function.php i've got the following message: "Warning: Use of undefined constant html - assumed 'html' (this will throw an Error in a future version of PHP) in /Applications/MAMP/htdocs/wp-content/themes/Amplify/functions.php on line 172 html" – Onno V. Jul 10 '18 at 07:57
  • @OnnoV. Sorry I have a little mistake in my code. I have just updated it… Try it again please. It will work. – LoicTheAztec Jul 10 '18 at 08:11
  • Can we add more then text? like $html .= wp_kses_post( apply_filters( 'wc_empty_cart_message', __( 'cart empty', 'woocommerce' ) ) ); $html .= wp_kses_post( apply_filters( 'wc_empty_cart_message', __( 'There is nothing in your cart, Lets add some items.', 'woocommerce' ) ) ); – user9437856 Jan 27 '21 at 13:02
  • @user9437856 Try it yourself and you will have the answer… But the best thing is to use a unique text, so why you don't merge both texts in one? – LoicTheAztec Jan 27 '21 at 13:46
  • Yes, I found the solution. I have an h2 tag and p tag that the response I am using. – user9437856 Jan 27 '21 at 14:21