Cecko's Lab

Custom Magento Development

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:

  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!

Interesting articles:

  1. Is Magento for you?
  2. ePay BG & Magento
  3. Magento: Sucks Test – An exercise for optimists!
  4. Magento Developers Paradise 2010 and Me
  5. Magento: Translate untranslatable labels in admin area
  • 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!

Tsvetan Stoychev

Hello dear visitors. My name is Tsvetan Stoychev.
I am freelancer working on Magento projects. Here I would like to share my knowledge in web development and especially in Magento area.

Contacts:
Email
ceckoslab [at] gmail.com
Phone number
+359 883 33 58 44