1

我在尝试通过我的主题 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) );
}

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

任何帮助将不胜感激!

4

2 回答 2

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);
于 2012-08-18T22:50:57.483 回答
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;

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

于 2013-08-01T06:40:02.497 回答