/inc/class-wcml-orders.php

  1. <?php 
  2. class WCML_Orders{ 
  3.  
  4. private $woocommerce_wpml; 
  5. private $sitepress; 
  6.  
  7. private $standart_order_notes = array('Order status changed from %s to %s.',  
  8. 'Order item stock reduced successfully.', 'Item #%s stock reduced from %s to %s.', 'Item #%s stock increased from %s to %s.', 'Awaiting BACS payment', 'Awaiting cheque payment', 'Payment to be made upon delivery.',  
  9. 'Validation error: PayPal amounts do not match (gross %s).', 'Validation error: PayPal IPN response from a different email address (%s).', 'Payment pending: %s',  
  10. 'Payment %s via IPN.', 'Validation error: PayPal amounts do not match (amt %s).', 'IPN payment completed', 'PDT payment completed' 
  11. ); 
  12.  
  13. public function __construct( &$woocommerce_wpml, &$sitepress ) { 
  14. $this->woocommerce_wpml = $woocommerce_wpml; 
  15. $this->sitepress = $sitepress; 
  16.  
  17. add_action('init', array($this, 'init')); 
  18.  
  19. //checkout page 
  20. add_action( 'wp_ajax_woocommerce_checkout', array($this, 'switch_to_current'), 9); 
  21. add_action( 'wp_ajax_nopriv_woocommerce_checkout', array($this, 'switch_to_current'), 9); 
  22.  
  23. add_action( 'wp_ajax_wcml_order_delete_items', array( $this, 'order_delete_items' ) ); 
  24.  
  25. function init() { 
  26.  
  27. add_action('woocommerce_shipping_update_ajax', array($this, 'fix_shipping_update')); 
  28. add_action('woocommerce_checkout_update_order_meta', array($this, 'set_order_language')); 
  29.  
  30. add_filter('icl_lang_sel_copy_parameters', array($this, 'append_query_parameters')); 
  31.  
  32. add_filter('the_comments', array($this, 'get_filtered_comments')); 
  33. add_filter('gettext', array($this, 'filtered_woocommerce_new_order_note_data'), 10, 3); 
  34.  
  35. add_filter( 'woocommerce_order_get_items', array( $this, 'woocommerce_order_get_items' ), 10, 2 ); 
  36.  
  37. add_action( 'woocommerce_process_shop_order_meta', array( $this, 'set_order_language_backend'), 10, 2 ); 
  38. add_action( 'woocommerce_order_actions_start', array( $this, 'order_language_dropdown' ), 11 ); //after order currency drop-down 
  39.  
  40. //special case for wcml-741 
  41. add_action('updated_post_meta', array($this, 'update_order_currency'), 100, 4); 
  42.  
  43. add_action( 'woocommerce_before_order_itemmeta', array( $this, 'backend_before_order_itemmeta' ), 100, 3 ); 
  44. add_action( 'woocommerce_after_order_itemmeta', array( $this, 'backend_after_order_itemmeta' ), 100, 3 ); 
  45.  
  46.  
  47. function filtered_woocommerce_new_order_note_data($translations, $text, $domain ) { 
  48. if(in_array($text, $this->standart_order_notes)) { 
  49.  
  50. $language = $this->woocommerce_wpml->strings->get_string_language( $text, 'woocommerce' ); 
  51.  
  52. if( $this->sitepress->get_user_admin_language( get_current_user_id(), true ) != $language ) { 
  53.  
  54. $string_id = icl_get_string_id( $text, 'woocommerce'); 
  55. $strings = icl_get_string_translations_by_id( $string_id ); 
  56. if($strings) { 
  57. $translations = $strings[ $this->sitepress->get_user_admin_language( get_current_user_id(), true ) ]['value']; 
  58.  
  59. }else{ 
  60. return $text; 
  61.  
  62. return $translations; 
  63.  
  64. function get_filtered_comments($comments) { 
  65.  
  66. $user_id = get_current_user_id(); 
  67.  
  68. if( $user_id ) { 
  69.  
  70. $user_language = get_user_meta( $user_id, 'icl_admin_language', true ); 
  71.  
  72. foreach($comments as $key=>$comment) { 
  73.  
  74. $comment_string_id = icl_get_string_id( $comment->comment_content, 'woocommerce'); 
  75.  
  76. if($comment_string_id) { 
  77. $comment_strings = icl_get_string_translations_by_id( $comment_string_id ); 
  78. if($comment_strings) { 
  79. $comments[$key]->comment_content = $comment_strings[$user_language]['value']; 
  80. }  
  81.  
  82.  
  83. return $comments; 
  84.  
  85.  
  86. function woocommerce_order_get_items( $items, $order ) { 
  87.  
  88. if( isset( $_GET[ 'post' ] ) && get_post_type( $_GET[ 'post' ] ) == 'shop_order' ) { 
  89. // on order edit page use admin default language 
  90. $language_to_filter = $this->sitepress->get_user_admin_language( get_current_user_id(), true ); 
  91. }elseif( isset( $_GET[ 'action' ] ) && ( $_GET['action'] == 'woocommerce_mark_order_complete' || $_GET['action'] == 'woocommerce_mark_order_status' || $_GET['action'] == 'mark_processing') ) { 
  92. //backward compatibility for WC < 2.7 
  93. $order_id = method_exists( 'WC_Order', 'get_id' ) ? $order->get_id() : $order->id; 
  94. $order_language = get_post_meta( $order_id, 'wpml_language', true ); 
  95. $language_to_filter = $order_language ? $order_language : $this->sitepress->get_default_language(); 
  96. }else{ 
  97. $language_to_filter = $this->sitepress->get_current_language(); 
  98.  
  99. foreach( $items as $index => $item ) { 
  100. if( is_array( $item ) ) { 
  101. // WC < 2.7 
  102. foreach( $item as $key => $item_data ) { 
  103. if( $key == 'product_id' ) { 
  104. $tr_product_id = apply_filters( 'translate_object_id', $item_data, 'product', false, $language_to_filter ); 
  105. if( !is_null( $tr_product_id ) ) { 
  106. $items[ $index ][ $key ] = $tr_product_id; 
  107. $items[ $index ][ 'name'] = get_post( $tr_product_id )->post_title; 
  108. if( $key == 'variation_id' ) { 
  109. $tr_variation_id = apply_filters( 'translate_object_id', $item_data, 'product_variation', false, $language_to_filter ); 
  110. if( !is_null($tr_variation_id)) { 
  111. $items[$index][$key] = $tr_variation_id; 
  112.  
  113. if (substr($key, 0, 3) == 'pa_') { 
  114. //attr is taxonomy 
  115.  
  116. $term_id = $this->woocommerce_wpml->terms->wcml_get_term_id_by_slug( $key, $item_data ); 
  117. $tr_id = apply_filters( 'translate_object_id', $term_id, $key, false, $language_to_filter ); 
  118.  
  119. if(!is_null($tr_id)) { 
  120. $translated_term = $this->woocommerce_wpml->terms->wcml_get_term_by_id( $tr_id, $key); 
  121. $items[$index][$key] = $translated_term->slug; 
  122.  
  123. if( $key == 'type' && $item_data == 'shipping' && isset( $item[ 'method_id' ] ) ) { 
  124.  
  125. $items[ $index ][ 'name' ] = $this->woocommerce_wpml->shipping->translate_shipping_method_title( $item[ 'name' ], $item[ 'method_id' ], $language_to_filter ); 
  126.  
  127. }else{ 
  128. // WC >= 2.7 
  129. if( $item instanceof WC_Order_Item_Product ) { 
  130. if( $item->get_type() == 'line_item' ) { 
  131. $item_product_id = $item->get_product_id(); 
  132. if( get_post_type( $item_product_id ) == 'product_variation' ) { 
  133. $item_product_id = wp_get_post_parent_id( $item_product_id ); 
  134.  
  135. $tr_product_id = apply_filters( 'translate_object_id', $item_product_id, 'product', false, $language_to_filter ); 
  136. if( !is_null( $tr_product_id ) ) { 
  137. $item->set_product_id( $tr_product_id ); 
  138. $item->set_name( get_post( $tr_product_id )->post_title ); 
  139. $tr_variation_id = apply_filters( 'translate_object_id', $item->get_variation_id(), 'product_variation', false, $language_to_filter ); 
  140. if( !is_null( $tr_variation_id ) ) { 
  141. $item->set_variation_id( $tr_variation_id ); 
  142.  
  143. $meta_data = array(); 
  144. foreach( $item->get_meta_data() as $data ) { 
  145.  
  146. if( substr( $data->key, 0, 3) == 'pa_' ) { 
  147. $term_id = $this->woocommerce_wpml->terms->wcml_get_term_id_by_slug( $data->key, $data->value ); 
  148. $tr_id = apply_filters( 'translate_object_id', $term_id, $data->key, false, $language_to_filter ); 
  149.  
  150. if(!is_null($tr_id)) { 
  151. $translated_term = $this->woocommerce_wpml->terms->wcml_get_term_by_id( $tr_id, $data->key); 
  152. $data->value = $translated_term->slug; 
  153.  
  154. $meta_data[] = $data; 
  155.  
  156. }elseif( $item instanceof WC_Order_Item_Shipping ) { 
  157. if( $item->get_method_id() ) { 
  158. $item->set_method_title( $this->woocommerce_wpml->shipping->translate_shipping_method_title( $item->get_method_title(), $item->get_method_id(), $language_to_filter ) ); 
  159.  
  160. return $items; 
  161.  
  162.  
  163. public function backend_before_order_itemmeta( $item_id, $item, $product ) { 
  164. global $sitepress; 
  165.  
  166. if( $this->get_order_language_by_item_id( $item_id ) != $sitepress->get_user_admin_language( get_current_user_id(), true ) ) { 
  167. foreach( $item[ 'item_meta' ] as $key => $item_meta ) { 
  168. if ( taxonomy_exists( wc_attribute_taxonomy_name( $key ) ) || substr( $key, 0, 3 ) == 'pa_' ) { 
  169. //backward compatibility for WC < 2.7 - in WC 2.7 $item_meta is a single attribute value not an array 
  170. if( !is_array( $item_meta ) ) { 
  171. $item_meta = array( $item_meta ); 
  172.  
  173. foreach( $item_meta as $value ) { 
  174. $this->force_update_itemmeta( $item_id, $key, $value, $sitepress->get_user_admin_language( get_current_user_id(), true ) ); 
  175.  
  176. public function backend_after_order_itemmeta( $item_id, $item, $product ) { 
  177. global $sitepress; 
  178.  
  179. $order_languge = $this->get_order_language_by_item_id( $item_id ); 
  180. if( $order_languge != $sitepress->get_user_admin_language( get_current_user_id(), true ) ) { 
  181. foreach( $item[ 'item_meta' ] as $key => $item_meta ) { 
  182. if ( taxonomy_exists( wc_attribute_taxonomy_name( $key ) ) || substr( $key, 0, 3 ) == 'pa_' ) { 
  183. //backward compatibility for WC < 2.7 - in WC 2.7 $item_meta is a single attribute value not an array 
  184. if( !is_array( $item_meta ) ) { 
  185. $item_meta = array( $item_meta ); 
  186.  
  187. foreach( $item_meta as $value ) { 
  188. $this->force_update_itemmeta( $item_id, $key, $value, $order_languge ); 
  189.  
  190. public function get_order_language_by_item_id( $item_id ) { 
  191. global $wpdb; 
  192.  
  193. $order_id = $wpdb->get_var( $wpdb->prepare( "SELECT order_id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d", $item_id ) ); 
  194.  
  195. return get_post_meta( $order_id, 'wpml_language', true ); 
  196.  
  197. //force update to display attribute in correct language on edit order page 
  198. public function force_update_itemmeta( $item_id, $key, $value, $languge ) { 
  199. global $wpdb, $woocommerce_wpml; 
  200.  
  201. $taxonomy = substr( $key, 0, 3 ) != 'pa_' ? wc_attribute_taxonomy_name( $key ) : $key; 
  202. $term_id = $woocommerce_wpml->terms->wcml_get_term_id_by_slug( $taxonomy, $value ); 
  203. $translated_term = $woocommerce_wpml->terms->wcml_get_translated_term( $term_id, $taxonomy, $languge ); 
  204. $translated_term = $woocommerce_wpml->terms->wcml_get_translated_term( $term_id, $taxonomy, $languge ); 
  205.  
  206. if( $translated_term ) { 
  207. $value = $translated_term->slug; 
  208. $wpdb->update( $wpdb->prefix.'woocommerce_order_itemmeta', array( 'meta_value' => $value ), array( 'order_item_id' => $item_id, 'meta_key' => $key ) ); 
  209.  
  210. // Fix for shipping update on the checkout page. 
  211. function fix_shipping_update($amount) { 
  212. global $sitepress, $post; 
  213.  
  214. if($sitepress->get_current_language() !== $sitepress->get_default_language() && $post->ID == $this->checkout_page_id()) { 
  215.  
  216. $_SESSION['icl_checkout_shipping_amount'] = $amount; 
  217.  
  218. $amount = $_SESSION['icl_checkout_shipping_amount']; 
  219.  
  220.  
  221. return $amount; 
  222.  
  223.  
  224. /** 
  225. * Adds language to order post type. 
  226. *  
  227. * Language was stored in the session created on checkout page. 
  228. * See params(). 
  229. *  
  230. * @param type $order_id 
  231. */  
  232. function set_order_language($order_id) {  
  233. if(!get_post_meta($order_id, 'wpml_language')) { 
  234. $language = isset($_SESSION['wpml_globalcart_language']) ? $_SESSION['wpml_globalcart_language'] : ICL_LANGUAGE_CODE; 
  235. update_post_meta($order_id, 'wpml_language', $language); 
  236.  
  237. function append_query_parameters($parameters) { 
  238.  
  239. if(is_order_received_page() || is_checkout()) { 
  240. if(!in_array('order', $parameters)) $parameters[] = 'order'; 
  241. if(!in_array('key', $parameters)) $parameters[] = 'key'; 
  242.  
  243. return $parameters; 
  244.  
  245. function switch_to_current() { 
  246. $this->woocommerce_wpml->emails->change_email_language($this->sitepress->get_current_language()); 
  247.  
  248. function order_language_dropdown( $order_id ) { 
  249. if( !get_post_meta( $order_id, '_order_currency') ) { 
  250. $languages = apply_filters( 'wpml_active_languages', array(), array( 'skip_missing' => 0, 'orderby' => 'code' ) ); 
  251. $selected_lang = isset( $_COOKIE [ '_wcml_dashboard_order_language' ] ) ? $_COOKIE [ '_wcml_dashboard_order_language' ] : $this->sitepress->get_default_language(); 
  252. ?> 
  253. <li class="wide"> 
  254. <label><?php _e('Order language:'); ?></label> 
  255. <select id="dropdown_shop_order_language" name="wcml_shop_order_language"> 
  256. <?php if (!empty($languages)): ?> 
  257.  
  258. <?php foreach ($languages as $l): ?> 
  259.  
  260. <option 
  261. value="<?php echo $l['language_code'] ?>" <?php echo $selected_lang == $l['language_code'] ? 'selected="selected"' : ''; ?>><?php echo $l['translated_name']; ?></option> 
  262.  
  263. <?php endforeach; ?> 
  264.  
  265. <?php endif; ?> 
  266. </select> 
  267. </li> 
  268. <?php 
  269. $wcml_set_dashboard_order_language_nonce = wp_create_nonce( 'set_dashboard_order_language' ); 
  270. wc_enqueue_js( " 
  271. var order_lang_current_value = jQuery('#dropdown_shop_order_language option:selected').val(); 
  272.  
  273. jQuery('#dropdown_shop_order_language').on('change', function() { 
  274. if(confirm('" . esc_js(__("All the products will be removed from the current order in order to change the language", 'woocommerce-multilingual')). "')) { 
  275. var lang = jQuery(this).val(); 
  276.  
  277. jQuery.ajax({ 
  278. url: ajaxurl,  
  279. type: 'post',  
  280. dataType: 'json',  
  281. data: {action: 'wcml_order_delete_items', order_id: woocommerce_admin_meta_boxes.post_id, lang: lang , wcml_nonce: '".$wcml_set_dashboard_order_language_nonce."' },  
  282. success: function( response ) { 
  283. if(typeof response.error !== 'undefined') { 
  284. alert(response.error); 
  285. }else{ 
  286. window.location = window.location.href; 
  287. }); 
  288. }else{ 
  289. jQuery(this).val( order_lang_current_value ); 
  290. return false; 
  291. }); 
  292.  
  293. "); 
  294.  
  295. function order_delete_items() { 
  296. $nonce = filter_input( INPUT_POST, 'wcml_nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); 
  297. if(!$nonce || !wp_verify_nonce($nonce, 'set_dashboard_order_language')) { 
  298. echo json_encode(array('error' => __('Invalid nonce', 'woocommerce-multilingual'))); 
  299. die(); 
  300.  
  301. setcookie('_wcml_dashboard_order_language', filter_input( INPUT_POST, 'lang', FILTER_SANITIZE_FULL_SPECIAL_CHARS ), time() + 86400, COOKIEPATH, COOKIE_DOMAIN); 
  302.  
  303.  
  304. function set_order_language_backend( $post_id, $post ) { 
  305.  
  306. if( isset( $_POST['wcml_shop_order_language'] ) ) { 
  307. update_post_meta( $post_id, 'wpml_language', filter_input( INPUT_POST, 'wcml_shop_order_language', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ); 
  308.  
  309.  
  310. function update_order_currency( $meta_id, $object_id, $meta_key, $meta_value ) { 
  311.  
  312. if( $this->woocommerce_wpml->settings['enable_multi_currency'] == WCML_MULTI_CURRENCIES_INDEPENDENT && get_post_type($object_id) == 'shop_order' && isset( $_GET['wc-ajax'] ) && $_GET['wc-ajax'] == 'checkout' ) { 
  313. update_post_meta( $object_id, '_order_currency', get_woocommerce_currency() ); 
  314.  
  315.  
  316.  
.