admin-menus
4 weeks ago
currencies
4 weeks ago
template-classes
4 weeks ago
translation-editor
4 weeks ago
class-wcml-ajax-setup.php
4 weeks ago
class-wcml-attributes.php
4 weeks ago
class-wcml-capabilities.php
4 weeks ago
class-wcml-cart-removed-items-widget.php
1 year ago
class-wcml-cart-switch-lang-functions.php
4 weeks ago
class-wcml-cart-sync-warnings.php
4 weeks ago
class-wcml-cart.php
4 weeks ago
class-wcml-comments.php
4 weeks ago
class-wcml-compatibility.php
4 weeks ago
class-wcml-coupons.php
4 weeks ago
class-wcml-dependencies.php
4 weeks ago
class-wcml-emails.php
4 weeks ago
class-wcml-endpoints.php
4 weeks ago
class-wcml-install.php
4 weeks ago
class-wcml-languages-upgrader.php
4 weeks ago
class-wcml-locale.php
4 weeks ago
class-wcml-orders.php
4 weeks ago
class-wcml-products-screen-options.php
4 weeks ago
class-wcml-products.php
4 weeks ago
class-wcml-reports.php
4 weeks ago
class-wcml-requests.php
4 weeks ago
class-wcml-resources.php
4 weeks ago
class-wcml-store-pages.php
4 weeks ago
class-wcml-terms.php
4 weeks ago
class-wcml-tp-support.php
4 weeks ago
class-wcml-troubleshooting.php
4 weeks ago
class-wcml-upgrade.php
4 weeks ago
class-wcml-url-translation.php
4 weeks ago
class-wcml-wc-gateways.php
4 weeks ago
class-wcml-wc-shipping.php
4 weeks ago
class-wcml-wc-strings.php
4 weeks ago
class-wcml-widgets.php
4 weeks ago
constants.php
4 weeks ago
functions-pure.php
4 weeks ago
functions.php
4 weeks ago
installer-loader.php
4 weeks ago
missing-php-functions.php
4 weeks ago
wcml-core-functions.php
4 weeks ago
wcml-switch-lang-request.php
4 weeks ago
class-wcml-cart-switch-lang-functions.php
107 lines
| 1 | <?php |
| 2 | |
| 3 | use function WPML\Container\make; |
| 4 | use WPML\API\Sanitize; |
| 5 | |
| 6 | class WCML_Cart_Switch_Lang_Functions implements \IWPML_Frontend_Action, \IWPML_Backend_Action { |
| 7 | |
| 8 | private $lang_from; |
| 9 | |
| 10 | private $lang_to; |
| 11 | |
| 12 | public function add_hooks() { |
| 13 | add_action( 'wp_footer', [ $this, 'wcml_language_switch_dialog' ] ); |
| 14 | add_action( 'wp_loaded', [ $this, 'wcml_language_force_switch' ] ); |
| 15 | add_action( 'wcml_user_switch_language', [ $this, 'language_has_switched' ], 10, 2 ); |
| 16 | |
| 17 | add_filter( 'woocommerce_product_add_to_cart_url', [ $this, 'remove_force_switch_from_add_to_cart_url' ] ); |
| 18 | } |
| 19 | |
| 20 | public function remove_force_switch_from_add_to_cart_url( $url ) { |
| 21 | $url = str_replace( 'force_switch=1&', '', $url ); |
| 22 | return $url; |
| 23 | } |
| 24 | |
| 25 | public function language_has_switched( $lang_from, $lang_to ) { |
| 26 | |
| 27 | $settings = get_option( '_wcml_settings' ); |
| 28 | |
| 29 | if ( |
| 30 | ! isset( $_GET['force_switch'] ) && |
| 31 | $lang_from !== $lang_to && |
| 32 | ! empty( $settings ) && |
| 33 | WCML_CART_CLEAR === $settings['cart_sync']['lang_switch'] |
| 34 | ) { |
| 35 | $this->lang_from = $lang_from; |
| 36 | $this->lang_to = $lang_to; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | public function wcml_language_force_switch() { |
| 41 | global $woocommerce_wpml, $woocommerce; |
| 42 | |
| 43 | if ( ! wpml_is_ajax() && isset( $_GET['force_switch'] ) && '1' === $_GET['force_switch'] ) { |
| 44 | $woocommerce_wpml->cart->empty_cart_if_needed( 'lang_switch' ); |
| 45 | $woocommerce->session->set( 'wcml_switched_type', 'lang_switch' ); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | public function wcml_language_switch_dialog() { |
| 50 | global $woocommerce_wpml, $sitepress, $post; |
| 51 | |
| 52 | if ( make( WCML_Dependencies::class )->check() ) { |
| 53 | $current_url = $this->get_current_url(); |
| 54 | |
| 55 | if ( is_shop() ) { |
| 56 | $requested_page_id = apply_filters( 'wpml_object_id', wc_get_page_id( 'shop' ), 'post', true, $this->lang_from ); |
| 57 | } elseif ( isset( $post->ID ) ) { |
| 58 | $requested_page_id = apply_filters( 'wpml_object_id', $post->ID, get_post_type( $post->ID ), true, $this->lang_from ); |
| 59 | } |
| 60 | |
| 61 | if ( isset( $requested_page_id ) ) { |
| 62 | $request_url = add_query_arg( 'force_switch', '0', $sitepress->convert_url( get_permalink( $requested_page_id ), $this->lang_from ) ); |
| 63 | } else { |
| 64 | $request_url = $current_url; |
| 65 | } |
| 66 | |
| 67 | $cart_for_session = false; |
| 68 | if ( WC()->cart instanceof WC_Cart ) { |
| 69 | $cart_for_session = WC()->cart->get_cart_for_session(); |
| 70 | } |
| 71 | |
| 72 | if ( $this->lang_from && $this->lang_to && $request_url && ! empty( $cart_for_session ) ) { |
| 73 | |
| 74 | $force_cart_url = add_query_arg( 'force_switch', '1', $current_url ); |
| 75 | $active_languages = apply_filters( 'wpml_active_languages', null, null ); |
| 76 | $dialog_title = __( 'Switching language?', 'woocommerce-multilingual' ); |
| 77 | |
| 78 | $confirmation_message = sprintf( |
| 79 | /* translators: %s is a language name */ |
| 80 | __( |
| 81 | "You've switched the language and there are items in the cart. If you keep the %s language, the cart will be emptied and you will have to add the items again to the cart.", |
| 82 | 'woocommerce-multilingual' |
| 83 | ), |
| 84 | $active_languages[ $this->lang_to ]['translated_name'] |
| 85 | ); |
| 86 | /* translators: %s is a language name */ |
| 87 | $stay_in = sprintf( __( 'Keep %s', 'woocommerce-multilingual' ), $active_languages[ $this->lang_to ]['translated_name'] ); |
| 88 | /* translators: %s is a language name */ |
| 89 | $switch_to = sprintf( __( 'Switch back to %s', 'woocommerce-multilingual' ), $active_languages[ $this->lang_from ]['translated_name'] ); |
| 90 | |
| 91 | $woocommerce_wpml->cart->cart_alert( $dialog_title, $confirmation_message, $stay_in, $switch_to, $force_cart_url, $request_url, true ); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | } |
| 96 | |
| 97 | public function get_current_url() { |
| 98 | $get_query = $_GET; |
| 99 | |
| 100 | foreach ( $get_query as $index => $item ) { |
| 101 | $get_query[ $index ] = Sanitize::string( $item ); |
| 102 | } |
| 103 | |
| 104 | return home_url( add_query_arg( $get_query ) ); |
| 105 | } |
| 106 | } |
| 107 |