• Resolved delaitec

    (@delaitec)


    Hello.

    I use my product titles in [My Title] format with the first letter of each word capitalized.

    But I do frequent imports, and the product titles in the CSV file I’m importing are in UPPERCASE.

    Thus, the title is already saved in the database in UPPERCASE and this prevents me from using CSS to leave the title in the format I want, since the “capitalize” feature of CSS does not work in uppercase words.

    For this reason, I need that when importing new products, the titles are saved on database with all letters in lowercase,
    this way I can use the “capitalize” feature

    How can I do this via Functions, without plugin?

    Obs.: I know that I can try to do this in the spreadsheet before importing, but if it were possible to do it automatically when importing, that would be great, as it is one less thing to worry about.

    Thanks.

Viewing 7 replies - 1 through 7 (of 7 total)
  • This works for me:

        .title {
          text-transform: lowercase;
        }
        .title:first-letter {
          text-transform: capitalize;
        }
    Thread Starter delaitec

    (@delaitec)

    @lorro Thanks for your reply.

    I try this before, this only afect the first letter on text block

    I need all first letter on each word.

    OK, I give up on CSS for that. Here is a JavaScript to do it:

      <script>
      window.onload = function() {
        var elements = document.getElementsByClassName( "title" );
        for ( var i = 0; i < elements.length; i++) {
          var title = elements[i].innerHTML;
          var words = title.split( " " );
          var new_title = "";
          for( j = 0; j < words.length; j++ ) {
            new_title = new_title + words[j].charAt(0).toUpperCase() + words[j].slice(1).toLowerCase() + " ";
          }
          elements[i].innerHTML = new_title;
        }
      };
      </script>

    Replace “title” with the classname used by your site for the product titles.

    Thread Starter delaitec

    (@delaitec)

    You can exporta all products on csv, then use excel to make lowercase in all titles, them import and the css “capitalize” will work. ??

    If you use the Javascript and find that the titles flash up briefly in capitals, or you are concerned that some users may not have Javascript enabled, you can use this php snippet to modify the product titles.

      // change product titles in all caps to title case 
      add_filter( 'the_title', 'custom_title_format' );
      function custom_title_format( $title ) {
      	if( is_shop() || is_product_category() || is_product() ) {
          $words = explode(  " ", $title );
          $title = "";
          foreach( $words as $word ) {
            $word = ucfirst( strtolower( $word ) );
            $title = $title.$word." ";
          }
      		return trim( $title );
      	}
    	  // return the normal Title if conditions aren't met
    	  return $title;
      }

    Hi @delaitec

    Thanks for reaching out!

    These forums are meant for general support with the core functionality of WooCommerce itself. What you want to achieve would require customization to do it. Since custom coding is outside our scope of support, I am leaving this thread open for a bit to see if anyone can chime in to help you out.

    For questions related to development and custom coding, your best bet is to ask on any of these channels for support. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, too.

    Hope this helps!

    Thread Starter delaitec

    (@delaitec)

    @lorro and @xue28
    Thank you

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Force product titles to lower case on DB’ is closed to new replies.