Skip to content

Instantly share code, notes, and snippets.

@m-thomson
Last active March 12, 2024 04:01
Show Gist options
  • Save m-thomson/982adf4d7f680887f03658949b283096 to your computer and use it in GitHub Desktop.
Save m-thomson/982adf4d7f680887f03658949b283096 to your computer and use it in GitHub Desktop.

WP All Import - IF/ELSE statement examples

Here are some example WP All Import [IF] statements. The criteria for an IF statment is written in XPath 1.0 syntax and can use XPath functions. Although powerful, XPath syntax can be quite complex. In many cases it might be easier to use a PHP function as shown here.

Note: The [ELSE]<something> part is optional

Number is equal to:

[IF({price[.=0]})]Zero[ELSE]Not Zero[ENDIF]

Number is greater than:

[IF({price[.>0]})]Greater than 0[ENDIF]

Text is equal to:

[IF({title[.='Foo']})]The title is Foo[ENDIF]

Text is not equal to:

[IF({title[.!='Foo']})]The title is not Foo[ENDIF]

Text is empty (see note below):

[IF({title[.='']})]The title is empty[ENDIF]

Text contains:

[IF({title[contains(.,’Foo')]})]Has foo[ENDIF]

Text length is:

[IF({title[string-length()>10]})]{title}[ENDIF]

IF/ELSE example:

[IF({title[.='']})]The title is empty[ELSE]{title}[ENDIF]

Note: You might have seen examples using not(text()) to check for empty fields. While this is valid XPath syntax, it seems to fail on some servers. The reasons are unclear. Just use the Text is empty syntax above instead.

@m-thomson
Copy link
Author

Compare two fields within the file like this:

[IF({sku[.=../parentsku]})]The fields are equal[ELSE]They're not equal[ENDIF]

@m-thomson
Copy link
Author

m-thomson commented Jul 13, 2017

Tip: You can often simplify your XPath statements by removing the [1] notations. That's only needed if there are multiple nodes with the same name (to indicate the one you want). By default the first one is grabbed so [1] is unnecessary. These are functionally equivalent if there is only one title field:

[IF({title[1][text() = 'Foo']})]The title is Foo[ENDIF]
[IF({title[text() = 'Foo']})]The title is Foo[ENDIF]

@m-thomson
Copy link
Author

m-thomson commented Aug 17, 2017

Nested IF statements are allowed:

[IF({color[.= '']})]
  [IF({size[.= '']})]
   One size fits all. Color is as pictured.
  [ELSE]
  One size fits all. Color: {color}
  [ENDIF]
[ELSE]
  Size: {size} Color: {color}
[ENDIF]

Note #1: This is written on multiple lines here for readability but can be placed on all a single line in your import.
Note #2: Just because you can nest IF statements doesn't always mean that you should :-). A custom PHP function is usually a cleaner solution... especially in single line fields.

@jonasskafte
Copy link

Hi there @m-thomson,

Thanks for sharing this, I've found it really useful! 😃

Am I right that it's not possible to use native XPath functions in WP All Export's 'Custom XML Feed'?

It doesn't seem to work when I try. I've followed your advice and tried to find a solution with a PHP function instead (example A here). However, there isn't a good example for when you want to return a different value from your import file depending on whether another field in the file is empty or not. In other words, I cannot work out how to recreate this IF/ELSE statement with PHP:

[IF({value_1[.='']})]{value_2}[ELSE]{value_3}[ENDIF]

I don't know if you're involved with WP All Import, but it would be great to have an example of that in the documentation as well.

Anyway, thanks again for this great resource!

@sprocker
Copy link

sprocker commented Feb 15, 2019

These syntaxes are great.....can you advise on how you use it to compare two fields on the feed?

[If {featured_image[1]} <> {image[1]}] {image[1]} [ENDIF]

Thanks

@Maurisss94
Copy link

Hi, i have a little question about conditional in this plugin.

If one field have this content: [xec_price2_ca({price[1]})][xec_sale_ca({price_freq[1]})]
Is a conditional?
Does it mean that if the first field does not exist, does it pick up the second one?

Thanks

@nikolaoskaikisis
Copy link

Hello, i have a question please. Is it possible not to import lines if they have specific word or text.
I cant find any syntax for this.

@m-thomson
Copy link
Author

Hi @nikolaoskaikisis

Check out the syntax above for "Text is not equal to".

@m-thomson
Copy link
Author

To anyone reading this:

I haven't worked for the company that makes WP All Import in several years and I don't use their plugin currently (as I'm not doing WordPress work). So unfortunately, I won't be able to answer your questions.

However, the support the company offers is pretty great. In fact it's some of the best I've ever seen. I encourage you to reach out to them on https://www.wpallimport.com with any questions.

I am leaving these gists up however since they do seem to help for quick reference.

Best of luck.
Mark

@nikolaoskaikisis
Copy link

Hi @m-thomson

thanks for your quick answer. I dont know exactly how to use that.
I have this selection {reifenbreite[1]}/{reifenquerschnitt[1]}/{reifenbauart[1]}{reifendurchmesser[1]} in my title.
Some filed that i import has 0 {reifenbreite[1]} or 0 {reifenquerschnitt[1]}. This kind of field i dont want to import.
How can i do that please with syntax [IF({title[.!='Foo']})]The title is not Foo[ENDIF]

@Ankit7791
Copy link

Hello @m-thomson

Can you please help with this

/property[price[1] > 5000 and town[1][not(contains(.,"Gandía"))]]

I am trying to not import property with Gandía , but it is not working ,

@amityweb
Copy link

amityweb commented Jul 28, 2021

If you need to check the value of an attribute, then put @ before the name.

I have an XML like this and only need to extract the ones with type=Location:

<categories>
	<category type="Location">
		<![CDATA[ London ]]>
	</category>
	<category type="Location">
		<![CDATA[ Manchester ]]>
	</category>
	<category type="Something Else">
		<![CDATA[ Whatever ]]>
	</category>
</categories>

Then process it like:

[FOREACH({categories/category})][IF({@type[.= 'Location']})]{.}[ENDIF][ENDFOREACH]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment