MyBookings.php
48 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\Compatibility\WcBookings\Templates; |
| 4 | |
| 5 | use WPML\Element\API\Languages; |
| 6 | use WPML\Element\API\Post; |
| 7 | use WPML\Element\API\PostTranslations; |
| 8 | use WPML\LIB\WP\Hooks; |
| 9 | |
| 10 | use function WPML\FP\spreadArgs; |
| 11 | |
| 12 | class MyBookings implements \IWPML_Action { |
| 13 | |
| 14 | public function add_hooks() { |
| 15 | Hooks::onFilter( 'woocommerce_bookings_account_tables' ) |
| 16 | ->then( spreadArgs( [ $this, 'filterByCurrentLanguage' ] ) ); |
| 17 | } |
| 18 | |
| 19 | public function filterByCurrentLanguage( $tables ) { |
| 20 | |
| 21 | $currentLanguage = Languages::getCurrentCode(); |
| 22 | |
| 23 | foreach ( $tables as $section => $table ) { |
| 24 | if ( isset( $table['bookings'] ) ) { |
| 25 | foreach ( $table['bookings'] as $key => $booking ) { |
| 26 | $languageCode = Post::getLang( $booking->get_id() ); |
| 27 | |
| 28 | if ( $languageCode !== $currentLanguage ) { |
| 29 | unset( $tables[ $section ]['bookings'][ $key ] ); |
| 30 | |
| 31 | } elseif ( ! $booking->get_product() ) { |
| 32 | $originalBookingId = PostTranslations::getOriginalId( $booking->get_id() ); |
| 33 | $originalBooking = get_wc_booking( $originalBookingId ); |
| 34 | if ( $originalBooking ) { |
| 35 | $booking->set_product_id( $originalBooking->get_product_id() ); |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | $tables[ $section ]['bookings'] = array_values( $tables[ $section ]['bookings'] ); |
| 42 | } |
| 43 | |
| 44 | return $tables; |
| 45 | } |
| 46 | |
| 47 | } |
| 48 |