Search code examples
phpwordpresswoocommercecartnotice

Change wc_empty_cart_message function in WooCommerce 3.1


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?


Solution

  • 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 );