Get Woocommerce customer order language

我的未来我决定 提交于 2021-01-29 00:42:21

问题


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?

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


回答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'


来源:https://stackoverflow.com/questions/29746821/get-woocommerce-customer-order-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!