Woocommerce, How to edit the “added to cart” message

前端 未结 6 871
感情败类
感情败类 2021-02-08 11:02

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...

6条回答
  •  执笔经年
    2021-02-08 11:32

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

    Replaced by wc_add_to_cart_message_html filter hook, the 2nd function argument has changed to $products (instead of $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 ) . ' × ' : '' ) . sprintf( _x( '“%s”', '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( '%s %s', esc_url( $return_to ), esc_html__( 'Continue shopping', 'woocommerce' ), esc_html( $added_text ) );
        } else {
            $message   = sprintf( '%s %s', esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View cart', 'woocommerce' ), esc_html( $added_text ) );
        }
        return $message;
    }
    

    Related threads (for Woocommerce 3+):

    • Hide Added to Cart message in Woocommerce
    • Customizing add-to-cart messages based on the product IDs in WooCommerce 3
    • Customize add to cart message in Woocommerce 3

提交回复
热议问题