基于WooCommerce页面的不同消息 [英] Different Messages Based on WooCommerce Page

查看:84
本文介绍了基于WooCommerce页面的不同消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更改将产品添加到购物车和/或通过挂接到woocommerce_add_message来更新购物车时显示的消息.它什么都没显示,我想知道为什么.

I am trying to alter messages displayed when adding an a product to cart and/ or updating the cart by hooking in to the woocommerce_add_message. It's not showing anything at all and I'm wondering why.

我尝试过echo并且我尝试过return__( 这是代码:

I've tried echo and I've tried return__( Here's the code:

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>';
    } 
}

我在做什么错了?

推荐答案

重要说明:过滤器挂钩始终具有要返回的可变参数.

使用过滤器挂钩时,始终需要返回过滤后的值参数(而不是回显它)……

When using a filter hook, you need always to return the filtered value argument (but not to echo it)…

还可以简化和压缩您的代码:

Also your code can be simplified and compacted:

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文件中.经过测试,可以正常工作.

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

这篇关于基于WooCommerce页面的不同消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆