In WooCommerce I can get the variaation name with:
$variation_obj = wc_get_product($variation['variation_id']);
variation_name = $variation_obj->get_name();
This gives me an output with the following pattern: ” – ” but I just need the Variation
attribute output. Examples are:
“Refresh Spray – 250 ml”
“Refresh Spray – 500 ml”
“Refresh Spray – 1 l”
How can I remove the ” – ” and everything in front of it?
3
Answers
There are various different methods for doing this kind of thing but my trick is the code below.
this should do exactly what you want.
You can search for the last occurrence and get the string after that:
and when you want to remove any whitespaces use trim afterwards:
If you want to remove everything before “-” including it:
But if you want to remove everything after including the “-“: