【问题标题】:add filter to change specific part of function添加过滤器以更改功能的特定部分
【发布时间】:2017-01-30 04:32:20
【问题描述】:

我正在尝试从 wc_cart_functions.php 文件中的 woocommerce 按钮中删除一个类。 wc_add_to_cart_message 函数中有一个字符串插入此字符串:

<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 ) . ' × ' : '' ) . sprintf( _x( '“%s”', '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 ) );
}

我试图创建一个过滤器来替换该特定字符串,因为它出现了两次,并且在这两种情况下我都希望删除该类。但这似乎不起作用:

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

按原样设置此过滤器后,我仍然可以看到具有相同未更改类的按钮。有什么想法吗?

【问题讨论】:

    标签: php wordpress woocommerce filtering


    【解决方案1】:

    您的过滤器希望找到%s,但它已被之前对sprintf 的调用所取代:这些%s 不再存在。

    您可以尝试使用正则表达式:

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

    [^&gt;]+ 部分表示:任何不包含&gt;a 标记结束)的字符序列。

    $1 表示:括号之间匹配的内容。它可能类似于&lt;a href="http://example.com"

    【讨论】:

    【解决方案2】:

    你可以试试这个:

    add_filter( 'wc_add_to_cart_message', 'add_to_cart_mod');
    
    function add_to_cart_mod( $message ) {  
        // 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">%s</a> %s', esc_url( $return_to ), esc_html__( 'Continue Shopping', 'woocommerce' ), esc_html( $added_text ) );
        } else {
            $message   = sprintf( '<a href="%s" class="button">%s</a> %s', esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View Cart', 'woocommerce' ), esc_html( $added_text ) );
        }
    
        return $message;
    }
    

    【讨论】:

    • 嘿,安迪,这会删除代码的 $ added_text 部分,导致只有按钮,后面没有文本。但是按钮类有效
    猜你喜欢
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 2018-03-21
    • 2012-07-25
    • 1970-01-01
    • 2022-08-20
    相关资源
    最近更新 更多