0

I have a CSV which I want to use for import. It has clomuns with compatibility information as follows:

|  |Compatible with Product X|Compatible with Product Y|Compatible with Product Z|
|1 |      Y                  |                         |       Y                 |
|2 |                         |       Y                 |       Y                 |
|3 |      Y                  |       Y                 |                         |
|4 |                         |                         |                         |

I want to use value replacer to map the colums to the my Magento attribute "compatibilty" which is a multiselect box.

The value of the "compatibilty" attribute should be as follows for a single value

Product X
And for multiple values it should be like this
Product X;Product Y;Product Z

I first use the column mapper to map every different cloum to campatibility. Then I use value replacer on the compatibility column. I came up with the following code the make it possible to assign the right values to the right products:


{{ {item.'Product_X}=='Y'?'Product X;':NULL }}{{ {item.'Product_Y}=='Y'?'Product Y;':NULL }}{{ {item.'Product_Z}=='Y'?'Product Z;':NULL }}

Or better readable

{{ {item.'Product_X}=='Y'?'Product X;':NULL }}
{{ {item.'Product_Y}=='Y'?'Product Y;':NULL }}
{{ {item.'Product_Z}=='Y'?'Product Z;':NULL }}

Everything gets set correctly. But the problem is that Magmi sometimes adds an ampty value to the attribute and assigns it to a product. I obviously don't want that.

1

here is a formula that does what you expect (since Magmi value replacer only accepts R-values)

It's a bit complicated, so maybe developing a very simple plugin maybe more readable.

implode(';',array_filter(array({item.Product_X}=='Y'?'Product X':'',{item.Product_Y}=='Y'?'Product Y':'',{item.Product_Z}=='Y'?'Product Z':'')))

it also trims the end ; if any.

  • I see what you are doing. I didn't know I could use any PHP in the value replacer. Can I just use all off your code in the value replacer? I did think to write a plugin for it but I don't know where to start. Do you know any good resources for that? – Akif Jul 23 '14 at 12:36
  • Btw, what do you mean with R-Values? – Akif Jul 23 '14 at 12:43
  • 1
    a Rvalue is a value that is assignable to a variable. ie : $x=<this is a rvalue> – dweeves Jul 23 '14 at 14:55
  • Thanks. The code doesn't work out of the box. I removed the ending semicolon to make it executable. But, the problem is it still adds empty values to the attribute in Magento. I thought It might be a problem with several semicolons after eachother. The example I gave is simplified for obvious reasons. In reality there are 12 columns with compatibility info. So what i did is I replaced the first paramater of the str_replace function with an array which has ';;', ';;;', ';;;;' etc. up to 12. It still adds emppty values to the attibute. Any ideas? Is there a way to do a debug view or so? – Akif Jul 23 '14 at 15:44
  • 1
    updated code to remove empty values (added array_filter , removed trim & str_replace since array would then contain only non empty values) – dweeves Jul 23 '14 at 16:14
0

It will add an empty value because of the trailing ';' that will always be prevalent if any of the columns have 'Y' in them. The empty space after the ';' will represent an empty value.

It is akin to doing this,

$myVar = '';
$myVar .= 'Product X;';
$myVar .= 'Product Y;';

// Product X;Product Y;   (<- empty value here!)

Instead of this:

$myVar = array();
$myVar[] = 'Product X';
$myVar[] = 'Product Y';
$myVarString = implode(';', $myVar);
// Product X;Product Y

You need to do the latter, if Magmi's value mapper will allow it. As this will remove any trailing ';'.

  • Can I use (any) PHP in the value replacer? – Akif Jul 23 '14 at 8:05

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.