0

I would like to know how to merge attribute values so that all of the affected products would be part of 1 filterable attribute as apposed to multiple.

Problem: The data sheets I use have multiple entries for the same value, this translates into multiple filterable attribute values for the same value.

Example:

Attribute touch_sensitive Value YES (10 products) Value Yes (5 products) Value Y (2 products) Value NO (20 products) Value No (1 product) Value N (6 products)

Essentially I have 6 different attributes on the front end spread across multiple products that should be really under 2 attributes.

What is the recommended method to merge multiple attribute values?

  • "Essentially I have 6 different attributes on the front end spread across multiple products that should be really under 2 attributes." By this I assume you mean that you have 6 different values and you need only 2 of them. Am I right?. And one more question, is your attribute a dropdown attribute? – Marius Jul 10 '14 at 8:00
  • @Marius yes that is correct, 6 different values that should be only 2 values. Yes the attribute is a drop down. Thanks! SR – SR_Magento Jul 10 '14 at 10:51
1

I think this can be achieved through simple product updates and then remove the useless values.

First you need to identify the ids of the values.

Let's say they have the following ids.

YES - 40
Yes - 41
Y   - 42
NO  - 50
No  - 51
N   - 52

And you want to keep only the values Yes (41) and No (51).
This means that all the products that have the values YES and Y for your attribute should have the value Yes. Translating this into ids means 40 & 42 become 41.
Doing the same for the No attribute...50 & 52 become 51.

Use the following script.

$trans = array();
$trans[40] = 41; // $trans[FROM] = TO;
$trans[42] = 41;
$trans[50] = 51;
$trans[52] = 51;

$attributeCode = 'touch_sensitive';
foreach ($trans as $from=>$to) {
    //get the product collection
    $collection = Mage::getModel('catalog/product')->getCollection();
    //filter by the current attribute value
    $collection->addAttributeToFilter($attributeCode, $from);
    //get all product ids
    $ids = $collection->getAllIds();
    //update the values
    Mage::getSingleton('catalog/product_action')->updateAttributes(
         $ids, 
         array($attributeCode => $to),
         0
    );
}

At the end, remove the options you don't need anymore and reindex everything.

  • Okay a quick clarification - in your example script everything is changed to value 41. So if I wanted $trans[50] = 51 that would change that one to the value 51 (No) Correct ? And I can use this to create a larger array of attributes that need to be adjusted not just for these values correct? (or do you recommend doing 1 attribute at a time) Thanks Marius! – SR_Magento Jul 10 '14 at 11:36
  • @sr_magento. You are right. It was a copy/paste error. I update the answer. $trans[50] should be 51. This way you can create a big array with a lot of the options you need, but I recommend doing them one attribute at a time. This way you are sure you don't assign one attribute value to the value of an other one. So FROM and TO should be option ids for options from the same attribute. You can also change the script above so you can run it once for multiple attributes. Just wrap it in a foreach loop where the $trans array is different for each attribute. – Marius Jul 10 '14 at 11:41
  • You are my hero, I will give this a whirl a little later and report back! – SR_Magento Jul 10 '14 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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