In a recent version of WooCommerce the developers made a tweak to how variable products are show when they only have one attribute. The chose attribute value is shown directly in the product title when, in example, the product is added to the cart.
I’ve noticed that not everyone likes this new feature and some asked to revert it back to how it was before.
Can we accommodate everyone? Yes, here is how.
Let’s Write Some Code!
I created a Pull Request not too long ago to add the filter woocommerce_is_attribute_in_product_name
to the core of WooCommerce.
This filter can be used to show the attributes below the product title, like it was before the most recent version of WooCommerce.
You can add this code to the functions.php file in wp-content/themes/your-child-theme-name/ to do that:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' ); |
This code alone though is not enough. You will now see the attributes table below the product title, but also you will see the attribute in the product title.
To remove it from there, you need some more code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Removes the attribute from the product title, in the cart. | |
* | |
* @return string | |
*/ | |
function remove_variation_from_product_title( $title, $cart_item, $cart_item_key ) { | |
$_product = $cart_item['data']; | |
$product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key ); | |
if ( $_product->is_type( 'variation' ) ) { | |
if ( ! $product_permalink ) { | |
return $_product->get_title(); | |
} else { | |
return sprintf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $_product->get_title() ); | |
} | |
} | |
return $title; | |
} | |
add_filter( 'woocommerce_cart_item_name', 'remove_variation_from_product_title', 10, 3 ); |
Using both these snippets together will make sure that you only show the attributes table like it was before, and the title of the product will be the one that you set in the Dashboard, with nothing added to it.
Leave a Reply