update_meta_data( '_user_language', qtrad_current_language() ); } } /** The block checkout builds the thank-you address in a request without a language; it opens in the order's language. */ function qtrad_wc_order_url( $url, $order = null ) { $lang = qtrad_wc_order_language( $order ); return is_string( $url ) && '' !== $lang ? qtrad_convert_url( $url, $lang, true, null ) : $url; } /** Order items keep the customer's language, also for custom attribute values. */ function qtrad_wc_order_line_item( $item, $cart_item_key, $values, $order ) { if ( ! is_object( $item ) || ! method_exists( $item, 'get_meta_data' ) ) { return; } foreach ( $item->get_meta_data() as $meta ) { $data = $meta->get_data(); if ( isset( $data['value'] ) && is_string( $data['value'] ) && qtrad_has_lang_tags( $data['value'] ) ) { $item->update_meta_data( $data['key'], qtrad_use_current( $data['value'] ), isset( $data['id'] ) ? $data['id'] : 0 ); } } } /** Language a customer email for this order goes out in. */ function qtrad_wc_order_language( $order ) { if ( ! is_object( $order ) || ! method_exists( $order, 'get_meta' ) ) { return ''; } $lang = (string) $order->get_meta( '_user_language', true ); return qtrad_is_enabled( $lang ) ? $lang : ''; } function qtrad_wc_email_hooks( $emails ) { foreach ( (array) $emails as $email ) { if ( is_object( $email ) && ! empty( $email->id ) ) { add_filter( 'woocommerce_email_recipient_' . $email->id, 'qtrad_wc_email_start', 1, 3 ); } } return $emails; } /** * WooCommerce asks for the recipient right before it builds the subject and * content, which is when a customer email switches to the order's language. */ function qtrad_wc_email_start( $recipient, $object = null, $email = null ) { if ( ! empty( $GLOBALS['qtrad_wc_email'] ) || ! qtrad_wc_sending( $email ) ) { return $recipient; } // Customer emails go out in the order's language, emails to the shop in the site's default language. $customer = is_object( $email ) && method_exists( $email, 'is_customer_email' ) && $email->is_customer_email(); $lang = $customer ? qtrad_wc_order_language( $object ) : qtrad_default_language(); if ( $lang === qtrad_current_language() ) { $lang = ''; } $GLOBALS['qtrad_wc_email'] = array( 'language' => qtrad_current_language(), 'locale' => false ); if ( '' !== $lang ) { // Switch the locale first: WordPress compares with determine_locale(), which follows the qTrad language. $locales = qtrad_config( 'locale' ); if ( ! empty( $locales[ $lang ] ) && function_exists( 'switch_to_locale' ) ) { $GLOBALS['qtrad_wc_email']['locale'] = switch_to_locale( $locales[ $lang ] ); } qtrad_set_language( $lang ); // The site title placeholder was filled when the email object was built, in the request's language. if ( isset( $email->placeholders ) && is_array( $email->placeholders ) && method_exists( $email, 'get_blogname' ) ) { $email->placeholders['{site_title}'] = $email->get_blogname(); } } return $recipient; } /** * The recipient is also read to display settings; only a send switches the * language. WooCommerce emails call setup_locale() when a send starts and * restore_locale() when it ends, and each applies a filter with the email. */ function qtrad_wc_sending( $email ) { return is_object( $email ) && ! empty( $GLOBALS['qtrad_wc_sending'][ spl_object_hash( $email ) ] ); } function qtrad_wc_email_sending_start( $allow, $email = null ) { if ( is_object( $email ) ) { $GLOBALS['qtrad_wc_sending'][ spl_object_hash( $email ) ] = true; } return $allow; } function qtrad_wc_email_sending_end( $allow, $email = null ) { if ( is_object( $email ) ) { unset( $GLOBALS['qtrad_wc_sending'][ spl_object_hash( $email ) ] ); } return $allow; } function qtrad_wc_email_done() { if ( empty( $GLOBALS['qtrad_wc_email'] ) || ! is_array( $GLOBALS['qtrad_wc_email'] ) ) { return; } $state = $GLOBALS['qtrad_wc_email']; unset( $GLOBALS['qtrad_wc_email'] ); if ( $state['locale'] ) { restore_previous_locale(); } qtrad_set_language( $state['language'] ); } /** Runs before WooCommerce restores its own locale, so the switches unwind in order. */ function qtrad_wc_email_restore( $restore ) { qtrad_wc_email_done(); return $restore; }