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
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:
The last two filters replace my code and the first part solves the issue in the Downloads page.
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):
SOLUTION #2
If you want to leave the code in your question unchanged you can use the
woocommerce_account_downloads_column_download-product
hook wheredownload-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.The code has been tested and works. Add it to your active theme’s functions.php.