删除/添加过滤器到Wordpress函数文件

时间:2012-08-18 22:27:59

标签: php wordpress woocommerce

我在尝试通过主题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 codex,但不幸的是我不认为我完全理解这是如何工作的。这是我尝试过的:

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

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

任何帮助都会受到赞赏!

2 个答案:

答案 0 :(得分:2)

当代码声明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);

答案 1 :(得分:0)

// 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;

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