WCML_Cart_Switch_Lang_Functions

The WooCommerce Multilingual WCML Cart Switch Lang Functions class.

Defined (1)

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

/inc/wcml-cart-switch-lang-functions.php  
  1. class WCML_Cart_Switch_Lang_Functions{ 
  2.  
  3. private $lang_from; 
  4. private $lang_to; 
  5.  
  6.  
  7. function __construct() { 
  8.  
  9. add_action( 'wp_footer', array( $this, 'wcml_language_switch_dialog' ) ); 
  10. add_action( 'wp_loaded', array( $this, 'wcml_language_force_switch' ) ); 
  11. add_action( 'wcml_user_switch_language', array( $this, 'language_has_switched' ), 10 , 2 ); 
  12.  
  13.  
  14. public function language_has_switched( $lang_from, $lang_to ) { 
  15.  
  16. $settings = get_option( '_wcml_settings' ); 
  17.  
  18. if( 
  19. !isset( $_GET[ 'force_switch' ] ) && 
  20. $lang_from != $lang_to && 
  21. !empty( $settings ) && 
  22. $settings[ 'cart_sync' ][ 'lang_switch' ] == WCML_CART_CLEAR 
  23. ) { 
  24. $this->lang_from = $lang_from; 
  25. $this->lang_to = $lang_to; 
  26.  
  27. function wcml_language_force_switch() { 
  28. global $woocommerce_wpml, $woocommerce; 
  29.  
  30. if( isset( $_GET[ 'force_switch' ] ) && $_GET[ 'force_switch' ] == true ) { 
  31. $woocommerce_wpml->cart->empty_cart_if_needed( 'lang_switch' ); 
  32. $woocommerce->session->set( 'wcml_switched_type', 'lang_switch' ); 
  33.  
  34. function wcml_language_switch_dialog( ) { 
  35. global $woocommerce_wpml, $sitepress, $wp; 
  36.  
  37. $dependencies = new WCML_Dependencies; 
  38.  
  39. if( $dependencies->check() ) { 
  40.  
  41. $current_url = home_url( add_query_arg( array(), $wp->request ) ); 
  42. $request_url = add_query_arg( 'force_switch', 0, $sitepress->convert_url( $current_url, $this->lang_from ) ); 
  43.  
  44. $cart_for_session = false; 
  45. if( isset( WC()->cart ) ) { 
  46. $cart_for_session = WC()->cart->get_cart_for_session(); 
  47.  
  48. if( $this->lang_from && $this->lang_to && $request_url && !empty( $cart_for_session ) ) { 
  49.  
  50. $force_cart_url = add_query_arg( 'force_switch', 1, $current_url ); 
  51.  
  52. $new_language_details = $sitepress->get_language_details( $this->lang_to ); 
  53. $current_language_details = $sitepress->get_language_details( $this->lang_from ); 
  54. $dialog_title = __( 'Switching language?', 'woocommerce-multilingual' ); 
  55. $confirmation_message = esc_html ( sprintf( 
  56. __( "You've switched 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.", 'woocommerce-multilingual' ),  
  57. $new_language_details[ 'display_name' ] 
  58. ) ); 
  59. $stay_in = sprintf( __( 'Stay in %s', 'woocommerce-multilingual' ), $new_language_details[ 'display_name' ] ); 
  60. $switch_to = sprintf( __( 'Switch back to %s', 'woocommerce-multilingual' ), $current_language_details[ 'display_name' ] ); 
  61.  
  62. $woocommerce_wpml->cart->cart_alert( $dialog_title, $confirmation_message, $stay_in, $switch_to, $force_cart_url, $request_url, true ); 
  63.  
  64.  
  65.