A product can have the following column for size:

<column name="size"><![CDATA[L;XL]]></column>

Meaning the product has two sizes, 'L' and 'XL'. We will devide these sizes with a custom PHP code so that two sizes ('L' and 'XL') will be imported for this product instead of one ('L;XL').

The custom PHP code we use for the above is:

[str_replace(";","|",{column[@name="size"]})]

In sizes, we would also like to only import/display products that have 2 sizes or more, due to availability. For example: a product with size column “XL;L” (two sizes available) will be imported, a product with size column “L” (only one size available) will not be imported.

How can we code in PHP that a certain product can only be imported if it has 2 or more sizes?

1

There are 1 best solutions below

0
On

The best way, but probably not the easiest, is to use regex. Assuming that you need "L" and "XL" to be the values that you are going to check

//$value  is going to be the string you are checking for L and XL
//match is the array that is returned containing the values that matched
$value = "L|XL"; //this should match the data you are searching
$pattern = '/^(L\|XL|XL\|L)$/';
if( pregmatch($pattern, $value,  $match) )
{
    //code to import/display
} 

Lets break down the '/^(L\|XL|XL\|L)$/' part of the code This is a pattern of text that we are looking for. The string starts and ends with a forward slash because it is a pattern delimiter. The ^ anchors our search to the beginning of the line of text and the $ anchors the end of the pattern to the end of the line of text we are searching.

You can return the values that match in the $match array. For every set of () that you use in the pattern, you can return a matching result using $match[1], $match[2] and so on. With our example, we will only have $match[1].

The \ is a escape character that causes the search to treat that character as text rather that a special character. (| is used for an 'OR')

So we are saying that if we have 'XL|L' or 'L|XL' that it is a match and execute the code in the if.