Change wc_empty_cart_message function in Woocommerce 3.1

 ̄綄美尐妖づ 提交于 2020-06-10 05:16:18

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/51241826/change-wc-empty-cart-message-function-in-woocommerce-3-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!