Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert weird text into specific word

Tags:

php

wordpress

I am working on woocommerce api to add order manually.

I have ordered variation product manually and it shows good in edit order page in admin side.

Now, problem is the site using polylang plugin.

In that, there is two language. I can successfully add order in english language.

But when I tried to add product in another language(arabic). It returns some order details in weird text format. In my API it returns :

"product_variation_details": "%d8%a7%d9%84%d8%ad%d8%ac%d9%85: صغير"

In edit order page it shows in proper way: enter image description here

I have used below code to get order details in API:

$variation_id = $single_items['item_meta']['_variation_id'][0];
if ($variation_id != 0) {
    $variation = wc_get_product($variation_id);                
    $product_variation_details = wc_get_formatted_variation($variation->get_variation_attributes(), true);
}

I have search a lot but cant get better solution. any help would be apriciated. thanks in advance.

like image 720
vrajesh Avatar asked Dec 18 '22 10:12

vrajesh


2 Answers

It seems that there is some decoding going on. Urldecode

If I'm taking the given string and print it urldecoded it returns this:

print urldecode("%d8%a7%d9%84%d8%ad%d8%ac%d9%85");
الحجم 

I hope this helps

like image 199
Florian Avatar answered Jan 05 '23 16:01

Florian


Try replacing everything inside your if with:

$variation = wc_get_product( $variation_id );
$variation_attributes = $variation->get_variation_attributes();
$variation_attributes_decoded = array();

foreach ( $variation_attributes as $name => $value ) {
  $decoded_name = rawurldecode( $name );
  $variation_attributes_decoded[ $decoded_name ] = $value;
}

$product_variation_details = wc_get_formatted_variation( $variation_attributes_decoded, true );

This is untested.

This is the line from wc_get_formatted_variation() that is outputting your text in question:

$variation_list[] = wc_attribute_label( str_replace( 'attribute_', '', $name ) ) . ': ' . rawurldecode( $value );

As you can see, it's decoding the $value but not the $name. My solution should decode the $name ahead of time.

Edit: just fixed a code error.

like image 41
pants Avatar answered Jan 05 '23 15:01

pants