Monday, February 6, 2017

how to convert weird text into specific word

Leave a Comment

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.

2 Answers

Answers 1

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

Answers 2

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.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment