1
votes

In WooCommerce, when someone adds a product to the basket a notice message is shown to confirm. Rather than showing the parent, I want to be able to show the variation that a customer has put into their shopping basket.

I'm using Andrew Schultz's code from the answer to Show product variation in woocommerce Added to cart message I guess that the hooks or names have changed because what's happening is that "pa_subject" is being shown when it should say "Geography", for example.

Any ideas how to change it?

function modify_wc_add_to_cart_message( $message, $products ) {
    $attribute_label = '';
    $titles = array();
    $count  = 0;

    foreach ( $products as $product_id => $qty ) {
        $product = wc_get_product( $product_id );

        if( $product->is_type( 'variable' ) ) {
            foreach( $product->get_variation_attributes() as $attribute_name => $attribute_values ) {
                if( isset( $_REQUEST['attribute_' . strtolower( $attribute_name )] ) ) {
                    if( in_array( $_REQUEST['attribute_' . strtolower( $attribute_name )], $attribute_values ) ) {
                        if( ! empty( $attribute_label ) )
                            $attribute_label .= ', ';

                        $attribute_label .= $attribute_name . ' : ' . $_REQUEST['attribute_size'];
                    }
                }
            }
        }

        $titles[] = ( $qty > 1 ? absint( $qty ) . ' × ' : '' ) . sprintf( _x( '“%s”', 'Item name in quotes', 'woocommerce' ), strip_tags( get_the_title( $product_id ) ) . ( ! empty( $attribute_label ) ? ' - ' . $attribute_label : '' ) ) ;
        $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 ) );

    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( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), esc_html__( 'Continue shopping', 'woocommerce' ), esc_html( $added_text ) );
    } else {
        $message   = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View cart', 'woocommerce' ), esc_html( $added_text ) );
    }

    return $message;
}
add_filter( 'wc_add_to_cart_message_html', 'modify_wc_add_to_cart_message', 10, 2 );

scamp of return

1

1 Answers

1
votes

Yes the code throw some errors when debug is enabled:

Notice: Undefined index: attribute_size in ../wp-content/themes/storefront-child/functions.php on line xxxx

Essentially with this line:

$attribute_label .= $attribute_name . ' : ' . $_REQUEST['attribute_size'];

It seem that this code was made to handle a "size" custom attribute, instead of any.

It also displays the woocommerce attribute slug starting with "pa_" instead of the attribute taxonomy label name:

enter image description here

So to make it work and display the correct attributes label names with their term name values, use the following revisited code version:

add_filter( 'wc_add_to_cart_message_html', 'change_add_to_cart_message', 10, 2 );
function change_add_to_cart_message( $message, $products ) {
    $titles = array();
    $count  = 0;

    foreach ( $products as $product_id => $qty ) {
        // Get the WC_Product object instance
        $product = wc_get_product( $product_id );
        if( $product->get_type() === 'variable' ) {
            $variation_attributes = array();
            foreach( $product->get_variation_attributes() as $taxonomy => $terms_slugs ) {
                $wc_attribute_name = wc_variation_attribute_name( $taxonomy );
                if( isset( $_REQUEST[$wc_attribute_name] ) ) {
                    if( in_array( $_REQUEST[$wc_attribute_name], $terms_slugs ) ) {
                        $term_name = get_term_by( 'slug', $_REQUEST[$wc_attribute_name], $taxonomy )->name;
                        $variation_attributes[] = wc_attribute_label( $taxonomy ) . ': ' . $term_name;
                    }
                }
            }
            $variation_attributes = implode(', ', $variation_attributes);
        }

        $titles[] = ( $qty > 1 ? absint( $qty ) . ' &times; ' : '' ) . sprintf( _x( '&ldquo;%s&rdquo;', 'Item name in quotes', 'woocommerce' ), strip_tags( get_the_title( $product_id ) ) . ( ! empty( $variation_attributes ) ? ' - ' . $variation_attributes : '' ) ) ;
        $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 ) );

    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( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), esc_html__( 'Continue shopping', 'woocommerce' ), esc_html( $added_text ) );
    } else {
        $message   = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View cart', 'woocommerce' ), esc_html( $added_text ) );
    }

    return $message;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

enter image description here


To display only the term names without the taxonomy label name

Replace in the code this line:

$variation_attributes[] = wc_attribute_label( $taxonomy ) . ': ' . $term_name;

by this one:

$variation_attributes[] = $term_name;

You will get something like:

enter image description here