doufan6033 2016-09-21 19:13
浏览 54
已采纳

添加过滤器以更改功能的特定部分

I am trying to remove a class from a button in woocommerce that is in the wc_cart_functions.php file. There is a string in the wc_add_to_cart_message function which inserts this string:

<a href="%s" class="button wc-forward">%s</a> %s

function wc_add_to_cart_message( $products, $show_qty = false ) {
$titles = array();
$count  = 0;

if ( ! is_array( $products ) ) {
    $products = array( $products );
    $show_qty = false;
}

if ( ! $show_qty ) {
    $products = array_fill_keys( array_keys( $products ), 1 );
}

foreach ( $products as $product_id => $qty ) {
    $titles[] = ( $qty > 1 ? absint( $qty ) . ' &times; ' : '' ) . sprintf( _x( '&ldquo;%s&rdquo;', 'Item name in quotes', 'woocommerce' ), strip_tags( get_the_title( $product_id ) ) );
    $count += $qty;
}

$titles     = array_filter( $titles );
$added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', $count, 'woocommerce' ), wc_format_list_of_items( $titles ) );

// Output success messages
if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
    $return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) );
    $message   = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), esc_html__( 'Continue Shopping', 'woocommerce' ), esc_html( $added_text ) );
} else {
    $message   = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View Cart', 'woocommerce' ), esc_html( $added_text ) );
}

wc_add_notice( apply_filters( 'wc_add_to_cart_message', $message, $product_id ) );
}

I tried to create a filter which would just replace that specific string, since it appears twice and in both instances I want the class removed. This does not seem to work though:

add_filter( 'wc_add_to_cart_message', 'add_to_cart_mod');
 function add_to_cart_mod($message) {
    $message = str_replace ( '<a href="%s" class="button wc-forward">%s</a> %s' , '<a href="%s" class="button">%s</a> %s', $message );
 return $message;
}

With this filter set as it is, I still see the button with the same unchanged class. Any thoughts?

  • 写回答

2条回答 默认 最新

  • douben1891 2016-09-21 19:23
    关注

    Your filter expects to find %s, but that was replaced by the previous call to sprintf: these %s are not there any more.

    You could try with a regular expression:

    $message = preg_replace ('/(<a [^>]+ )class="button wc-forward"/', 
                             '$1class="button"', $message );    
    

    The [^>]+ part means: any sequence of characters that does not contain a > (end of a tag).

    $1 means: whatever was matched between the parentheses. It could be something like <a href="http://example.com".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制