WCML_WC_Subscriptions

The WooCommerce Multilingual WCML WC Subscriptions class.

Defined (1)

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

/compatibility/class-wcml-wc-subscriptions.php  
  1. class WCML_WC_Subscriptions{ 
  2.  
  3. private $new_subscription = false; 
  4.  
  5. function __construct() { 
  6.  
  7. add_action('init', array($this, 'init'), 9); 
  8. add_filter('wcml_variation_term_taxonomy_ids', array($this, 'wcml_variation_term_taxonomy_ids')); 
  9. add_filter('woocommerce_subscription_lengths', array($this, 'woocommerce_subscription_lengths'), 10, 2); 
  10.  
  11. add_filter('wcml_register_endpoints_query_vars', array($this, 'register_endpoint' ), 10, 3 ); 
  12. add_filter('wcml_endpoint_permalink_filter', array($this, 'endpoint_permalink_filter'), 10, 2); 
  13.  
  14. //custom prices 
  15. add_filter( 'wcml_custom_prices_fields', array( $this, 'set_prices_fields' ), 10, 2 ); 
  16. add_filter( 'wcml_custom_prices_strings', array( $this, 'set_labels_for_prices_fields' ), 10, 2 ); 
  17. add_filter( 'wcml_custom_prices_fields_labels', array( $this, 'set_labels_for_prices_fields' ), 10, 2 ); 
  18. add_filter( 'wcml_update_custom_prices_values', array( $this, 'update_custom_prices_values' ), 10 , 3 ); 
  19. add_action( 'wcml_after_custom_prices_block', array( $this, 'new_subscription_prices_block') ); 
  20.  
  21. // reenable coupons for subscriptions when multicurrency is on 
  22. add_action('woocommerce_subscription_cart_after_grouping', array($this, 'woocommerce_subscription_cart_after_grouping')); 
  23.  
  24. add_action( 'woocommerce_subscriptions_product_options_pricing', array( $this, 'show_pointer_info' ) ); 
  25. add_action( 'woocommerce_variable_subscription_pricing', array( $this, 'show_pointer_info' ) ); 
  26.  
  27. function init() { 
  28. if( !is_admin() ) { 
  29. add_filter('woocommerce_subscriptions_product_sign_up_fee', array($this, 'product_price_filter'), 10, 2); 
  30.  
  31. add_action( 'woocommerce_before_calculate_totals', array( $this, 'maybe_backup_recurring_carts'), 1 ); 
  32. add_action( 'woocommerce_after_calculate_totals', array( $this, 'maybe_restore_recurring_carts'), 200 ); 
  33.  
  34. function product_price_filter($subscription_sign_up_fee, $product) { 
  35.  
  36. $subscription_sign_up_fee = apply_filters('wcml_raw_price_amount', $subscription_sign_up_fee ); 
  37.  
  38. return $subscription_sign_up_fee; 
  39.  
  40. function wcml_variation_term_taxonomy_ids($get_variation_term_taxonomy_ids) { 
  41. global $wpdb; 
  42. $get_variation_term_taxonomy_id = $wpdb->get_var("SELECT tt.term_taxonomy_id FROM $wpdb->terms AS t LEFT JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE t.slug = 'variable-subscription'"); 
  43.  
  44. if(!empty($get_variation_term_taxonomy_id)) { 
  45. $get_variation_term_taxonomy_ids[] = $get_variation_term_taxonomy_id; 
  46.  
  47. return $get_variation_term_taxonomy_ids; 
  48.  
  49. public function woocommerce_subscription_lengths($subscription_ranges, $subscription_period) { 
  50.  
  51. if (is_array($subscription_ranges)) { 
  52. foreach ($subscription_ranges as $period => $ranges) { 
  53. if (is_array($ranges)) { 
  54. foreach ($ranges as $range) { 
  55. if ($range == "9 months") { 
  56. $breakpoint = true; 
  57. $new_subscription_ranges[$period][] = apply_filters( 'wpml_translate_single_string', $range, 'wc_subscription_ranges', $range); 
  58.  
  59. return isset($new_subscription_ranges) ? $new_subscription_ranges : $subscription_ranges; 
  60.  
  61. public function woocommerce_subscription_cart_after_grouping() { 
  62. global $woocommerce_wpml; 
  63.  
  64. if( $woocommerce_wpml->settings['enable_multi_currency'] == WCML_MULTI_CURRENCIES_INDEPENDENT ) { 
  65. remove_action('woocommerce_before_calculate_totals', 'WC_Subscriptions_Coupon::remove_coupons', 10); 
  66.  
  67.  
  68. function set_prices_fields( $fields, $product_id ) { 
  69. if( $this->is_subscriptions_product( $product_id ) || $this->new_subscription ) { 
  70. $fields[] = '_subscription_sign_up_fee'; 
  71.  
  72. return $fields; 
  73.  
  74.  
  75. function set_labels_for_prices_fields( $labels, $product_id ) { 
  76.  
  77. if( $this->is_subscriptions_product( $product_id ) || $this->new_subscription ) { 
  78. $labels[ '_regular_price' ] = __( 'Subscription Price', 'woocommerce-multilingual' ); 
  79. $labels[ '_subscription_sign_up_fee' ] = __( 'Sign-up Fee', 'woocommerce-multilingual' ); 
  80.  
  81. return $labels; 
  82.  
  83.  
  84. function update_custom_prices_values( $prices, $code, $variation_id = false ) { 
  85.  
  86. if( isset( $_POST[ '_custom_subscription_sign_up_fee' ][ $code ] ) ) { 
  87. $prices[ '_subscription_sign_up_fee' ] = wc_format_decimal( $_POST[ '_custom_subscription_sign_up_fee' ][ $code ] ); 
  88.  
  89. if( $variation_id && isset( $_POST[ '_custom_variation_subscription_sign_up_fee' ][ $code ][ $variation_id ] ) ) { 
  90. $prices[ '_subscription_sign_up_fee' ] = wc_format_decimal( $_POST[ '_custom__custom_variation_subscription_sign_up_fee' ][ $code ][ $variation_id ] ); 
  91.  
  92. return $prices; 
  93.  
  94.  
  95. function is_subscriptions_product( $product_id ) { 
  96. global $wpdb; 
  97. $get_variation_term_taxonomy_ids = $wpdb->get_col("SELECT tt.term_taxonomy_id FROM $wpdb->terms AS t LEFT JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE t.slug IN ( 'subscription', 'variable-subscription' ) AND tt.taxonomy = 'product_type'"); 
  98.  
  99. if( get_post_type( $product_id ) == 'product_variation' ) { 
  100. $product_id = wp_get_post_parent_id( $product_id ); 
  101.  
  102. $is_subscriptions_product = $wpdb->get_var($wpdb->prepare("SELECT count(object_id) FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN (".join(', ', $get_variation_term_taxonomy_ids).")", $product_id)); 
  103. return $is_subscriptions_product; 
  104.  
  105. function new_subscription_prices_block( $product_id ) { 
  106. global $woocommerce_wpml; 
  107. if( $product_id == 'new' ) { 
  108. $this->new_subscription = true; 
  109. echo '<div class="wcml_prices_if_subscription" style="display: none">'; 
  110. $custom_prices_ui = new WCML_Custom_Prices_UI( $woocommerce_wpml, 'new' ); 
  111. $custom_prices_ui->show(); 
  112. echo '</div>'; 
  113. ?> 
  114. <script> 
  115. jQuery(document).ready(function($) { 
  116. jQuery('.wcml_prices_if_subscription .wcml_custom_prices_input').attr('name', '_wcml_custom_prices[new_subscription]').attr( 'id', '_wcml_custom_prices[new_subscription]'); 
  117. jQuery('.wcml_prices_if_subscription .wcml_custom_prices_options_block>label').attr('for', '_wcml_custom_prices[new_subscription]'); 
  118. jQuery('.wcml_prices_if_subscription .wcml_schedule_input').each( function() { 
  119. jQuery(this).attr('name', jQuery(this).attr('name')+'_subscription'); 
  120. }); 
  121.  
  122. jQuery('.options_group>.wcml_custom_prices_block .wcml_custom_prices_input:first-child').click(); 
  123. jQuery('.options_group>.wcml_custom_prices_block .wcml_schedule_options .wcml_schedule_input:first-child').click(); 
  124.  
  125. jQuery(document).on('change', 'select#product-type', function () { 
  126. if (jQuery(this).val() == 'subscription') { 
  127. jQuery('.wcml_prices_if_subscription').show(); 
  128. jQuery('.options_group>.wcml_custom_prices_block').hide(); 
  129. } else if (jQuery(this).val() != 'variable-subscription') { 
  130. jQuery('.wcml_prices_if_subscription').hide(); 
  131. jQuery('.options_group>.wcml_custom_prices_block').show(); 
  132. }); 
  133.  
  134. jQuery(document).on('click', '#publish', function () { 
  135. if ( jQuery('.wcml_prices_if_subscription').is( ':visible' ) ) { 
  136. jQuery('.options_group>.wcml_custom_prices_block').remove(); 
  137. jQuery('.wcml_prices_if_subscription .wcml_custom_prices_input').attr('name', '_wcml_custom_prices[new]'); 
  138. jQuery('.wcml_prices_if_subscription .wcml_schedule_input').each( function() { 
  139. jQuery(this).attr('name', jQuery(this).attr('name').replace('_subscription', '') ); 
  140. }); 
  141. }else{ 
  142. jQuery('.wcml_prices_if_subscription').remove(); 
  143. }); 
  144. }); 
  145. </script> 
  146. <?php 
  147.  
  148. function register_endpoint( $query_vars, $wc_vars, $obj ) { 
  149.  
  150. $query_vars[ 'view-subscription' ] = $obj->get_endpoint_translation( 'view-subscription', isset( $wc_vars['view-subscription'] ) ? $wc_vars['view-subscription'] : 'view-subscription' ); 
  151. return $query_vars; 
  152.  
  153. function endpoint_permalink_filter( $endpoint, $key ) { 
  154.  
  155. if( $key == 'view-subscription' ) { 
  156. return 'view-subscription'; 
  157.  
  158. return $endpoint; 
  159.  
  160. public function show_pointer_info() { 
  161.  
  162. $pointer_ui = new WCML_Pointer_UI( 
  163. sprintf( __( 'You can translate strings related to subscription products on the %sWPML String Translation page%s. Use the search on the top of that page to find the strings.', 'woocommerce-multilingual' ), '<a href="'.admin_url('admin.php?page='.WPML_ST_FOLDER.'/menu/string-translation.php&context=woocommerce_subscriptions').'">', '</a>' ),  
  164. 'https://wpml.org/documentation/woocommerce-extensions-compatibility/translating-woocommerce-subscriptions-woocommerce-multilingual/',  
  165. 'general_product_data .subscription_pricing',  
  166. 'prepend' 
  167. ); 
  168.  
  169. $pointer_ui->show(); 
  170.  
  171. /** 
  172. * @param WC_Cart $cart 
  173. */ 
  174. public function maybe_backup_recurring_carts( $cart ) { 
  175. if( ! empty( $cart->recurring_carts ) ) { 
  176. $this->recurring_carts = $cart->recurring_carts; 
  177.  
  178. /** 
  179. * @param WC_Cart $cart 
  180. */ 
  181. public function maybe_restore_recurring_carts( $cart ) { 
  182. if( ! empty( $this->recurring_carts ) ) { 
  183. $cart->recurring_carts = $this->recurring_carts; 
  184. $this->recurring_carts = null; 
  185.