gpt4 book ai didi

php - 更改 Woocommerce 中的 "You cannot add another (product) to your cart"通知

转载 作者:行者123 更新时间:2023-12-04 00:29:33 25 4
gpt4 key购买 nike

当您尝试将其他产品添加到购物车中标记为单独出售时,我找不到更改 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>&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;
}

最佳答案

您可以使用gettext 过滤器钩子(Hook)来更改位于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 文件中。经过测试并有效。

关于php - 更改 Woocommerce 中的 "You cannot add another (product) to your cart"通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55347571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com