试试这个:
add_filter( 'wc_add_to_cart_message_html', function( $message ) {
return preg_replace(
// Or use ^(<a href=".+?" class="button wc-forward">.+?</a>) *(.+)
'#^(<a href=".+?" class="button wc-forward">.+?</a>) +(.+)#',
'$2 $1',
trim( $message )
);
} );
如果没有插件(或者如果主题没有)修改默认 HTML 标记(如您在 line #121 和 line #123 上看到的那样),上面的代码应该可以正常工作。
[EDIT #3] 或者,您可以完全重新生成消息/HTML,如下例所示:
add_filter( 'wc_add_to_cart_message_html', 'my_wc_add_to_cart_message_html', 10, 2 );
function my_wc_add_to_cart_message_html( $message, $products ) {
$titles = array();
$count = 0;
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 - the "View cart" or "Continue shopping" link comes after the product link/info.
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( '%s <a href="%s" class="button wc-forward">%s</a>', esc_html( $added_text ), esc_url( $return_to ), esc_html__( 'Continue shopping', 'woocommerce' ) );
} else {
$message = sprintf( '%s <a href="%s" class="button wc-forward">%s</a>', esc_html( $added_text ), esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View cart', 'woocommerce' ) );
}
return $message;
}
希望有帮助!