WCML_REST_API_Support

The WooCommerce Multilingual WCML REST API Support class.

Defined (2)

The class is defined in the following location(s).

/inc/rest-api-support/class-wcml-rest-api-suport.php  
  1. class WCML_REST_API_Support{ 
  2.  
  3. /** @var woocommerce_wpml */ 
  4. private $woocommerce_wpml; 
  5. /** @var Sitepress */ 
  6. private $sitepress; 
  7.  
  8. function __construct( &$woocommerce_wpml, &$sitepress ) { 
  9.  
  10. $this->woocommerce_wpml =& $woocommerce_wpml; 
  11. $this->sitepress =& $sitepress; 
  12.  
  13. $this->prevent_default_lang_url_redirect(); 
  14.  
  15. add_action( 'rest_api_init', array( $this, 'set_language_for_request' ) ); 
  16.  
  17. add_action( 'parse_query', array($this, 'auto_adjust_included_ids') ); 
  18.  
  19. // Products 
  20. add_action( 'woocommerce_rest_prepare_product', array( $this, 'append_product_language_and_translations' ) ); 
  21. add_action( 'woocommerce_rest_prepare_product', array( $this, 'append_product_secondary_prices' ) ); 
  22.  
  23. add_filter( 'woocommerce_rest_product_query', array( $this, 'filter_products_query' ), 10, 2 ); 
  24.  
  25.  
  26. add_action( 'woocommerce_rest_insert_product', array( $this, 'set_product_language' ), 10 , 2 ); 
  27. add_action( 'woocommerce_rest_update_product', array( $this, 'set_product_language' ), 10 , 2 ); 
  28.  
  29. add_action( 'woocommerce_rest_insert_product', array( $this, 'set_product_custom_prices' ), 10 , 2 ); 
  30. add_action( 'woocommerce_rest_update_product', array( $this, 'set_product_custom_prices' ), 10 , 2 ); 
  31.  
  32. // Orders 
  33. add_filter( 'woocommerce_rest_shop_order_query', array( $this, 'filter_orders_by_language' ), 20, 2 ); 
  34. add_action( 'woocommerce_rest_prepare_shop_order', array( $this, 'filter_order_items_by_language'), 10, 3 ); 
  35. add_action( 'woocommerce_rest_insert_shop_order' , array( $this, 'set_order_language' ), 10, 2 ); 
  36.  
  37. // Terms 
  38. add_action( 'woocommerce_rest_product_cat_query', array($this, 'filter_terms_query' ), 10, 2 ); 
  39. add_action( 'woocommerce_rest_product_tag_query', array($this, 'filter_terms_query' ), 10, 2 ); 
  40.  
  41.  
  42. /** 
  43. * @param $wp_rest_server 
  44. * enforces the language of request as the current language to be able to filter items by language 
  45. */ 
  46. public function set_language_for_request( $wp_rest_server ) { 
  47. if( isset( $_GET['lang'] ) ) { 
  48. $request_language = $_GET['lang']; 
  49. $active_languages = $this->sitepress->get_active_languages(); 
  50. if( isset( $active_languages[ $request_language ] ) ) { 
  51. $this->sitepress->switch_lang( $request_language ); 
  52.  
  53.  
  54. /** 
  55. * Check if is request to the WooCommerce REST API. 
  56. * @return bool 
  57. */ 
  58. protected static function is_request_to_rest_api() { 
  59. if ( empty( $_SERVER['REQUEST_URI'] ) ) { 
  60. return false; 
  61.  
  62. $rest_prefix = trailingslashit( rest_get_url_prefix() ); 
  63. // Check if WooCommerce endpoint. 
  64. $woocommerce = ( false !== strpos( $_SERVER['REQUEST_URI'], $rest_prefix . 'wc/' ) ); 
  65.  
  66. return apply_filters( 'woocommerce_rest_is_request_to_rest_api', $woocommerce ); 
  67.  
  68. /** 
  69. * Prevent WPML redirection when using the default language as a parameter in the url 
  70. */ 
  71. private function prevent_default_lang_url_redirect() { 
  72.  
  73. if( self::is_request_to_rest_api() ) { 
  74. $exp = explode( '?', $_SERVER['REQUEST_URI'] ); 
  75. if ( ! empty( $exp[1] ) ) { 
  76. parse_str( $exp[1], $vars ); 
  77. if ( isset($vars['lang']) && $vars['lang'] === $this->sitepress->get_default_language() ) { 
  78. unset( $vars['lang'] ); 
  79. $_SERVER['REQUEST_URI'] = $exp[0] . '?' . http_build_query( $vars ); 
  80.  
  81. // Use url without the language parameter. Needed for the signature match. 
  82. public static function remove_wpml_global_url_filters() { 
  83. if( self::is_request_to_rest_api() ) { 
  84. global $wpml_url_filters; 
  85. remove_filter( 'home_url', array( $wpml_url_filters, 'home_url_filter' ), - 10 ); 
  86.  
  87. /** 
  88. * When lang=all don't filter products by language 
  89. * @param array $args 
  90. * @param WP_REST_Request $request 
  91. * @return array 
  92. */ 
  93. public function filter_products_query( $args, $request ) { 
  94. $data = $request->get_params(); 
  95. if( isset( $data['lang'] ) && $data['lang'] === 'all' ) { 
  96. global $wpml_query_filter; 
  97. remove_filter( 'posts_join', array( $wpml_query_filter, 'posts_join_filter' ), 10 ); 
  98. remove_filter( 'posts_where', array( $wpml_query_filter, 'posts_where_filter' ), 10 ); 
  99. return $args; 
  100.  
  101. /** 
  102. * When lang=all don't filter terms by language 
  103. * @param array $args 
  104. * @param WP_REST_Request $request 
  105. * @return array 
  106. */ 
  107. public function filter_terms_query( $args, $request ) { 
  108. $data = $request->get_params(); 
  109. if( isset( $data['lang'] ) && $data['lang'] === 'all' ) { 
  110. remove_filter( 'terms_clauses', array( $this->sitepress, 'terms_clauses' ), 10, 4 ); 
  111. remove_filter( 'get_term', array( $this->sitepress, 'get_term_adjust_id' ), 1, 1 ); 
  112. return $args; 
  113.  
  114. /** 
  115. * @param WP_Query $wp_query 
  116. */ 
  117. public function auto_adjust_included_ids( $wp_query ) { 
  118. $lang = $wp_query->get('lang'); 
  119. $include = $wp_query->get('post__in'); 
  120. if( empty( $lang ) && !empty( $include ) ) { 
  121. $filtered_include = array(); 
  122. foreach( $include as $id ) { 
  123. $filtered_include[] = apply_filters( 'translate_object_id', $id, get_post_type($id), true ); 
  124. $wp_query->set( 'post__in' , $filtered_include ); 
  125.  
  126. /** 
  127. * Appends the language and translation information to the get_product response 
  128. * @param $product_data 
  129. */ 
  130. public function append_product_language_and_translations( $product_data ) { 
  131.  
  132. $product_data->data['translations'] = array(); 
  133.  
  134. $trid = $this->sitepress->get_element_trid( $product_data->data['id'], 'post_product' ); 
  135.  
  136. if( $trid ) { 
  137. $translations = $this->sitepress->get_element_translations( $trid, 'post_product' ); 
  138. foreach ( $translations as $translation ) { 
  139. if ( $translation->element_id == $product_data->data['id'] ) { 
  140. $product_language = $translation->language_code; 
  141. } else { 
  142. $product_data->data['translations'][ $translation->language_code ] = $translation->element_id; 
  143.  
  144. $product_data->data['lang'] = $product_language; 
  145.  
  146. return $product_data; 
  147.  
  148. /** 
  149. * Appends the secondary prices information to the get_product response 
  150. * @param $product_data 
  151. */ 
  152. public function append_product_secondary_prices( $product_data ) { 
  153.  
  154. if( !empty($this->woocommerce_wpml->multi_currency) && !empty($this->woocommerce_wpml->settings['currencies_order']) ) { 
  155.  
  156. $product_data->data['multi-currency-prices'] = array(); 
  157.  
  158. $custom_prices_on = get_post_meta( $product_data->data['id'], '_wcml_custom_prices_status', true); 
  159.  
  160. foreach( $this->woocommerce_wpml->settings['currencies_order'] as $currency ) { 
  161.  
  162. if( $currency != get_option('woocommerce_currency') ) { 
  163.  
  164. if( $custom_prices_on ) { 
  165.  
  166. $custom_prices = (array) $this->woocommerce_wpml->multi_currency->custom_prices->get_product_custom_prices( $product_data->data['id'], $currency ); 
  167. foreach( $custom_prices as $key => $price) { 
  168. $product_data->data['multi-currency-prices'][$currency][ preg_replace('#^_#', '', $key) ] = $price; 
  169.  
  170.  
  171. } else { 
  172. $product_data->data['multi-currency-prices'][$currency]['regular_price'] = 
  173. $this->woocommerce_wpml->multi_currency->prices->raw_price_filter( $product_data->data['regular_price'], $currency ); 
  174. if( !empty($product_data->data['sale_price']) ) { 
  175. $product_data->data['multi-currency-prices'][$currency]['sale_price'] = 
  176. $this->woocommerce_wpml->multi_currency->prices->raw_price_filter( $product_data->data['sale_price'], $currency ); 
  177.  
  178.  
  179.  
  180.  
  181. return $product_data; 
  182.  
  183. /** 
  184. * Sets the product information according to the provided language 
  185. * @param WP_Post $post 
  186. * @param WP_REST_Request $request 
  187. * @throws WC_API_Exception 
  188. */ 
  189. public function set_product_language( $post, $request ) { 
  190.  
  191. $data = $request->get_params(); 
  192.  
  193. if( isset( $data['lang'] )) { 
  194. $active_languages = $this->sitepress->get_active_languages(); 
  195. if( !isset( $active_languages[$data['lang']] ) ) { 
  196. throw new WC_REST_Exception( '404', sprintf( __( 'Invalid language parameter: %s', 'woocommerce-multilingual' ), $data['lang'] ), '404' ); 
  197. if( isset( $data['translation_of'] ) ) { 
  198. $trid = $this->sitepress->get_element_trid( $data['translation_of'], 'post_product' ); 
  199. if( empty($trid) ) { 
  200. throw new WC_REST_Exception( '404', sprintf( __( 'Source product id not found: %s', 'woocommerce-multilingual' ), $data['translation_of'] ), '404' ); 
  201. }else{ 
  202. $trid = null; 
  203. $this->sitepress->set_element_language_details( $post->ID, 'post_product', $trid, $data['lang'] ); 
  204. wpml_tm_save_post( $post->ID, $post , ICL_TM_COMPLETE ); 
  205. }else{ 
  206. if( isset( $data['translation_of'] ) ) { 
  207. throw new WC_REST_Exception( '404', __( 'Using "translation_of" requires providing a "lang" parameter too', 'woocommerce-multilingual' ), '404' ); 
  208.  
  209.  
  210. /** 
  211. * Sets custom prices in secondary currencies for products 
  212. * @param WP_Post $post 
  213. * @param WP_REST_Request $request 
  214. * @throws WC_API_Exception 
  215. */ 
  216. public function set_product_custom_prices( $post, $request ) { 
  217.  
  218. $data = $request->get_params(); 
  219.  
  220. if( !empty( $this->woocommerce_wpml->multi_currency ) ) { 
  221.  
  222. if( !empty( $data['custom_prices'] ) ) { 
  223.  
  224. $original_post_id = $this->sitepress->get_original_element_id_filter('', $post->ID, 'post_product' ); 
  225.  
  226. update_post_meta( $original_post_id, '_wcml_custom_prices_status', 1); 
  227.  
  228. foreach( $data['custom_prices'] as $currency => $prices ) { 
  229.  
  230. $prices_uscore = array(); 
  231. foreach( $prices as $k => $p) { 
  232. $prices_uscore['_' . $k] = $p; 
  233. $this->woocommerce_wpml->multi_currency->custom_prices->update_custom_prices( $original_post_id, $prices_uscore, $currency ); 
  234.  
  235.  
  236.  
  237.  
  238. public function filter_orders_by_language( $args, $request ) { 
  239.  
  240. $lang = $request->get_param( 'lang' ); 
  241.  
  242. if( !is_null( $lang ) && $lang !== 'all' ) { 
  243.  
  244. $args['meta_query'][] = array( 
  245. 'key' => 'wpml_language',  
  246. 'value' => strval( $lang ) 
  247. ); 
  248.  
  249.  
  250. return $args; 
  251.  
  252. /** 
  253. * Filters the items of an order according to a given languages 
  254. * @param $order_data 
  255. * @param $order 
  256. * @param $fields 
  257. * @param $server 
  258. * @return mixed 
  259. */ 
  260.  
  261. public function filter_order_items_by_language( $response, $order, $request ) { 
  262.  
  263. $lang = get_query_var('lang'); 
  264.  
  265. $order_lang = get_post_meta( $order->ID, 'wpml_language', true ); 
  266.  
  267. if( $order_lang != $lang ) { 
  268.  
  269. foreach( $response->data['line_items'] as $k => $item ) { 
  270.  
  271. global $wpdb; 
  272. $sql = "SELECT meta_value FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id=%d AND meta_key='_product_id'"; 
  273. $product_id = $wpdb->get_var( $wpdb->prepare( $sql, $item['id']) ); 
  274.  
  275. if( $product_id ) { 
  276.  
  277. $translated_product_id = apply_filters( 'translate_object_id', $product_id, 'product', true, $lang ); 
  278.  
  279. if( $translated_product_id ) { 
  280. $translated_product = get_post( $translated_product_id ); 
  281. $response->data['line_items'][$k]['product_id'] = $translated_product_id; 
  282. if( $translated_product->post_type == 'product_variation' ) { 
  283. $post_parent = get_post( $translated_product->post_parent ); 
  284. $post_name = $post_parent->post_title; 
  285. } else { 
  286. $post_name = $translated_product->post_title; 
  287. $response->data['line_items'][$k]['name'] = $post_name; 
  288.  
  289.  
  290.  
  291.  
  292. return $response; 
  293.  
  294. /** 
  295. * Sets the language for a new order 
  296. * @param $order_id 
  297. * @param $data 
  298. * @throws WC_API_Exception 
  299. */ 
  300. public function set_order_language( $post, $request ) { 
  301.  
  302. $data = $request->get_params(); 
  303. if( isset( $data['lang'] ) ) { 
  304. $order_id = $post->ID; 
  305. $active_languages = $this->sitepress->get_active_languages(); 
  306. if( !isset( $active_languages[$data['lang']] ) ) { 
  307. throw new WC_REST_Exception( '404', sprintf( __( 'Invalid language parameter: %s' ), $data['lang'] ), '404' ); 
  308.  
  309. update_post_meta( $order_id, 'wpml_language', $data['lang'] ); 
  310.  
  311.  
  312.  
  313.  
  314.  
/inc/rest-api-support/class-wcml-rest-api-support.php  
  1. class WCML_REST_API_Support{ 
  2.  
  3. /** @var woocommerce_wpml */ 
  4. private $woocommerce_wpml; 
  5. /** @var Sitepress */ 
  6. private $sitepress; 
  7.  
  8. function __construct( &$woocommerce_wpml, &$sitepress ) { 
  9. $this->woocommerce_wpml =& $woocommerce_wpml; 
  10. $this->sitepress =& $sitepress; 
  11.  
  12. /** 
  13. * Adding hooks 
  14. */ 
  15. public function initialize() { 
  16.  
  17. $this->prevent_default_lang_url_redirect(); 
  18.  
  19. add_action( 'rest_api_init', array( $this, 'set_language_for_request' ) ); 
  20.  
  21. add_action( 'parse_query', array($this, 'auto_adjust_included_ids') ); 
  22.  
  23. // Products 
  24. add_action( 'woocommerce_rest_prepare_product_object', array( $this, 'append_product_language_and_translations' ) ); 
  25. add_action( 'woocommerce_rest_prepare_product_object', array( $this, 'append_product_secondary_prices' ) ); 
  26. add_filter( 'woocommerce_rest_product_query', array( $this, 'filter_products_query' ), 10, 2 ); 
  27. add_action( 'woocommerce_rest_insert_product_object', array( $this, 'set_product_language' ), 10, 2 ); 
  28. add_action( 'woocommerce_rest_insert_product_object', array( $this, 'set_product_custom_prices' ), 10, 2 ); 
  29. add_action( 'woocommerce_rest_prepare_product_object', array( $this, 'copy_product_custom_fields' ), 10 , 3 ); 
  30.  
  31. // Orders 
  32. add_filter( 'woocommerce_rest_shop_order_object_query', array( $this, 'filter_orders_by_language' ), 20, 2 ); 
  33. add_action( 'woocommerce_rest_prepare_shop_order_object', array( $this, 'filter_order_items_by_language'), 10, 3 ); 
  34. add_action( 'woocommerce_rest_insert_shop_order_object' , array( $this, 'set_order_language' ), 10, 2 ); 
  35.  
  36. // Terms 
  37. add_action( 'woocommerce_rest_product_cat_query', array($this, 'filter_terms_query' ), 10, 2 ); 
  38. add_action( 'woocommerce_rest_product_tag_query', array($this, 'filter_terms_query' ), 10, 2 ); 
  39.  
  40.  
  41. /** 
  42. * Check if is request to the WooCommerce REST API. 
  43. * @return bool 
  44. */ 
  45. public static function is_rest_api_request() { 
  46.  
  47. if ( empty( $_SERVER['REQUEST_URI'] ) ) { 
  48. return false; 
  49.  
  50. $rest_prefix = trailingslashit( rest_get_url_prefix() ); 
  51. // Check if WooCommerce endpoint. 
  52. $woocommerce = ( false !== strpos( $_SERVER['REQUEST_URI'], $rest_prefix . 'wc/' ) ); 
  53.  
  54. return apply_filters( 'woocommerce_rest_is_request_to_rest_api', $woocommerce ); 
  55.  
  56.  
  57. /** 
  58. * @return int 
  59. * Returns the version number of the API used for the current request 
  60. */ 
  61. public static function get_api_request_version() { 
  62. $version = 0; 
  63. $rest_prefix = trailingslashit( rest_get_url_prefix() ); 
  64. if( preg_match( "@" . $rest_prefix . "wc/v([0-9]+)/@", $_SERVER['REQUEST_URI'], $matches ) ) { 
  65. $version = intval($matches[1]); 
  66. return $version; 
  67.  
  68. /** 
  69. * @param WP_REST_Server $wp_rest_server 
  70. * enforces the language of request as the current language to be able to filter items by language 
  71. */ 
  72. public function set_language_for_request( $wp_rest_server ) { 
  73. if( isset( $_GET['lang'] ) ) { 
  74. $request_language = $_GET['lang']; 
  75. $active_languages = $this->sitepress->get_active_languages(); 
  76. if( isset( $active_languages[ $request_language ] ) ) { 
  77. $this->sitepress->switch_lang( $request_language ); 
  78.  
  79. /** 
  80. * Prevent WPML redirection when using the default language as a parameter in the url 
  81. */ 
  82. private function prevent_default_lang_url_redirect() { 
  83. $exp = explode( '?', $_SERVER['REQUEST_URI'] ); 
  84. if ( ! empty( $exp[1] ) ) { 
  85. parse_str( $exp[1], $vars ); 
  86. if ( isset($vars['lang']) && $vars['lang'] === $this->sitepress->get_default_language() ) { 
  87. unset( $vars['lang'] ); 
  88. $_SERVER['REQUEST_URI'] = $exp[0] . '?' . http_build_query( $vars ); 
  89.  
  90. // Use url without the language parameter. Needed for the signature match. 
  91. public static function remove_wpml_global_url_filters() { 
  92. global $wpml_url_filters; 
  93. remove_filter( 'home_url', array( $wpml_url_filters, 'home_url_filter' ), - 10 ); 
  94.  
  95. /** 
  96. * When lang=all don't filter products by language 
  97. * @param array $args 
  98. * @param WP_REST_Request $request 
  99. * @return array 
  100. */ 
  101. public function filter_products_query( $args, $request ) { 
  102. $data = $request->get_params(); 
  103. if( isset( $data['lang'] ) && $data['lang'] === 'all' ) { 
  104. global $wpml_query_filter; 
  105. remove_filter( 'posts_join', array( $wpml_query_filter, 'posts_join_filter' ), 10 ); 
  106. remove_filter( 'posts_where', array( $wpml_query_filter, 'posts_where_filter' ), 10 ); 
  107. return $args; 
  108.  
  109. /** 
  110. * When lang=all don't filter terms by language 
  111. * @param array $args 
  112. * @param WP_REST_Request $request 
  113. * @return array 
  114. * @throws WC_REST_Exception 
  115. */ 
  116. public function filter_terms_query( $args, $request ) { 
  117.  
  118. $data = $request->get_params(); 
  119.  
  120. if ( isset( $data['lang'] ) ) { 
  121.  
  122. $active_languages = $this->sitepress->get_active_languages(); 
  123.  
  124. if ( $data['lang'] === 'all' ) { 
  125. remove_filter( 'terms_clauses', array( $this->sitepress, 'terms_clauses' ), 10, 4 ); 
  126. remove_filter( 'get_term', array( $this->sitepress, 'get_term_adjust_id' ), 1, 1 ); 
  127. } elseif ( ! isset( $active_languages[ $data['lang'] ] ) ) { 
  128. throw new WC_REST_Exception( 
  129. '404',  
  130. sprintf( __( 'Invalid language parameter: %s', 'woocommerce-multilingual' ),  
  131. $data['lang'] ),  
  132. '404' 
  133. ); 
  134.  
  135.  
  136. return $args; 
  137.  
  138. /** 
  139. * @param WP_Query $wp_query 
  140. */ 
  141. public function auto_adjust_included_ids( $wp_query ) { 
  142. $lang = $wp_query->get('lang'); 
  143. $include = $wp_query->get('post__in'); 
  144. if( empty( $lang ) && !empty( $include ) ) { 
  145. $filtered_include = array(); 
  146. foreach( $include as $id ) { 
  147. $filtered_include[] = apply_filters( 'translate_object_id', $id, get_post_type($id), true ); 
  148. $wp_query->set( 'post__in' , $filtered_include ); 
  149.  
  150. /** 
  151. * Appends the language and translation information to the get_product response 
  152. * @param $product_data 
  153. * @return WP_REST_Response 
  154. */ 
  155. public function append_product_language_and_translations( $product_data ) { 
  156.  
  157. $product_data->data['translations'] = array(); 
  158.  
  159. $trid = $this->sitepress->get_element_trid( $product_data->data['id'], 'post_product' ); 
  160.  
  161. if( $trid ) { 
  162. $translations = $this->sitepress->get_element_translations( $trid, 'post_product' ); 
  163. foreach ( $translations as $translation ) { 
  164. if ( $translation->element_id == $product_data->data['id'] ) { 
  165. $product_language = $translation->language_code; 
  166. } else { 
  167. $product_data->data['translations'][ $translation->language_code ] = $translation->element_id; 
  168.  
  169. $product_data->data['lang'] = $product_language; 
  170.  
  171. return $product_data; 
  172.  
  173. /** 
  174. * Appends the secondary prices information to the get_product response 
  175. * @param $product_data 
  176. * @return WP_REST_Response 
  177. */ 
  178. public function append_product_secondary_prices( $product_data ) { 
  179.  
  180. if( !empty($this->woocommerce_wpml->multi_currency) && !empty($this->woocommerce_wpml->settings['currencies_order']) ) { 
  181.  
  182. $product_data->data['multi-currency-prices'] = array(); 
  183.  
  184. $custom_prices_on = get_post_meta( $product_data->data['id'], '_wcml_custom_prices_status', true); 
  185.  
  186. foreach( $this->woocommerce_wpml->settings['currencies_order'] as $currency ) { 
  187.  
  188. if( $currency != get_option('woocommerce_currency') ) { 
  189.  
  190. if( $custom_prices_on ) { 
  191.  
  192. $custom_prices = (array) $this->woocommerce_wpml->multi_currency->custom_prices->get_product_custom_prices( $product_data->data['id'], $currency ); 
  193. foreach( $custom_prices as $key => $price) { 
  194. $product_data->data['multi-currency-prices'][$currency][ preg_replace('#^_#', '', $key) ] = $price; 
  195.  
  196.  
  197. } else { 
  198. $product_data->data['multi-currency-prices'][$currency]['regular_price'] = 
  199. $this->woocommerce_wpml->multi_currency->prices->raw_price_filter( $product_data->data['regular_price'], $currency ); 
  200. if( !empty($product_data->data['sale_price']) ) { 
  201. $product_data->data['multi-currency-prices'][$currency]['sale_price'] = 
  202. $this->woocommerce_wpml->multi_currency->prices->raw_price_filter( $product_data->data['sale_price'], $currency ); 
  203.  
  204.  
  205.  
  206.  
  207. return $product_data; 
  208.  
  209. /** 
  210. * Sets the product information according to the provided language 
  211. * @param object $product 
  212. * @param WP_REST_Request $request 
  213. * @throws WC_REST_Exception 
  214. */ 
  215. public function set_product_language( $product, $request ) { 
  216.  
  217. $data = $request->get_params(); 
  218.  
  219. if( isset( $data['lang'] )) { 
  220. $active_languages = $this->sitepress->get_active_languages(); 
  221. if( !isset( $active_languages[$data['lang']] ) ) { 
  222. throw new WC_REST_Exception( '404', sprintf( __( 'Invalid language parameter: %s', 'woocommerce-multilingual' ), $data['lang'] ), '404' ); 
  223. if( isset( $data['translation_of'] ) ) { 
  224. $trid = $this->sitepress->get_element_trid( $data['translation_of'], 'post_product' ); 
  225. if( empty($trid) ) { 
  226. throw new WC_REST_Exception( '404', sprintf( __( 'Source product id not found: %s', 'woocommerce-multilingual' ), $data['translation_of'] ), '404' ); 
  227. }else{ 
  228. $trid = null; 
  229.  
  230. $this->sitepress->set_element_language_details( $product->get_id(), 'post_product', $trid, $data['lang'] ); 
  231. wpml_tm_save_post( $product->get_id(), get_post( $product->get_id() ), ICL_TM_COMPLETE ); 
  232. }else{ 
  233. if( isset( $data['translation_of'] ) ) { 
  234. throw new WC_REST_Exception( '404', __( 'Using "translation_of" requires providing a "lang" parameter too', 'woocommerce-multilingual' ), '404' ); 
  235.  
  236.  
  237. /** 
  238. * Sets custom prices in secondary currencies for products 
  239. * @param object $product 
  240. * @param WP_REST_Request $request 
  241. * @throws WC_API_Exception 
  242. */ 
  243. public function set_product_custom_prices( $product, $request ) { 
  244.  
  245. $data = $request->get_params(); 
  246.  
  247. if( !empty( $this->woocommerce_wpml->multi_currency ) ) { 
  248.  
  249. if( !empty( $data['custom_prices'] ) ) { 
  250.  
  251. $original_post_id = $this->sitepress->get_original_element_id_filter('', $product->get_id(), 'post_product' ); 
  252.  
  253. update_post_meta( $original_post_id, '_wcml_custom_prices_status', 1); 
  254.  
  255. foreach( $data['custom_prices'] as $currency => $prices ) { 
  256.  
  257. $prices_uscore = array(); 
  258. foreach( $prices as $k => $p) { 
  259. $prices_uscore['_' . $k] = $p; 
  260. $this->woocommerce_wpml->multi_currency->custom_prices->update_custom_prices( $original_post_id, $prices_uscore, $currency ); 
  261.  
  262.  
  263.  
  264.  
  265. /** 
  266. * @param WP_REST_Response $response 
  267. * @param mixed $object 
  268. * @param WP_REST_Request $request 
  269. * Copy custom fields explicitly 
  270. * @return WP_REST_Response 
  271. */ 
  272. public function copy_product_custom_fields($response, $object, $request) { 
  273. global $wpdb; 
  274. global $wpml_post_translations; 
  275.  
  276. $data = $request->get_params(); 
  277.  
  278. if( isset( $data['id'] ) ) { 
  279. $translations = $wpml_post_translations->get_element_translations( $data['id'], false, true ); 
  280. foreach ( $translations as $translation_id ) { 
  281. $this->sitepress->copy_custom_fields( $data['id'], $translation_id ); 
  282.  
  283. return $response; 
  284.  
  285. public function filter_orders_by_language( $args, $request ) { 
  286.  
  287. $lang = $request->get_param( 'lang' ); 
  288.  
  289. if( !is_null( $lang ) && $lang !== 'all' ) { 
  290.  
  291. $args['meta_query'][] = array( 
  292. 'key' => 'wpml_language',  
  293. 'value' => strval( $lang ) 
  294. ); 
  295.  
  296.  
  297. return $args; 
  298.  
  299. /** 
  300. * Filters the items of an order according to a given languages 
  301. * @param WP_REST_Response $response 
  302. * @param WC_Order $order 
  303. * @param WP_REST_Request $request 
  304. * @return WP_REST_Response 
  305. */ 
  306.  
  307. public function filter_order_items_by_language( $response, $order, $request ) { 
  308.  
  309. $lang = get_query_var('lang'); 
  310. $order_lang = get_post_meta( $order->get_id(), 'wpml_language', true ); 
  311.  
  312. if( $order_lang != $lang || 1 ) { 
  313.  
  314. foreach( $response->data['line_items'] as $k => $item ) { 
  315.  
  316. global $wpdb; 
  317. $sql = "SELECT meta_value FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id=%d AND meta_key='_product_id'"; 
  318. $product_id = $wpdb->get_var( $wpdb->prepare( $sql, $item['id']) ); 
  319.  
  320. if( $product_id ) { 
  321.  
  322. $translated_product_id = apply_filters( 'translate_object_id', $product_id, 'product', true, $lang ); 
  323.  
  324. if( $translated_product_id ) { 
  325. $translated_product = get_post( $translated_product_id ); 
  326. $response->data['line_items'][$k]['product_id'] = $translated_product_id; 
  327. if( $translated_product->post_type == 'product_variation' ) { 
  328. $post_parent = get_post( $translated_product->post_parent ); 
  329. $post_name = $post_parent->post_title; 
  330. } else { 
  331. $post_name = $translated_product->post_title; 
  332. $response->data['line_items'][$k]['name'] = $post_name; 
  333.  
  334.  
  335.  
  336.  
  337. return $response; 
  338.  
  339. /** 
  340. * Sets the language for a new order 
  341. * @param WC_Order $order 
  342. * @param WP_REST_Request $request 
  343. * @throws WC_REST_Exception 
  344. */ 
  345. public function set_order_language( $order, $request ) { 
  346.  
  347. $data = $request->get_params(); 
  348. if( isset( $data['lang'] ) ) { 
  349. $order_id = $order->get_id(); 
  350. $active_languages = $this->sitepress->get_active_languages(); 
  351. if( !isset( $active_languages[$data['lang']] ) ) { 
  352. throw new WC_REST_Exception( '404', sprintf( __( 'Invalid language parameter: %s' ), $data['lang'] ), '404' ); 
  353.  
  354. update_post_meta( $order_id, 'wpml_language', $data['lang'] ); 
  355.  
  356.  
  357.  
  358.  
  359.