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