Home > Software engineering >  WooCommerce: How to change the label name Dimensions?
WooCommerce: How to change the label name Dimensions?

Time:01-23

WooCommerce has a default attribute in the products data table called dimensions which takes values entered as LxBxH. I would like to change the label name dimensions to packaging dimensions in the front end as well as if possible in the back end. What code has to be entered in functions.php in order to achieve the same?

enter image description here

CodePudding user response:

See code below, it uses the woocommerce_attribute_label filter to check if the attribute name is "pa_dimensions" (WooCommerce uses the "pa_" prefix for product attributes). If it is, the label is changed to "Packaging Dimensions".

If you have already created some products and you have used the 'pa_dimensions' attribute, the change will be reflected only in the new products you will create or the existing products you will edit.

  function change_dimensions_label( $label, $name ) {
        if ( $name === 'pa_dimensions' ) {
            $label = 'Packaging Dimensions';
        }
        return $label;
    }
    add_filter( 'woocommerce_attribute_label', 'change_dimensions_label', 10, 2 );

CodePudding user response:

function woo_replace_string($translation, $text, $domain) {

    $search_string = 'Dimensions';
    if (strpos($translation, $search_string) !== false) {

        $translation = str_replace($search_string, 'Packaging Dimensions', $translation);
    }
    return $translation;
}

add_filter('gettext_woocommerce', 'woo_replace_string', 20, 3);

Use gettext_{$domain} hook for this. See the doc here

  • Related