• Resolved stevehoo

    (@stevehoo)


    Hi, I’m using the filter given on stackoverflow the get at the message shown when something is added to the cart. It works well.

    However, I want to add the variation to the product’s name. Instead of “Red T-shirt has been added to your basket.” I need “Red T-shirt large has been added to your basket.”

    Can any of you clever people help? Here’s the filter:

    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;
    }

    Be happy for ever,
    Steve

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator James Huff

    (@macmanx)

    I recommend asking at https://www.ads-software.com/support/plugin/woocommerce so the plugin’s developers and support community can help you with this.

    Thread Starter stevehoo

    (@stevehoo)

    OK James. Newbie apologies.

    Moderator James Huff

    (@macmanx)

    No worries. ??

    Thread Starter stevehoo

    (@stevehoo)

    I didn’t. I dig through Google, deeper this time, found this. And made this. It works for me but I’ve taken much of the error checking out.
    Hope it helps others.

    add_filter('wc_add_to_cart_message', 'handler_function_name', 10, 2);
    function handler_function_name($message, $product_id) {
         $variation_id = isset( $_REQUEST[ 'variation_id' ] ) ? $_REQUEST[ 'variation_id' ] : null;
        // Collect the product, product variations and attributes
                $var_product = get_product( $variation_id );
                $variations = $var_product->get_variation_attributes();
                $attributes = $var_product->get_attributes();
                $name_output = null;
        if ( is_array( $variations ) ) {
    
                    foreach( $variations as $key => $value ) {
    
                        $key = str_replace( 'attribute_', '', $key ); // Clean the attribute name
    
                        $attribute = $attributes[$key]; // Get the attribute data
    
                        // Check if the attribute is a taxonomy
                        if( $attribute['is_taxonomy'] ){
    
                            // Get the taxonomy name
                            $attr_name = get_term_by( 'slug', $value, $key, 'ARRAY_A' );
                            $attr_name = $attr_name['name'];
    
                        } else {
                            $attr_name = ucwords($value); // Clean up the custom attribute name
                        }
    
                        $name_output[] = $attr_name; // Load them into an array to be imploded
                    }
                }
    
                $product_title = get_the_title( $product_id ); // Get the main product title
    
            $product_title .= ( $name_output ? ' Edition Number ' . implode( ', ', $name_output ) : '' ); // Add variation(s) if not null
    
            $added_text = sprintf( __( '"%s" was successfully added to your cart.', 'woocommerce' ), $product_title );
    
         // Output success messages
    
            $return_to  = apply_filters( 'woocommerce_continue_shopping_redirect', wp_get_referer() ? wp_get_referer() : home_url() );
    
            $message    = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', $return_to, __( 'Continue Shopping', 'woocommerce' ), $added_text );
    
            $message    = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', wc_get_page_permalink( 'cart' ), __( 'View Cart', 'woocommerce' ), $added_text );
    
        return $message;
    }

    Moderator James Huff

    (@macmanx)

    Thanks for sharing your solution!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add variation to the added to cart message’ is closed to new replies.