WCML_WC_Shipping

The WooCommerce Multilingual WCML WC Shipping class.

Defined (1)

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

/inc/class-wcml-wc-shipping.php  
  1. class WCML_WC_Shipping{ 
  2.  
  3. private $current_language; 
  4. private $sitepress; 
  5.  
  6. function __construct( &$sitepress ) { 
  7.  
  8. $this->sitepress = $sitepress; 
  9.  
  10. $this->current_language = $this->sitepress->get_current_language(); 
  11. if( $this->current_language == 'all' ) { 
  12. $this->current_language = $this->sitepress->get_default_language(); 
  13.  
  14.  
  15. function add_hooks() { 
  16.  
  17. add_action('woocommerce_tax_rate_added', array($this, 'register_tax_rate_label_string'), 10, 2 ); 
  18. add_action('wp_ajax_woocommerce_shipping_zone_methods_save_settings', array( $this, 'save_shipping_zone_method_from_ajax'), 9 ); 
  19. add_action( 'icl_save_term_translation', array( $this, 'sync_class_costs_for_new_shipping_classes' ), 100, 2 ); 
  20. add_action( 'wp_ajax_woocommerce_shipping_zone_methods_save_settings', array( $this, 'update_woocommerce_shipping_settings_for_class_costs_from_ajax'), 9); 
  21.  
  22. add_filter('woocommerce_package_rates', array($this, 'translate_shipping_methods_in_package')); 
  23. add_filter('woocommerce_rate_label', array($this, 'translate_woocommerce_rate_label')); 
  24. add_filter( 'pre_update_option_woocommerce_flat_rate_settings', array( $this, 'update_woocommerce_shipping_settings_for_class_costs' ) ); 
  25. add_filter( 'pre_update_option_woocommerce_international_delivery_settings', array( $this, 'update_woocommerce_shipping_settings_for_class_costs' ) ); 
  26.  
  27. $this->shipping_methods_filters(); 
  28.  
  29. function shipping_methods_filters() { 
  30.  
  31. $shipping_methods = WC()->shipping->get_shipping_methods(); 
  32.  
  33. foreach ( $shipping_methods as $shipping_method ) { 
  34.  
  35. if( isset( $shipping_method->id ) ) { 
  36. $shipping_method_id = $shipping_method->id; 
  37. }else{ 
  38. continue; 
  39.  
  40. if( ( defined('WC_VERSION') && version_compare( WC_VERSION , '2.6', '<' ) ) ) { 
  41. add_filter( 'woocommerce_settings_api_sanitized_fields_'.$shipping_method_id, array( $this, 'register_shipping_strings' ) ); 
  42. }else{ 
  43. add_filter( 'woocommerce_shipping_' . $shipping_method_id . '_instance_settings_values', array( $this, 'register_zone_shipping_strings' ), 9, 2 ); 
  44.  
  45. add_filter( 'option_woocommerce_'.$shipping_method_id.'_settings', array( $this, 'translate_shipping_strings' ), 9, 2 ); 
  46.  
  47. function save_shipping_zone_method_from_ajax() { 
  48. foreach( $_POST['data'] as $key => $value ) { 
  49. if( strstr( $key, '_title' ) ) { 
  50. $shipping_id = str_replace( 'woocommerce_', '', $key ); 
  51. $shipping_id = str_replace( '_title', '', $shipping_id ); 
  52. $this->register_shipping_title( $shipping_id.$_POST['instance_id'], $value ); 
  53. break; 
  54.  
  55. function register_zone_shipping_strings( $instance_settings, $object ) { 
  56. if( !empty( $instance_settings['title'] ) ) { 
  57. $this->register_shipping_title( $object->id.$object->instance_id, $instance_settings['title'] ); 
  58.  
  59. $instance_settings = $this->sync_flat_rate_class_cost( $object->get_post_data(), $instance_settings ); 
  60.  
  61. return $instance_settings; 
  62.  
  63. function register_shipping_strings( $fields ) { 
  64. $shipping = WC_Shipping::instance(); 
  65.  
  66. foreach( $shipping->get_shipping_methods() as $shipping_method ) { 
  67. if( isset( $_POST['woocommerce_'.$shipping_method->id.'_enabled'] ) ) { 
  68. $shipping_method_id = $shipping_method->id; 
  69. break; 
  70.  
  71. if( isset( $shipping_method_id ) ) { 
  72. $this->register_shipping_title( $shipping_method_id, $fields['title'] ); 
  73.  
  74. return $fields; 
  75.  
  76. function register_shipping_title( $shipping_method_id, $title ) { 
  77. do_action( 'wpml_register_single_string', 'woocommerce', $shipping_method_id .'_shipping_method_title', $title ); 
  78.  
  79. function translate_shipping_strings( $value, $option = false ) { 
  80.  
  81. if( $option && isset( $value['enabled']) && $value['enabled'] == 'no' ) { 
  82. return $value; 
  83.  
  84. $shipping_id = str_replace( 'woocommerce_', '', $option ); 
  85. $shipping_id = str_replace( '_settings', '', $shipping_id ); 
  86.  
  87. if( isset( $value['title'] ) ) { 
  88. $value['title'] = $this->translate_shipping_method_title( $value['title'], $shipping_id ); 
  89.  
  90. return $value; 
  91.  
  92. function translate_shipping_methods_in_package( $available_methods ) { 
  93.  
  94. foreach($available_methods as $key => $method) { 
  95. $available_methods[$key]->label = $this->translate_shipping_method_title( $method->label, $key ); 
  96.  
  97. return $available_methods; 
  98.  
  99. function translate_shipping_method_title( $title, $shipping_id, $language = false ) { 
  100.  
  101. if( !is_admin() ) { 
  102.  
  103. $shipping_id = str_replace( ':', '', $shipping_id );  
  104. $translated_title = apply_filters( 'wpml_translate_single_string', $title, 'woocommerce', $shipping_id .'_shipping_method_title', $language ? $language : $this->current_language ); 
  105.  
  106. if( $translated_title ) { 
  107. $title = $translated_title; 
  108.  
  109.  
  110. return $title; 
  111.  
  112. function translate_woocommerce_rate_label( $label ) { 
  113.  
  114. $label = apply_filters( 'wpml_translate_single_string', $label, 'woocommerce taxes', $label ); 
  115.  
  116. return $label; 
  117.  
  118. function register_tax_rate_label_string( $id, $tax_rate ) { 
  119.  
  120. if( !empty( $tax_rate['tax_rate_name'] ) ) { 
  121. do_action('wpml_register_single_string', 'woocommerce taxes', $tax_rate['tax_rate_name'] , $tax_rate['tax_rate_name'] ); 
  122.  
  123.  
  124. function sync_class_costs_for_new_shipping_classes( $original_tax, $result ) { 
  125. //update flat rate options for shipping classes 
  126. if( $original_tax->taxonomy == 'product_shipping_class' ) { 
  127.  
  128. $settings = get_option( 'woocommerce_flat_rate_settings' ); 
  129. if( is_array( $settings ) ) { 
  130. update_option( 'woocommerce_flat_rate_settings', $this->update_woocommerce_shipping_settings_for_class_costs( $settings ) ); 
  131.  
  132. $settings = get_option( 'woocommerce_international_delivery_settings' ); 
  133. if( is_array( $settings ) ) { 
  134. update_option( 'woocommerce_international_delivery_settings', $this->update_woocommerce_shipping_settings_for_class_costs( $settings ) ); 
  135.  
  136.  
  137. function update_woocommerce_shipping_settings_for_class_costs( $settings ) { 
  138.  
  139. remove_filter( 'get_term', array( $this->sitepress, 'get_term_adjust_id' ), 1 ); 
  140. foreach( $settings as $setting_key => $value ) { 
  141.  
  142. if( substr($setting_key, 0, 11) == 'class_cost_' ) { 
  143.  
  144. $shipp_class_key = substr($setting_key, 11 ); 
  145.  
  146. if( is_numeric( $shipp_class_key ) ) { 
  147. $shipp_class = get_term( $shipp_class_key, 'product_shipping_class' ); 
  148. }else{ 
  149. $shipp_class = get_term_by( 'slug', $shipp_class_key, 'product_shipping_class' ); 
  150.  
  151. $trid = $this->sitepress->get_element_trid( $shipp_class->term_taxonomy_id, 'tax_product_shipping_class' ); 
  152.  
  153. $translations = $this->sitepress->get_element_translations( $trid, 'tax_product_shipping_class' ); 
  154.  
  155. foreach( $translations as $translation ) { 
  156.  
  157. $tr_shipp_class = get_term_by( 'term_taxonomy_id', $translation->element_id, 'product_shipping_class' ); 
  158.  
  159. if( is_numeric( $shipp_class_key ) ) { 
  160. $settings[ 'class_cost_'.$tr_shipp_class->term_id ] = $value; 
  161. }else{ 
  162. $settings[ 'class_cost_'.$tr_shipp_class->slug ] = $value; 
  163.  
  164.  
  165.  
  166. add_filter( 'get_term', array( $this->sitepress, 'get_term_adjust_id' ), 1 ); 
  167.  
  168. return $settings; 
  169.  
  170. function update_woocommerce_shipping_settings_for_class_costs_from_ajax() { 
  171.  
  172. if (isset($_POST['data']['woocommerce_flat_rate_type']) && $_POST['data']['woocommerce_flat_rate_type'] == 'class') { 
  173.  
  174. $flat_rate_setting_id = 'woocommerce_flat_rate_' . $_POST['data']['instance_id'] . '_settings'; 
  175. $settings = get_option( $flat_rate_setting_id, true ); 
  176.  
  177. $settings = $this->sync_flat_rate_class_cost( $_POST['data'], $settings ); 
  178.  
  179. update_option($flat_rate_setting_id, $settings); 
  180.  
  181. function sync_flat_rate_class_cost( $data, $inst_settings ) { 
  182.  
  183. $settings = array(); 
  184. foreach ( $data as $key => $value ) { 
  185. if ( substr( $key, 0, 33) == 'woocommerce_flat_rate_class_cost_' ) { 
  186. $settings[ substr( $key, 22 ) ] = $value; 
  187.  
  188. $updated_costs_settings = $this->update_woocommerce_shipping_settings_for_class_costs( $settings ); 
  189.  
  190. $inst_settings = is_array( $inst_settings ) ? array_merge( $inst_settings, $updated_costs_settings ) : $updated_costs_settings; 
  191.  
  192. return $inst_settings; 
  193.