【问题标题】:wc_print_notices is not displaying any messages in woocommerce pages?wc_print_notices 没有在 woocommerce 页面中显示任何消息?
【发布时间】:2017-08-23 07:21:08
【问题描述】:

我为商店、购物车和单页使用了默认的 woocommerce 模板。 我也没有删除任何钩子,但我也没有收到任何消息。 有什么想法吗?

add_action( 'woocommerce_before_single_product',   'Cusotm_wc_print_notices', 10 );
function Cusotm_wc_print_notices()
{
   echo 'Hook is working fine';
}

我收到这条消息“Hook 工作正常”,但不是 wc_print_notices();。

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    我不确定是什么问题。您的问题需要更多详细信息。话虽如此,您能否尝试将此代码添加到您当前主题的functions.php中。

    add_action( 'template_redirect', 'test' );
    function test() {
    
        wc_add_notice( __( 'Sorry there was a problem.', 'woocommerce' ), 'error' );
    
    }
    

    如果它有什么作用,请告诉我。


    更新

    如果你有这样的事情:

    add_action( 'woocommerce_before_single_product',   'Cusotm_wc_print_notices', 10 );
    function Cusotm_wc_print_notices()
    {
       $notices = WC()->session->get('wc_notices');
       print_r($notices);
    }
    

    它不会起作用,因为一旦调用了wc_print_notices()$notices 就会为空。

    尝试更改优先级,您会有所收获。应该是这样的:

    add_action( 'woocommerce_before_single_product',   'Cusotm_wc_print_notices', 9 ); 
    


    使用低于 10 的优先级。因为 WooCommerce 使用的是 10。

    add_action( 'woocommerce_before_single_product', 'wc_print_notices', 10 );
    

    【讨论】:

    • 上面的代码显示“Sorry there was a problem”的错误信息。并且工作正常,但我无法显示 wc_print_notices()。
    • 其实 $notices = WC()->session->get('wc_notices', array());没有显示任何东西,当我执行 var_dump($notices) 时,它显示 => array(0) { }。
    • wc_print_notices 被调用一次时,你的$notices 将一无所获,因为一切都会被清除。
    • 在调用前就可以得到。
    • 示例,尝试在function Cusotm_wc_print_notices(){} 中执行add_action( 'woocommerce_before_single_product', 'Cusotm_wc_print_notices', 9 );$notices = WC()->session->get('wc_notices');。请注意,我使用 9 作为优先级。
    猜你喜欢
    • 2021-06-07
    • 2019-06-16
    • 1970-01-01
    • 2023-02-01
    • 2016-02-24
    • 2020-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多