Value Replacer

From Magmi Wiki
Jump to: navigation, search

This powerful plugin enables to replace/create values using dynamically computed values from the datasource original data.

This plugin could be very useful when dealing with non magento-like csvs

Usage

  • Enter the name of values you want to create or replace in the Replaced attributes field, comma separated.
  • when you leave the field, new fields appear that ask for the replacing values for the attribute names entered in the Replaced attribute field.

Hint

The replaced values do not need to exist in the data source, if they don't exist, the plugin will create them !!

Replacing values syntax

The replacing values fields admit 2 kind of values.

  • simple syntax : any string with tokens of type {item.xxx} , xxx being the name of one existing column in the datasource.
  • advanced syntax :
    {{some complex expression}}
    , with some complex expression admitting {item.xxx} tokens inside

Simple Syntax Examples

  • redefining the sku with SomePrefix-sku-SomePostfix:

put sku in the list of Replaced attributes and enter the following into the Replacing value for sku field:

 SomePrefix-{item.sku}-SomePostfix

  • create categories column (for the category importer) from custom Cat & SubCat columns in the datasource.

put categories in the list of Replaced attributes and enter the following into the Replacing value for categories field:

 {item.Cat}/{item.SubCat}

  • create categories column from several separated columns

Say you have columns in your csv named "cat1","cat2","cat3" and you want to have item in the following category tree "/cat1/cat2/cat3"

    • put categories in the list of Replaced attributes
    • Enter the following into the Replacing value for categories field:

{item.cat1}/{item.cat2}/{item.cat3}

Advanced Syntax examples

  • Computing price from cost & margin columns of datasource**, with margin expressed in % (but without the % sign)

put price in the list of Replaced attributes and enter the following into the Replacing value for price field:

  {{ {item.cost}*(1+({item.margin}/100.0)) }}
  • Cleaning cumulative whitespaces** in column yyy
  {{preg_replace('|\s+|',' ',{item.yyy})}}
  • Replacing an empty field of the csv file by a default value (here a null value by a point .)
  {{{item.short_description}==''?'.':{item.short_description}}}

Another complex problem solved with value replacer

What is acceptable inside {{}}

  • any occurrences of {item.xxx}
  • any valid call of php functions
  • the total content of any {{}} should be a valid php rvalue (ie: should be assignable to a php variable)
  • be careful to not have blank characters before and after item.xxx in the expression {item.xxx}
  • NEW : do no put any quotes around {item.xxx} (even for string based functions)
  • From 0.0.7 : {meta.xxx}

{meta.xxx} possible values

  • {meta.new} : 0 or 1, tells if the item already existed in DB
  • {meta.same}  : 0 or 1 , tells if we are "multiline" definition for the same sku. (ie: if the sku of row before the current is the same than the current row)
  • {meta.product_id} : the entity_id of the product in the db
  • {meta.asid} : the attribute set id of the product in the db

{{}} and double quotes

  • Double quotes inside {{}} are currently not supported due to some obscure parameter passing escaping conflict.
  • replace all doublequotes by \x22 if you need to use them inside regexp or char(22) if you are trying to build a string with double quotes.

{{}} Helpers

Some helper objects are available in {{}} context

Slugger

  • this helper is able to create a slug value from a string that it takes as parameter. it's very handy for setting url_key values.

Slugger Sample

Say you want your product url key to be the name of the product (given you don't provide it in datasource)

You'd use the value replacer for url_key column and put the following value in "new value for url key"

{{ Slugger::slug({item.name}) }}

This would automatically create a slug for the name that would be used as url key

ValueRemapper

(from plugin version 0.0.8 , magmi 0.7.18)

  • this powerful helper enable you to use a csv for remapping input values to others. This is very useful when dealing with external providers data.
  • this helper can perform :
    • Value mapping , case sensitive or insensitive for a given source value in the datasource for a given column
    • Value mapping , case sensitive or insensitive for a given source multiple value in the datasource for a given column
     In the latter case, each part of the multivalue will be tested for mapping, multivalue separator is configurable (defaulting to ,)

ValueRemapper, csv format

The csv format that is accepted by the remapper is :

  • separator : ; (semicolon)
  • enclosure : none (NO ENCLOSURE AROUND VALUES)
  • NO HEADER

ValueRemapper Sample CSV

say we have a remapcolor.csv file, that is located somewhere at '/some/absolute/path'

Y;Yellow
G;Green
B;Blue

say now we have another csv , remapcat.csv that is located somewhere at '/some/other/path'

InputCatName;New Cat Name
OtherCategory;xxxxx

ValueRemapper Sample : single value remapping, case sensitive matching

if you want to setup a value replacer entry for color column, you'd be able to perform remapping through the following formula

{{ ValueRemapper::use_csv('/some/absolute/path/remapcolor.csv')->map({item.color}) }}

Same sample, case insensitive matching

{{ ValueRemapper::use_csv('/some/absolute/path/remapcolor.csv')->map({item.color},true) }}

ValueRemapper Sample : multivalue remapping

Since categories column from the category importer plugin have tree separator, you may also "selectively" remap part of the values like this

{{ ValueRemapper::use_csv('/some/other/path/remapcat.csv')->mapmulti({item.categories},'/') }} 

Same sample, case insensitive matching

{{ ValueRemapper::use_csv('/some/other/path/remapcat.csv')->mapmulti({item.categories},'/',true) }} 


Above the '/' is the category tree separator, telling to apply mapping to each value between the '/'

Plugin Precedence

Value replacer is called AFTER generic mapper, so any textual values returned by Advanced Syntax expression won't be parsed by Generic Mapper plugin.

So, say you want to set visibility to "Not visible individually" for empty visibility value.

This example won't work:

{{ {item.visibility}==''?'Not visible Individually':{item.visiblity} }}

This one will:

{{ {item.visibility}==''?'1':{item.visiblity} }}

Explanation:

'1' is the value that Generic Mapper would have put in visibility field for Not visible Individually textual value. Since Value replacer is called after Generic Mapper , no textual value mapping will be done. So,the Value Replacer expression has to return the mapped value

Important Warning

As Peter Parker says : Great Powers come with great responsibilities. So understand what you are doing before experimenting and complaining. the Advanced Syntax is not for common users not knowing anything about programming.



Back to Product Import Related Plugins