WCML_Accommodation_Bookings

The WooCommerce Multilingual WCML Accommodation Bookings class.

Defined (1)

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

/compatibility/class-wcml-accommodation-bookings.php  
  1. class WCML_Accommodation_Bookings{ 
  2.  
  3. /** 
  4. * @var woocommerce_wpml 
  5. */ 
  6. private $woocommerce_wpml; 
  7.  
  8. function __construct( &$woocommerce_wpml ) { 
  9.  
  10. $this->woocommerce_wpml = $woocommerce_wpml; 
  11.  
  12. add_action( 'woocommerce_accommodation_bookings_after_booking_base_cost' , array( $this, 'wcml_price_field_after_booking_base_cost' ) ); 
  13. add_action( 'woocommerce_accommodation_bookings_after_booking_pricing_override_block_cost' , array( $this, 'wcml_price_field_after_booking_pricing_override_block_cost' ), 10, 2 ); 
  14. add_action( 'woocommerce_accommodation_bookings_after_bookings_pricing' , array( $this , 'after_bookings_pricing' ) ); 
  15.  
  16. add_action( 'save_post', array( $this, 'save_custom_costs' ), 110, 2 ); 
  17. add_filter( 'get_post_metadata', array( $this, 'product_price_filter'), 9, 4 ); 
  18.  
  19. add_action( 'init', array( $this, 'load_assets' ), 100 ); 
  20.  
  21. function wcml_price_field_after_booking_base_cost( $post_id ) { 
  22.  
  23. $this->echo_wcml_price_field( $post_id, 'wcml_wc_booking_base_cost' ); 
  24.  
  25.  
  26. function wcml_price_field_after_booking_pricing_override_block_cost( $pricing, $post_id ) { 
  27.  
  28. $this->echo_wcml_price_field( $post_id, 'wcml_wc_booking_pricing_override_block_cost', $pricing ); 
  29.  
  30.  
  31. function after_bookings_pricing( $post_id ) { 
  32.  
  33. $product_terms = wp_get_post_terms( $post_id, 'product_type', array( "fields" => "names" ) ); 
  34.  
  35. if( 
  36. in_array( 'accommodation-booking', $product_terms ) && 
  37. $this->woocommerce_wpml->products->is_original_product( $post_id ) && 
  38. $this->woocommerce_wpml->settings['enable_multi_currency'] == WCML_MULTI_CURRENCIES_INDEPENDENT 
  39. ) { 
  40.  
  41. $custom_costs_status = get_post_meta( $post_id, '_wcml_custom_costs_status', true ); 
  42.  
  43. $checked = !$custom_costs_status ? 'checked="checked"' : ' '; 
  44.  
  45. echo '<div class="wcml_custom_costs">'; 
  46.  
  47. echo '<input type="radio" name="_wcml_custom_costs" id="wcml_custom_costs_auto" value="0" class="wcml_custom_costs_input" '. $checked .' />'; 
  48. echo '<label for="wcml_custom_costs_auto">'. __('Calculate costs in other currencies automatically', 'woocommerce-multilingual') .'</label>'; 
  49.  
  50. $checked = $custom_costs_status == 1 ? 'checked="checked"' : ' '; 
  51.  
  52. echo '<input type="radio" name="_wcml_custom_costs" value="1" id="wcml_custom_costs_manually" class="wcml_custom_costs_input" '. $checked .' />'; 
  53. echo '<label for="wcml_custom_costs_manually">'. __('Set costs in other currencies manually', 'woocommerce-multilingual') .'</label>'; 
  54.  
  55. wp_nonce_field( 'wcml_save_accommodation_bookings_custom_costs', '_wcml_custom_costs_nonce' ); 
  56.  
  57. echo '</div>'; 
  58.  
  59.  
  60. function echo_wcml_price_field( $post_id, $field, $pricing = false, $check = true, $resource_id = false ) { 
  61.  
  62. if( ( !$check || $this->woocommerce_wpml->products->is_original_product( $post_id ) ) && $this->woocommerce_wpml->settings['enable_multi_currency'] == WCML_MULTI_CURRENCIES_INDEPENDENT ) { 
  63.  
  64. $currencies = $this->woocommerce_wpml->multi_currency->get_currencies(); 
  65.  
  66. $wc_currencies = get_woocommerce_currencies(); 
  67.  
  68. echo '<div class="wcml_custom_cost_field" >'; 
  69.  
  70. foreach($currencies as $currency_code => $currency) { 
  71.  
  72. switch( $field ) { 
  73. case 'wcml_wc_booking_base_cost': 
  74. woocommerce_wp_text_input( array( 'id' => 'wcml_wc_booking_base_cost', 'class'=>'wcml_bookings_custom_price', 'name' => 'wcml_wc_accommodation_booking_base_cost['.$currency_code.']', 'label' => get_woocommerce_currency_symbol($currency_code), 'description' => __( 'This is the cost per block booked. All other costs (for resources and persons) are added to this.', 'woocommerce-bookings' ), 'value' => get_post_meta( $post_id, '_wc_booking_base_cost_'.$currency_code, true ), 'type' => 'number', 'desc_tip' => true, 'custom_attributes' => array( 
  75. 'min' => '',  
  76. 'step' => '0.01' 
  77. ) ) ); 
  78. break; 
  79.  
  80.  
  81. case 'wcml_wc_booking_pricing_override_block_cost': 
  82.  
  83. if( isset( $pricing[ 'override_block_'.$currency_code ] ) ) { 
  84. $value = $pricing[ 'override_block_'.$currency_code ]; 
  85. }else{ 
  86. $value = ''; 
  87.  
  88. echo '<div class="wcml_bookings_range_block" >'; 
  89. echo '<label>'. get_woocommerce_currency_symbol($currency_code) .'</label>'; 
  90. echo '<input type="number" step="0.01" name="wcml_wc_accommodation_booking_pricing_override_block_cost['.$currency_code.']" class="wcml_bookings_custom_price" value="'. $value .'" placeholder="0" />'; 
  91. echo '</div>'; 
  92. break; 
  93.  
  94. default: 
  95. break; 
  96.  
  97.  
  98.  
  99. echo '</div>'; 
  100.  
  101.  
  102. function save_custom_costs( $post_id, $post ) { 
  103.  
  104. $nonce = filter_input( INPUT_POST, '_wcml_custom_costs_nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); 
  105.  
  106. if( isset( $_POST['_wcml_custom_costs'] ) && isset( $nonce ) && wp_verify_nonce( $nonce, 'wcml_save_accommodation_bookings_custom_costs' ) ) { 
  107.  
  108. update_post_meta( $post_id, '_wcml_custom_costs_status', $_POST['_wcml_custom_costs'] ); 
  109.  
  110. if( $_POST['_wcml_custom_costs'] == 1 ) { 
  111.  
  112. $currencies = $this->woocommerce_wpml->multi_currency->get_currencies(); 
  113.  
  114. foreach( $currencies as $code => $currency ) { 
  115.  
  116. $wc_booking_base_cost = $_POST[ 'wcml_wc_accommodation_booking_base_cost' ][ $code ]; 
  117. update_post_meta( $post_id, '_wc_booking_base_cost_'.$code, $wc_booking_base_cost ); 
  118. update_post_meta( $post_id, '_price_'.$code, $wc_booking_base_cost ); 
  119.  
  120.  
  121. $updated_meta = array(); 
  122. $booking_pricing = get_post_meta( $post_id, '_wc_booking_pricing', true ); 
  123.  
  124. foreach ( maybe_unserialize( $booking_pricing ) as $key => $prices ) { 
  125.  
  126. $updated_meta[ $key ] = $prices; 
  127.  
  128. foreach ( $currencies as $code => $currency ) { 
  129.  
  130. $updated_meta[ $key ][ 'override_block_'.$code ] = $_POST[ 'wcml_wc_accommodation_booking_pricing_override_block_cost' ][ $code ]; 
  131.  
  132.  
  133.  
  134. update_post_meta( $post_id, '_wc_booking_pricing', $updated_meta ); 
  135.  
  136.  
  137.  
  138. function product_price_filter( $value, $object_id, $meta_key, $single ) { 
  139.  
  140. if( 
  141. get_post_type( $object_id ) == 'product' && 
  142. $meta_key == '_price' && 
  143. $this->woocommerce_wpml->settings[ 'enable_multi_currency' ] == WCML_MULTI_CURRENCIES_INDEPENDENT && 
  144. !is_admin() && 
  145. ( $currency = $this->woocommerce_wpml->multi_currency->get_client_currency() ) != get_option( 'woocommerce_currency' ) 
  146. ) { 
  147.  
  148. remove_filter( 'get_post_metadata', array( $this, 'product_price_filter' ), 9, 4 ); 
  149.  
  150. $original_product = $this->woocommerce_wpml->products->get_original_product_id( $object_id ); 
  151.  
  152. if ( get_post_meta( $original_product, '_wcml_custom_costs_status' ) ) { 
  153.  
  154. $price = get_post_meta( $object_id, '_price_' . $currency , true ); 
  155.  
  156. add_filter( 'get_post_metadata', array( $this, 'product_price_filter' ), 9, 4 ); 
  157.  
  158. return isset( $price) ? $price : $value; 
  159.  
  160. public function load_assets() { 
  161.  
  162. $this->woocommerce_wpml->compatibility->bookings->load_assets( 'accommodation-booking' ); 
  163.