Wp All Import - Exclude product id from title

356 views Asked by At

I want to import products by XML but the products has ids at the and of the titles,

here is a short title for example: “Levis bootcut jean 5450313”,

the id is “5450313”,

how can i Exclude product ids and import a clear title: “Levis bootcut jean”,

i have an example function but i don't know how to modify for my case:

function my_fix_title( $title ) {
$title = explode( ' ', $title );
array_pop( $title );
return implode( ' ', $title ); }

Wp All Import call the function like this: [my_fix_title({product_name[1]})]

Regards

2

There are 2 answers

6
Sameer Kumar Jain On

array_pop delete the last element of the array and return it. So we can simply catch the return value from array_pop and send it back to the caller function. Please try following

function my_fix_title( $title ) {
$title = explode( ' ', $title );
$id = array_pop( $title );
return $id; 
}

UPDATES: Since the OP had an issue with the cache and the method he has worked fine, this answer is no longer valid.

2
Paul T. On

Could also use preg_replace too:

$title1 = 'some product 24556';
$title2 = 'another product 56789';

function my_fix_title( $title ) {
    $fixedTitle = preg_replace('/[ ]\d+$/', '', $title);
    return $fixedTitle;
}

echo my_fix_title($title1);
echo '<br>';
echo my_fix_title($title2);

Output:

some product
another product

Example fiddle

The '/[ ]\d+$/' explanation:

/   // Start pattern
[ ] // A space
\d+ // One or more digits
 $  // At the end of the string
/   // End pattern