Skip to content
Advertisement

How do I replace the dash by round brackets on WooCommerce product variations?

I would like to replace the dash ( – ) with round brackets ( ) around WooCommerce product variation names.

Normally a WooCommerce product variation is displayed like this:

Product name - variation name

Sometimes this dash can be confused by a minus, and it looks like it’s the main product minus the variation. This depends on both the product and variation names.

So the displayed product variation must be, for example:

Product name (variation name)

Advertisement

Answer

add_filter('woocommerce_product_variation_get_name', 'woocommerce_product_get_name', 10, 2);

function woocommerce_product_get_name($name, $product) {

    if (strpos($name, '-') !== false) {
        $modified_name_last = substr($name, strrpos($name, '-') + 1);
        $modified_name_first = substr($name, 0, strrpos($name, "-"));
        $name = $modified_name_first . ' (' . $modified_name_last . ')';
    }

    return $name;
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement