在Woocommerce 3中自定义添加到购物车消息 [英] Customize add to cart message in Woocommerce 3

查看:217
本文介绍了在Woocommerce 3中自定义添加到购物车消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改已将"[产品名称]"添加到您的购物篮"提示,说已将x项添加到您的购物篮".

I would like to change the '"[product name]" has been added to your basket' prompt to say 'x item(s) has/have been added to your basket'.

此主题解释了如何编辑添加到购物车消息,但是我找不到有关可以使用的变量的任何信息.

This thread explains how to edit the add to cart message, but I cannot find any information about the variables which can be used.

如何显示添加的产品数量而不是名称?

How can I show the number of products added rather than the name?

推荐答案

自Woocommerce 3起,过滤器挂钩wc_add_to_cart_message已过时,现在已由

Since Woocommerce 3, the filter hook wc_add_to_cart_message is obsolete and it's now replaced by wc_add_to_cart_message_html

函数可用变量的参数有两个:

The function usable variables arguments are two:

  • $message是输出的字符串消息
  • $products是包含产品ID/数量对(键/值) 的索引数组.
  • $message that is the outputed string message
  • $products that is an indexed array containing the product Id / quantity pairs (key / value).

要将默认 "{qty} x {product-name}已添加到购物车" 消息更改为:

To change the default "{qty} x {product-name} has(ve) been added to your cart" message to:

{qty}个项目已添加到您的购物篮.

{qty} item(s) has/have been added to your basket.

您将使用以下内容:

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 ) {
    $count = 0;
    foreach ( $products as $product_id => $qty ) {
        $count += $qty;
    }
    // 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;
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试并可以正常工作.

Code goes in function.php file of your active child theme (or active theme). tested and works.

这篇关于在Woocommerce 3中自定义添加到购物车消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆