Get Woocommerce customer order language

I’m developing a complementary plugin for woocommerce.
I have a sql request that gets all the order and customer info, but i need to get the language from the order.

How can i detect the language was using a customer when he made an order? Is it registered somewhere?

Read More

In other CMS like prestashop it’s stored as id_lang in orders and customer tables.

Related posts

Leave a Reply

2 comments

  1. Without getting into which plugin you will chose and how it operates, here is how you would save some extra data to the order.

    // save the extra field when checkout is processed
    function kia_save_extra_checkout_fields( $order_id, $posted ){
        $language = detect_language_with_your_plugin_of_choice() ? detect_language_with_your_plugin_of_choice() : "en";
        update_post_meta( $order_id, '_order_language', $language );
    }
    add_action( 'woocommerce_checkout_update_order_meta', 'kia_save_extra_checkout_fields', 10, 2 );
    

    And because I had an awful time the one time I tried to use WPML, maybe consider checking out Multilingual Press.

  2. Finally solved, both solutions are OK.

    With WPML plugin you can get the value in the table postmeta with the meta_key = wpml_language

    Just added a left join in my SQL query:

    SELECT O.ID as id_order, O.post_date_gmt as date, M.meta_value as email, M2.meta_value as firstname, M3.meta_value as lastname, M4.meta_value as iso_code
        FROM ".$prefix."posts O 
        LEFT JOIN ".$prefix."postmeta M ON M.post_id = O.ID AND M.meta_key = '_billing_email'
        LEFT JOIN ".$prefix."postmeta M2 ON M2.post_id = O.ID AND M2.meta_key = '_billing_first_name'
        LEFT JOIN ".$prefix."postmeta M3 ON M3.post_id = O.ID AND M3.meta_key = '_billing_last_name'
        LEFT JOIN ".$prefix."postmeta M4 ON M3.post_id = O.ID AND M4.meta_key = 'wpml_language'
        WHERE O.post_type = 'shop_order' AND O.post_status = 'wc-completed'