1

After exporting my previous database, allot of my product names where upper case. I now want to run a sql query to update the product names to title case. What is correct way to do this

1 Answer 1

3

A few ideas:

Use CSS! (Courtesy @McNab):

h2 { text-transform: capitalize; }

Echo it from PHP in your View:

You shouldn't have to store it into the database, you can do this with a PHP wrapper around your echo. Instead of:

<?php echo $product->getName(); ?>

Use:

<?php echo ucwords(strtolower( $product->getName() )); ?>

Create a view helper:

Or, instead, register it in a Magento helper. Let's call that helper's module mymodule and use it as such:

class MyCompany_MyModule_Helper_Data extends Mage_Core_Helper_Abstract
{
    public function toUppercase($name)
    {
        return ucwords(strtolower( $name ));
    }
}

And use it like this:

<?php echo Mage::helper('mymodule')->toUppercase($product->getName()); ?>

I don't care, I want to do it in the DB:

Because the product names are stored in EAV your attribute code may differ. So you'll want to avoid direct SQL. Never query the db directly anyhow. It's bad practice.

You can load the collection in a one-off script and update all the product names as such:

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');

foreach($products as $product){
    $name = $product->getName();
    $product->setName( ucwords(strtolower( $name )) );
    $product->save();
}

If you have flat catalog enabled you'll want to re-index after this operation as the flat tables may not be enabled. You may also want to disable all indexes prior to running this.


Seriously, I just want a direct SQL query:

Good luck! Go read this article on creating your own Title Case function:

https://stackoverflow.com/a/1191683/582138

3
  • 2
    +1 for this - I have be honest, I would have just done text-transform: capitalize in the css ;)
    – McNab
    Jul 31, 2013 at 21:56
  • Ah, another good point. Forgot about good-ol' CSS.
    – philwinkle
    Jul 31, 2013 at 22:12
  • @philwinkle Thanks for all of these options :) Really appreciate your time. I'm not sure which method to go with here. McNab, Good point completely over looked the CSS option. Thanks!
    – Wessel
    Aug 1, 2013 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.