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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25880460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:01:03  来源:igfitidea点击:

Woocommerce, How to edit the "added to cart" message

wordpresswoocommercee-commercehookedit

提问by Dante

When click on add to cart button, the Woocommerce shows the message, view cart, I want to edit this message, actually edit all the span, put some icon etc...

当单击添加到购物车按钮时,Woocommerce 会显示消息,查看购物车,我想编辑此消息,实际编辑所有跨度,放置一些图标等...

回答by zmonteca

Add a filter to your theme/functions.php. The code below just overrides the existing $message. This overwrites $message with an nearly identical one that prepends a "checkout" link to the message.

将过滤器添加到您的主题/functions.php。下面的代码只是覆盖了现有的 $message。这会用几乎相同的消息覆盖 $message ,该消息在消息之前添加了“结帐”链接。

Make sure you return the $message.

确保您返回 $message。

You can of course just modify the existing message, as the entire thing is passed as a string via the first param or $message var.

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

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

回答by BradleyD

Have you tried a filter like the following

你有没有试过像下面这样的过滤器

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

In reply to the ajax message update, try a translation function like:

回复ajax消息更新,试试翻译功能如下:

function your_woo_ajax_solution( $translation, $text, $domain ) {
  if ( $domain == 'woocommerce' ) { // your domain name
    if ( $text == 'View Cart' ) { // current text that shows
        $translation = 'Basket updated.'; // The text that you would like to show
    }
  }

  return $translation;
}
add_filter( 'gettext', 'your_woo_ajax_solution', 10, 3 );

回答by LoicTheAztec

2017 - 2019 - For Woocommerce 3+(handling multiple products added to cart)

2017 - 2019 - 对于 Woocommerce 3+ (处理添加到购物车的多个产品)

Replaced by wc_add_to_cart_message_htmlfilter hook, the 2nd function argument has changed to $products(instead of $product_id)

替换为wc_add_to_cart_message_html过滤器钩子,第二个函数参数已更改为$products(而不是$product_id……

You can make changes on the code inside this hooked function, like in this thread:

您可以更改此挂钩函数内的代码,例如在此线程中

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

Related threads (for Woocommerce 3+):

相关主题(适用于 Woocommerce 3+):

回答by Abstract

If you look at add-to-cart.jsit fires a trigger added_to_carton adding a product to cart. I hooked into that and did this

如果您查看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>');
});

Here you can add anything after product is added to cart.

将产品添加到购物车后,您可以在此处添加任何内容。

Hope that helps!

希望有帮助!

回答by Herzling

In Woocommerce 3.0 "wc_add_to_cart_message" is obsolete and no longer works.So while the answer by @zmonteca was ok, is no longer working on the Woocommerce 3.0

在 Woocommerce 3.0 中,“wc_add_to_cart_message”已过时且不再有效。因此,虽然@zmonteca 的回答没问题,但不再适用于 Woocommerce 3.0

Just replace "wc_add_to_cart_message" with "wc_add_to_cart_message_html" and voile... works.

只需将“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;}

回答by kostadinovstoil

@Dante is correct, the solutions provided by @BradleyD won't work for ajax_add_to_cart on shop page.

@Dante 是正确的,@BradleyD 提供的解决方案不适用于商店页面上的 ajax_add_to_cart。

The solution provided by @Abstract is working as expected. I am also using his solution.

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

Another jQuery approach is to to listen for the the ajaxSuccess event on the document object and do the desired modifications for the clicked button.

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

Something like that should work:

这样的事情应该工作:

$(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.
    }

});