【问题标题】:remove/add filter to Wordpress Function fil删除/添加过滤器到 Wordpress 函数文件
【发布时间】:2012-08-14 21:35:57
【问题描述】:

我在尝试通过我的主题 functions.php 文件更改消息的输出时遇到问题。

这是woocommerce插件提供的代码:

function woocommerce_add_to_cart_message() {
    global $woocommerce;

    if (get_option('woocommerce_cart_redirect_after_add')=='yes') :

        $return_to  = (wp_get_referer()) ? wp_get_referer() : home_url();

        $message    = sprintf('<a href="%s" class="button">%s</a> %s', $return_to, __('Continue Shopping;', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );

    else :

        $message    = sprintf('<a href="%s" class="button">%s</a> %s', get_permalink(woocommerce_get_page_id('cart')), __('View Cart &rarr;', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );

    endif;

    $woocommerce->add_message( apply_filter('woocommerce_add_to_cart_message', $message) );
}

我正在尝试更改按钮消息,但我不确定如何执行此操作。

我一直在阅读 WP 法典,但不幸的是,我认为我不太了解它是如何工作的。这是我尝试过的:

function remove_woocommerce_add_to_cart_message() {
    remove_filter('woocommerce_add_to_cart_message', $message) ;
}

function woocommerce_add_to_cart_message_edited() {
    global $woocommerce;

    if (get_option('woocommerce_cart_redirect_after_add')=='yes') :

        $return_to  = (wp_get_referer()) ? wp_get_referer() : home_url();

        $message    = sprintf('<a href="%s" class="button">%s</a> %s', $return_to, __('Continue Shopping;', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );

    else :

        $message    = sprintf('<a href="%s" class="button">%s</a> %s', get_permalink(woocommerce_get_page_id('cart')), __('NEW CART MESSAGE', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );

    endif;

    $woocommerce->add_message( apply_filter('woocommerce_add_to_cart_message', $message) );
}

所以,我尝试删除输出消息的过滤器,然后声明它,但它似乎不起作用。

任何帮助将不胜感激!

【问题讨论】:

  • 你在任何地方调用你的函数吗?
  • 我猜不是?我真的不确定。主题是用一堆钩子设置的,所以我需要绑定它们的功能。这就是我试图移除过滤器的方法。但我显然做得不对。

标签: php wordpress woocommerce


【解决方案1】:

当代码声明 apply_filter('woocommerce_add_to_cart_message', $message) 时,代码将查找添加到该挂钩的任何过滤器,并一次通过一个过滤器。

你可以创建一个函数,任意命名,然后像这样将它添加到那个钩子中:

function my_filer_function( $message )
{
    // Here you should modify $message as you want, and then return it.
    $newButtonString = 'NEW BUTTON STRING';
    $replaceString = '<a$1class="button">' . $newButtonString .'</a>';
    $message = preg_replace('#<a(.*?)class="button">(.*?)</a>#', $replaceString, $message);
    return $message;
}
// Then add the function to that filter hook and prioritize it last
add_filter( 'woocommerce_add_to_cart_message', 'my_filer_function', 999);

【讨论】:

  • 太棒了。谢谢!我试试看。
  • 这并没有像我希望的那样工作。这会更改显示“产品已成功添加到购物车”的区域 - 我要特别更改的是按钮文本 - 显示“查看购物车”的区域 - 所以我所做的只是从以前的功能,它的工作原理。感谢您的帮助!
  • 太棒了。如果你愿意,你也可以像上面编辑过的代码那样做。
【解决方案2】:
// Output success messages
    if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) :

        $return_to  = apply_filters( 'woocommerce_continue_shopping_redirect', wp_get_referer() ? wp_get_referer() : home_url() );


$message    = sprintf('%s',$added_text);
    else :


$message    = sprintf('%s',$added_text);

    endif;

以上代码我已对其进行了更改,请检查可能对您有帮助

【讨论】:

    猜你喜欢
    • 2012-01-22
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 2021-07-09
    • 1970-01-01
    • 2013-11-08
    • 2022-03-06
    相关资源
    最近更新 更多