Magento: Programmatically cleanup product text attributes

Tweet about this on TwitterShare on LinkedInShare on Google+Share on FacebookEmail this to someone

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:

  1. To fix all products manually from Magento admin panel
  2. 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:

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!

Tweet about this on TwitterShare on LinkedInShare on Google+Share on FacebookEmail this to someone

Tsvetan Stoychev

Tsvetan aka. Cecko is the founder of Cecko's Lab. He is Magento addicted since Magento CE 1.2.1.2 and has worked on over 30 Magento projects. At the moment he is in charge to take care about the money flow of the company, to keep constant communication with the clients and to keep the people in the office busy.

More Posts

Follow Me:
TwitterLinkedIn

  • Simone D’Amico

    but first way is many times faster of second.

    You’re so so right!! I had a problem with the method save() in an Observer (I couldn’t figure out why but It requires more than 60seconds :) ) and I’ve used the saveAttribute() method instead and it works really good. :)

    Thanks for your post, it helped me a lot with my problem!

  • Anonymous

    You welcome!

    So happy to see, that developers solving problems by reading the article!

  • ceckoslab

    You welcome!

    So happy to see, that developers solving problems by reading the article!