skip to Main Content

By default WooCommerce shows the attribute of a variable product in the title and I’m using this code to show the attribute below the title in the cart and checkout pages:

add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' );

But that doesn’t work in My Account page, users see the full product name with no attribute.

To fix it I’m using the code below to show the attribute in the product title:

function show_attributes_outside_title_1( $enabled ) {
    if ( !is_account_page() ) {
        $enabled = false;
    }
    return $enabled;
}
add_filter( 'woocommerce_product_variation_title_include_attributes', 'show_attributes_outside_title_1' );

function show_attributes_outside_title_2( $enabled ) {
    if ( !is_account_page() ) {
        $enabled = false;
    }
    return $enabled;
}
add_filter( 'woocommerce_is_attribute_in_product_name', 'show_attributes_outside_title_2' );

But I’d like to show the attribute below the title (or a new column), it’s easier to read and goes with the same desing you see in the cart and checkout pages.

2

Answers


  1. Chosen as BEST ANSWER

    I changed Vincenzo's answer a bit to make it look the same way I see the attributes in my Cart and Checkout pages. Here's the code in case anybody else needs it:

    // Shows the variation chosen in the product name in the download table of the my-account page
    add_action( 'woocommerce_account_downloads_column_download-product', 'change_product_download_name', 10, 1 );
    function change_product_download_name( $download ) {
        // gets the product object
        $product = wc_get_product( $download['product_id'] );
        // gets the name of the product
        $product_name = $download['product_name'];
        // define variable
        $product_attributes = '';
        // if the product is a variation
        if ( $product->is_type( 'variation' ) ) {
            // gets the name of the product with the chosen variation
            $product_name = $product->get_name();
            $product_attributes = wc_get_formatted_variation( $product, true, true, false );
        }
        // print the product name (with or without product url)
        if ( $download['product_url'] ) {
            echo '<a href="' . esc_url( $download['product_url'] ) . '">' . esc_html( $product_name ) . '</a><p>' . esc_html( $product_attributes ) . '</p>';
        } else {
            echo esc_html( $product_name ) . '<p>' . esc_html( $product_attributes ) . '</p>';
        }
    }
    
    // Shows variation outside title in cart and checkout pages
    add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
    add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' );
    

    The last two filters replace my code and the first part solves the issue in the Downloads page.


  2. There is some confusion in the initial part of the question.

    You say you want to show the attribute under the product title on the cart and checkout page but then return __return_false, do you intend to do the opposite?

    SOLUTION #1

    You may want to reverse the check to make sure that your chosen product variation attribute is shown under the product name on your account page under Downloads (as evidenced by your comment above):

    add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
    add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' );
    
    add_filter( 'woocommerce_product_variation_title_include_attributes', 'show_attributes_outside_title_1' );
    function show_attributes_outside_title_1( $enabled ) {
        if ( is_account_page() ) {
            $enabled = true;
        }
        return $enabled;
    }
    
    add_filter( 'woocommerce_is_attribute_in_product_name', 'show_attributes_outside_title_2' );
    function show_attributes_outside_title_2( $enabled ) {
        if ( ! is_account_page() ) {
            $enabled = false;
        }
        return $enabled;
    }
    

    SOLUTION #2

    If you want to leave the code in your question unchanged you can use the woocommerce_account_downloads_column_download-product hook where download-product is the id of the product name column (in the /my-account/downloads/ page). Here the documentation.

    Finally, with the wc_get_formatted_variation function you can get the name of the chosen variation. For more information on parameters read the documentation.

    // shows the variation chosen in the product name in the download table of the my-account page
    add_action( 'woocommerce_account_downloads_column_download-product', 'change_product_download_name', 10, 1 );
    function change_product_download_name( $download ) {
        // gets the product object
        $product = wc_get_product( $download['product_id'] );
        // gets the name of the produc
        $product_name = $download['product_name'];
        // if the product is a variation
        if ( $product->is_type( 'variation' ) ) {
            // gets the name of the product with the chosen variation
            $product_name = $product->get_name() . " - " . wc_get_formatted_variation( $product, true, false, false );
        }
        // print the product name (with or without product url)
        if ( $download['product_url'] ) {
            echo '<a href="' . esc_url( $download['product_url'] ) . '">' . esc_html( $product_name ) . '</a>';
        } else {
            echo esc_html( $product_name );
        }
    }
    

    The code has been tested and works. Add it to your active theme’s functions.php.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search