我试图修改在向购物车中添加产品和/或通过链接到woocommerce_add_message来更新购物车时显示的消息。它根本没有显示任何东西,我想知道为什么。
我尝试过echo,我尝试过return__(,这是代码:
add_filter('woocommerce_add_message', 'change_cart_message', 10);
function change_cart_message() {
    $ncst = WC()->cart->subtotal;
    if ( is_checkout() ) {
        echo 'Your new order subtotal is: '.$ncst.'. <a style="color: green;" href="#customer_details">Ready to checkout?</a>';
    }
    elseif ( is_product() ) {
        echo 'Your new order subtotal is: '.$ncst.'. <a style="color: green;" href="'.wc_get_checkout_url().'">Ready to checkout?</a>';
    }
    else {
        echo 'Your new order subtotal is: '.$ncst.'. <a style="color: green;" href="'.wc_get_checkout_url().'">Ready to checkout?</a>';
    } 
}我做错了什么?
发布于 2019-01-25 14:27:06
重要注意事项:过滤器钩子总是要返回一个变量参数。
使用筛选器钩子时,始终需要返回筛选值参数(但不回显)…
此外,您的代码也可以简化和压缩:
add_filter('woocommerce_add_message', 'change_cart_message', 10, 1 );
function change_cart_message( $message ) {
    $subtotal = WC()->cart->subtotal;
    $href = is_checkout() ? '#customer_details' : wc_get_checkout_url();
    return sprintf(  __("Your new order subtotal is: %s. %s"), wc_price($subtotal),
        '<a class="button alt" href="'.$href.'">' . __("Ready to checkout?") . '</a>' );
}代码在您的活动子主题(或活动主题)的function.php文件中。测试和工作。

https://stackoverflow.com/questions/54366556
复制相似问题