Wordpress Woocommerce,如何编辑;“添加到购物车中”;消息

Wordpress Woocommerce,如何编辑;“添加到购物车中”;消息,wordpress,woocommerce,e-commerce,hook,edit,Wordpress,Woocommerce,E Commerce,Hook,Edit,单击“添加到购物车”按钮时,Woocommerce会显示消息“查看购物车,我想编辑此消息,实际编辑所有范围,放置一些图标等…”。您是否尝试过以下过滤器 function your_add_to_cart_message() { if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) : $message = sprintf( '%s<a href="%s" class="your-style">%

单击“添加到购物车”按钮时,Woocommerce会显示消息“查看购物车,我想编辑此消息,实际编辑所有范围,放置一些图标等…”。

您是否尝试过以下过滤器

function your_add_to_cart_message() {
if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) :
    $message = sprintf( '%s<a href="%s" class="your-style">%s</a>', __( 'Successfully added to cart.', 'woocommerce' ), esc_url( get_permalink( woocommerce_get_page_id( 'shop' ) ) ), __( 'Continue Shopping', 'woocommerce' ) );
else :
    $message = sprintf( '%s<a href="%s" class="your-class">%s</a>', __( 'Successfully added to cart.' , 'woocommerce' ), esc_url( get_permalink( woocommerce_get_page_id( 'cart' ) ) ), __( 'View Cart', 'woocommerce' ) );
endif;
return $message;
}
add_filter( 'wc_add_to_cart_message', 'your_add_to_cart_message' );

如果查看
add to cart.js
它会在向购物车添加产品时触发
added to_cart
。我沉迷其中,做了这件事

jQuery(document.body).on("added_to_cart", function( data ) {
    jQuery('button.added').nextAll().remove();
    jQuery('button.added').after(' <span style="text-align:center;display:block;" class="cart_updated_ajax"><a href="' + wc_add_to_cart_params.cart_url + '" title="' +
                            wc_add_to_cart_params.i18n_view_cart + '">Cart Updated</a></span>');
});
jQuery(document.body).on(“添加到购物车”,函数(数据){
jQuery('button.added').nextAll().remove();
jQuery('button.added')。在('')之后;
});
在这里,您可以在产品添加到购物车后添加任何内容


希望有帮助

在theme/functions.php中添加一个过滤器。下面的代码仅覆盖现有的$message。这将用一个几乎相同的消息覆盖$message,该消息在“checkout”链接前面加上一个前缀

确保返回$message

当然,您可以只修改现有消息,因为整个消息是通过第一个参数或$message变量作为字符串传递的

add_filter ( 'wc_add_to_cart_message', 'wc_add_to_cart_message_filter', 10, 2 );
function wc_add_to_cart_message_filter($message, $product_id = null) {
    $titles[] = get_the_title( $product_id );

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

    $message = sprintf( '%s <a href="%s" class="button">%s</a>&nbsp;<a href="%s" class="button">%s</a>',
                    esc_html( $added_text ),
                    esc_url( wc_get_page_permalink( 'checkout' ) ),
                    esc_html__( 'Checkout', 'woocommerce' ),
                    esc_url( wc_get_page_permalink( 'cart' ) ),
                    esc_html__( 'View Cart', 'woocommerce' ));

    return $message;
}
add_过滤器('wc_add_to_cart_message','wc_add_to_cart_message_filter',10,2);
函数wc\u add\u to\u cart\u message\u filter($message,$product\u id=null){
$titles[]=获取标题($product\u id);
$titles=数组过滤器($titles);
$added_text=sprintf(_n('%s已添加到您的购物车中,'%s已添加到您的购物车中,'%s已添加到您的购物车中,''sizeof($titles),'woocommerce'),wc_格式_项目列表($titles));
$message=sprintf(“%s”,
esc_html($added_text),
esc_url(wc_get_page_permalink('checkout')),
esc_html_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,
esc_url(wc_get_page_permalink('cart')),
esc_html_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
返回$message;
}
在Woocommerce 3.0中,“wc\u add\u to\u cart\u message”已过时,不再有效。因此,尽管@zmonteca的回答是正确的,但Woocommerce 3.0不再有效

只需将“wc_add_to_cart_message”替换为“wc_add_to_cart_message_html”,然后查看。。。工作

add_filter ( 'wc_add_to_cart_message', 'wc_add_to_cart_message_filter', 10, 2 );
function wc_add_to_cart_message_filter($message, $product_id = null) {
$titles[] = get_the_title( $product_id );

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

$message = sprintf( '%s <a href="%s" class="button">%s</a>&nbsp;<a href="%s" class="button">%s</a>',
                esc_html( $added_text ),
                esc_url( wc_get_page_permalink( 'checkout' ) ),
                esc_html__( 'Checkout', 'woocommerce' ),
                esc_url( wc_get_page_permalink( 'cart' ) ),
                esc_html__( 'View Cart', 'woocommerce' ));

return $message;}
add_过滤器('wc_add_to_cart_message','wc_add_to_cart_message_filter',10,2);
函数wc\u add\u to\u cart\u message\u filter($message,$product\u id=null){
$titles[]=获取标题($product\u id);
$titles=数组过滤器($titles);
$added_text=sprintf(_n('%s已添加到您的购物车中,'%s已添加到您的购物车中,'%s已添加到您的购物车中,''sizeof($titles),'woocommerce'),wc_格式_项目列表($titles));
$message=sprintf(“%s”,
esc_html($added_text),
esc_url(wc_get_page_permalink('checkout')),
esc_html_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,
esc_url(wc_get_page_permalink('cart')),
esc_html_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
返回$message;}

@Dante是正确的,@BradleyD提供的解决方案不适用于商店页面上的ajax\u add\u to\u cart

@Abstract提供的解决方案正在按预期工作。我也在使用他的解决方案

另一种jQuery方法是侦听document对象上的ajaxSuccess事件,并对单击的按钮进行所需的修改

类似的方法应该会奏效:

$(document).ajaxSuccess(function(event, xhr, settings) {
    if (settings.url.indexOf('?wc-ajax=add_to_cart') !== -1) {
        // You can find the clicked button element under the event.target.activeElement
        // Than you can do whatever you want here. Add new html element and text, etc.
    }

});
2017-2019-适用于Woocommerce 3+(处理添加到购物车中的多个产品)

由筛选器挂钩替换,第二个函数参数已更改为
$products
(而不是
$product\u id

您可以在此挂钩函数内对代码进行更改,如:

add_过滤器('wc_add_to_cart_message_html','custom_add_to_cart_message_html',10,2);
函数自定义\添加\到\购物车\消息\ html($message$products){
$titles=array();
$count=0;
foreach($product as$product\U id=>$qty){
$titles[]=($qty>1?absint($qty)。×;:“”).sprintf('ux('s&rdquo;'s&rdquo;','Item name in quotes','woocommerce'),strip_标签(获取_title('product_id));
$count+=$qty;
}
$titles=数组过滤器($titles);
$added_text=sprintf(_n('%s已添加到您的购物车中,'%s已添加到您的购物车中,$count,'woocommerce'),wc_格式\u项目列表($titles));
//自定义消息就在下面
$added\u text=sprintf(\u n(“%s项有%s”,“%s项有%s”,$count,“woocommerce”),
$count,_uu(“已添加到您的篮子中。”,“woocommerce”);
//输出成功消息
if('yes'==获取选项('woocommerce\u cart\u redirect\u after\u add')){
$return\u to=apply\u filters('woocommerce\u continue\u shopping\u redirect',wc\u get\u raw\u referer()?wp\u validate\u redirect(wc\u get\u raw\u referer(),false):wc\u get\u page\u permalink('shop');
$message=sprintf('%s',esc_url($return_to),esc_html('Continue shopping','woocommerce'),esc_html($added_text));
}否则{
$message=sprintf('%s',esc_url(wc_get_page_permalink('cart')),esc_html_('View cart','woocommerce'),esc_html($added_text));
}
返回$message;
}
相关线程(适用于Woocommerce 3+):


您想更改AJAX添加到购物车或产品页面上的消息?是的,此消息只显示“查看购物车”我想编辑此消息,我曾尝试使用css“before”编辑该类,但结果并不太酷…此功能很好,但我想更改产品列表上的消息,当单击AJAX添加到购物车时,此功能仅更改了单个产品页面上的消息。此解决方案不再有效。WC 3.0要求使用“WC_add_to_cart_message_html”而不是“WC_add_to_cart_message”和“WC_add_to_cart_message_filter”IDC规则说什么,谢谢Loic,这是一个很好的解决方案。
$(document).ajaxSuccess(function(event, xhr, settings) {
    if (settings.url.indexOf('?wc-ajax=add_to_cart') !== -1) {
        // You can find the clicked button element under the event.target.activeElement
        // Than you can do whatever you want here. Add new html element and text, etc.
    }

});
add_filter( 'wc_add_to_cart_message_html', 'custom_add_to_cart_message_html', 10, 2 );
function custom_add_to_cart_message_html( $message, $products ) {
    $titles = array();
    $count  = 0;

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

    // The custom message is just below
    $added_text = sprintf( _n("%s item has %s", "%s items have %s", $count, "woocommerce" ),
        $count, __("been added to your basket.", "woocommerce") );

    // 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 ) );
    }
    return $message;
}