在Woocommerce中更改“您不能将其他(产品)添加到购物车”通知

时间:2019-03-25 22:59:10

标签: php wordpress woocommerce product cart

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

1 个答案:

答案 0 :(得分:1)

您可以使用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文件上。经过测试,可以正常工作。