Magento: Programmatically cleanup product text attributes

Quick Overview
This post may help if you want to do mass replace of strings of your product text attributes and it describes just one of many possible ways to do it.
My situation
While I was working on my last project, client reported next problem:
“We have some strange symbols in product description, short description and other text attributes. Can you fix them?”
After short conversation with the client I realized, that the content of those attributes where cloned from another website from previous coder.
What can I say? Strange business strategy!
As result of those actions, sometimes the symbol ‘,’ was replaced instead of ‘•’ .

There were 379 products with this problem. I started brainstorming and found next 2 solutions:
- To fix all products manually from Magento admin panel
- To create tool, that automatically will fix all products
I chose solution 1. It was more profitable, because client pay per hour.
Of Course I am Kidding! I decided to be cool and chose solution 2.
Actually I had something I mind. I recalled that I had read about 2 – 3 months ago 2 articles:
- Classy Llama Studios: Saving the Value of a Specific Attribute from a Model
- Inchoo: Magento, products on sale
So my idea to solve the problem was to combine some snippets from those 2 posts and add some “spices”.
I have written short extension and here is the snippet, that solve the problem:
<?php
class CeckosLab_CleanAttributes_CleanController extends Mage_Core_Controller_Front_Action {
public function cleanupAction() {
$attributes = array('description', 'short_description', 'partnumbers', 'compatiblelaptops');
$search = '•';
$replace = ',';
$product = Mage::getModel('catalog/product');
$collection = $product->getCollection();
foreach ($collection as $product)
{
$productIds[] = $product->getId();
}
foreach ($productIds as $productId)
{
$changedAttributes = array();
$_product = new Mage_Catalog_Model_Product();
$_product->load($productId);
foreach($attributes as $attribute) {
$count = 0;
$newValue = str_replace($search, $replace, $_product->getData($attribute), $count);
//If $count > 0 that means we replaced |$count| times search string
if($count > 0) {
$changedAttributes[] = $attribute;
$_product->setData($attribute, $newValue);
}
}
foreach($changedAttributes as $attr) {
$_product->getResource()->saveAttribute($_product, $attr);
}
}
}
}
Something that I want to pay attention.
It was possible instead of
foreach($changedAttributes as $attr) {
$_product->getResource()->saveAttribute($_product, $attr);
}
to use
$_product->save();
but first way is many times faster of second.
Please if you decide to use part of this code, do backup of your database first!
Code is tested on Magento version 1.4.1.1
Conclusion
As conclusion I can say, that I already had a plan how to do this fix, but decided to save some time and nerves and to concentrate on significant tasks. So it is good to have sources of information like previously listed blog posts, because Magento is complex platform and it is impossible to know everything.
Probably some readers will have negative opinion of me. Yes I worked with information, that is extracted from another web site, but actually my client knows about negative consequences if he publish this information on Internet.
Hope that helps to somebody!
Interesting articles:
- Is Magento for you?
- ePay BG & Magento
- Magento: Sucks Test – An exercise for optimists!
- Magento Developers Paradise 2010 and Me
- Magento: Translate untranslatable labels in admin area
This entry was posted on Tuesday, January 4th, 2011 at 10:03 pm and is filed under Magento. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
-
Simone D’Amico
-
Anonymous