Change the comma between the values added inside the same field into something else

Closed
ImportWP Pro - WordPress XML & CSV Importer ImportWP Pro - WordPress XML & CSV Importer May 08, 2021
Login to reply
Marco PIERRARD
3 years ago

Hello,

Just tested and it's OK now i work as expected.

Thanks again

Regards

James Collings Support Agent
3 years ago

Dont know what happened to the function i pasted it doesnt work at all, can you try this one instead:


function iwp_ticket_30354_change_delimiter($str)

{
    $parts = explode(',', $str);
    $parts = array_filter(array_map('trim', $parts));
   
    $delimiter = ' et ';
    if (count($parts) > 2) {
        $last = array_pop($parts);
        $suffix = ' et ' . $last;
        $delimiter = ', ';
    }
    return implode($delimiter, $parts) . $suffix;
}

Marco PIERRARD
3 years ago

Hello,

I've just tested the code and it's working well when there is two tags the result in the field is "A et B" like expected but there is problem when the tag is not there (or empty) or there is only one tag.

When the tag is not there or empty, nothing should change in the actual field (because of the code added previously inside the custom onix plugin) but the data in the field is remplace with " et ".

And if there is only one tag, the field is udpated with " et A" instead of just A.

Except when there is two tags (or more i guess I didn't test this case) I always have " et " before the data I should get.

Regards

James Collings Support Agent
3 years ago

To use the selector which you have:

{/DescriptiveDetail/Contributor[ContributorRole="A12"]/PersonName}")]

Yes you are correct in how you use it:

[iwp_ticket_30354_change_delimiter("{/DescriptiveDetail/Contributor[ContributorRole="A12"]/PersonName}")]

So if {/DescriptiveDetail/Contributor[ContributorRole="A12"]/PersonName} returns Multiple tags which are then combined with commas, the custom function iwp_ticket_30354_change_delimiter will parse these joining each with ' et '.

Marco PIERRARD
3 years ago

Hello,

Thank you for this code.
I'm not sure I understand how this new code works, perhaps i didn't explained weel what I neeeded.

This is an example of the value I have in the importer:

{/DescriptiveDetail/Contributor[ContributorRole="A19"]/PersonName}

Sometimes I can have more that one item with this same tag and in that case I have A,B (assuming the data inside the two tags are A and B).

The code looks nice to me but i'm confused by the last part of your message.

If I put the code and put this in the value of the importer is it good ?

[iwp_ticket_30354_change_delimiter("{/DescriptiveDetail/Contributor[ContributorRole="A12"]/PersonName}")]

With your examples it looks like A, B and C are different paths, in my cas it's the same path but it can have more than one value. But perhaps i'm missing something.

Regards

James Collings Support Agent
3 years ago

Hi Marco,

If you add the custom function to the plugin file i made:

function iwp_ticket_30354_change_delimiter($str)

{
    $parts = explode(',', $str);
    $parts = array_filter(array_map('trim', $parts));
    $suffix = '';
    if ($parts > 2) {
        $last = array_pop($parts);
        $suffix = ' et ' . $last;
    }
    return implode(', ', $parts) . $suffix;
}

To use the custom function xml for example if you have a field that is currently importing {/xpath} change it to [iwp_ticket_30354_change_delimiter("{/xpath}")]

When importing it will convert it to the following:

[iwp_ticket_30354_change_delimiter("A")] = A

[iwp_ticket_30354_change_delimiter("A,B")] = A et B

[iwp_ticket_30354_change_delimiter("A,B,C")] = A, B et C

...

Hopefully this is what you meant, i can remove the "A,b et C" and make it "A et B et C" if you prefer.

James

Marco PIERRARD
3 years ago

Hello,

Sometimes, I can have two times the same tag that goes inside an ACF field.
In this case the import add the two values let's say it's "A" and "B" inside the field like that "A,B".

What I want is change the comma with something else.
It will be " et " (" and ") in french to have "A et B".

Is it this something that could be done with a little bit of code ?

Thank you

Regards