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
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:
-
2+1 for this - I have be honest, I would have just done
text-transform: capitalize
in the css ;)– McNabJul 31, 2013 at 21:56 -
-
@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!– WesselAug 1, 2013 at 7:57