在 Woocommerce 中更改 "You cannot add another (product) to your cart" 通知
Change "You cannot add another (product) to your cart" notice in Woocommerce
当您尝试将其他产品添加到购物车中标记为单独出售时,我找不到更改 Woocommerce 默认消息的方法。
我发现这是您在将产品添加到购物车时编辑默认成功消息的方式:
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> <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;
}
您可以使用 gettext
过滤器挂钩来更改位于 WC_Cart
add_to_cart()
method:
中的此通知
add_filter( 'gettext', 'change_specific_add_to_cart_notice', 10, 3 );
add_filter( 'ngettext', 'change_specific_add_to_cart_notice', 10, 3 );
function change_specific_add_to_cart_notice( $translated, $text, $domain ) {
if( $text === 'You cannot add another "%s" to your cart.' && $domain === 'woocommerce' && ! is_admin() ){
// Replacement text (where "%s" is the dynamic product name)
$translated = __( 'It is not possible to add again "%s"', $domain );
}
return $translated;
}
代码在您的活动子主题(或活动主题)的 function.php 文件中继续。已测试并有效。
当您尝试将其他产品添加到购物车中标记为单独出售时,我找不到更改 Woocommerce 默认消息的方法。
我发现这是您在将产品添加到购物车时编辑默认成功消息的方式:
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> <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;
}
您可以使用 gettext
过滤器挂钩来更改位于 WC_Cart
add_to_cart()
method:
add_filter( 'gettext', 'change_specific_add_to_cart_notice', 10, 3 );
add_filter( 'ngettext', 'change_specific_add_to_cart_notice', 10, 3 );
function change_specific_add_to_cart_notice( $translated, $text, $domain ) {
if( $text === 'You cannot add another "%s" to your cart.' && $domain === 'woocommerce' && ! is_admin() ){
// Replacement text (where "%s" is the dynamic product name)
$translated = __( 'It is not possible to add again "%s"', $domain );
}
return $translated;
}
代码在您的活动子主题(或活动主题)的 function.php 文件中继续。已测试并有效。