'woocommerce_cart', 'checkout' => 'woocommerce_checkout', 'myaccount' => 'woocommerce_my_account', ); function store_language_conflict(): bool { return defined( 'PLLWC_VERSION' ) || defined( 'WCML_VERSION' ) || class_exists( '\PLLWC' ) || class_exists( '\woocommerce_wpml' ); } function enable_store_translation(): void { if ( ! class_exists( '\WooCommerce' ) || ! get_option( 'uruvo_store_translation' ) || store_language_conflict() ) { return; } foreach ( array_keys( STORE_PAGES ) as $page ) { add_filter( "woocommerce_get_{$page}_page_id", __NAMESPACE__ . '\\localize_store_page_id' ); } add_filter( 'woocommerce_ajax_get_endpoint', __NAMESPACE__ . '\\localize_store_ajax' ); add_action( 'woocommerce_checkout_create_order', __NAMESPACE__ . '\\remember_order_language' ); add_filter( 'woocommerce_email_actions', array( Store_Order_Language::class, 'hook_notifications' ) ); add_filter( 'woocommerce_allow_switching_email_locale', array( Store_Order_Language::class, 'switch_email' ), 20, 2 ); add_filter( 'woocommerce_allow_restoring_email_locale', array( Store_Order_Language::class, 'restore_email' ), 20, 2 ); } function store_request_language(): string { $email_language = Store_Order_Language::active_language(); if ( $email_language ) { return $email_language; } if ( ! function_exists( 'pll_current_language' ) ) { return ''; } if ( defined( 'WC_DOING_AJAX' ) && WC_DOING_AJAX ) { $requested = sanitize_key( wp_unslash( $_GET['lang'] ?? '' ) ); if ( $requested && locale_for( $requested ) ) { return $requested; } } return (string) pll_current_language( 'slug' ); } function store_page_in_language( int $page_id, string $language ): int { $translated = $page_id && $language && function_exists( 'pll_get_post' ) ? (int) pll_get_post( $page_id, $language ) : 0; $page = $translated ? get_post( $translated ) : null; return $page && 'page' === $page->post_type && 'publish' === $page->post_status && '' === $page->post_password ? $translated : 0; } function localize_store_page_id( $page_id ) { if ( is_admin() && ! wp_doing_ajax() && ! Store_Order_Language::active_language() ) { return $page_id; } $translated = store_page_in_language( (int) $page_id, store_request_language() ); if ( ! $translated || $translated === (int) $page_id ) { return $page_id; } // WooCommerce uses a different transport for block checkout. Keep the // store's assigned page until the classic flow has a matching translation. $page = str_replace( array( 'woocommerce_get_', '_page_id' ), '', current_filter() ); $shortcode = STORE_PAGES[ $page ] ?? ''; if ( ! $shortcode || ! has_shortcode( (string) get_post_field( 'post_content', (int) $page_id ), $shortcode ) || ! has_shortcode( (string) get_post_field( 'post_content', $translated ), $shortcode ) ) { return $page_id; } return $translated; } function localize_store_ajax( string $url ): string { $language = store_request_language(); if ( ! $language ) { return $url; } // WooCommerce replaces this placeholder in the browser. return str_replace( '%25%25endpoint%25%25', '%%endpoint%%', add_query_arg( 'lang', $language, $url ) ); } function remember_order_language( \WC_Order $order ): void { $language = store_request_language(); if ( $language && locale_for( $language ) ) { $order->update_meta_data( '_uruvo_order_language', $language ); } } final class Store_Order_Language { private static array $orders = array(); private static array $switches = array(); public static function hook_notifications( array $actions ): array { foreach ( $actions as $action ) { if ( ! str_starts_with( $action, 'woocommerce_order_' ) && 'woocommerce_new_customer_note' !== $action ) { continue; } add_action( $action . '_notification', array( self::class, 'begin' ), 1 ); add_action( $action . '_notification', array( self::class, 'end' ), PHP_INT_MAX, 0 ); } return $actions; } public static function begin( $value ): void { $order = $value instanceof \WC_Order ? $value : wc_get_order( is_array( $value ) ? absint( $value['order_id'] ?? 0 ) : absint( $value ) ); self::$orders[] = $order instanceof \WC_Order ? (string) $order->get_meta( '_uruvo_order_language' ) : ''; } public static function end(): void { array_pop( self::$orders ); } public static function active_language(): string { $active = end( self::$switches ); return $active['language'] ?? ''; } public static function switch_email( bool $allowed, \WC_Email $email ): bool { $language = end( self::$orders ); if ( ! $allowed || ! $language || ! $email->is_customer_email() ) { return $allowed; } $locale = locale_for( $language ); if ( ! $locale ) { return $allowed; } $changed = get_locale() !== $locale && switch_to_locale( $locale ); if ( ! $changed && get_locale() !== $locale ) { return $allowed; } self::$switches[] = array( 'email' => $email, 'language' => $language, 'changed' => $changed ); return false; } public static function restore_email( bool $allowed, \WC_Email $email ): bool { $active = end( self::$switches ); if ( ! $active || $active['email'] !== $email ) { return $allowed; } array_pop( self::$switches ); if ( $active['changed'] ) { restore_previous_locale(); } return false; } } function render_store_translation_settings(): void { if ( ! class_exists( '\WooCommerce' ) ) { return; } echo '

' . esc_html__( 'WooCommerce checkout pilot', 'uruvo' ) . '

'; if ( store_language_conflict() ) { echo '

' . esc_html__( 'Your other WooCommerce language integration manages this store.', 'uruvo' ) . '

'; return; } echo '

' . esc_html__( 'Use translated classic cart, checkout and account pages on the same store. Each language uses the same products and inventory. Test on staging before enabling this on a live store.', 'uruvo' ) . '

'; start_form( 'store_translation' ); printf( '

', checked( get_option( 'uruvo_store_translation' ), true, false ), esc_html__( 'Enable translated classic checkout', 'uruvo' ) ); submit_button( __( 'Save WooCommerce settings', 'uruvo' ), 'secondary' ); echo ''; $names = translation_language_names(); if ( ! $names ) { echo '

' . esc_html__( 'Add your site languages first.', 'uruvo' ) . '

'; return; } $labels = array( 'cart' => __( 'Cart', 'uruvo' ), 'checkout' => __( 'Checkout', 'uruvo' ), 'myaccount' => __( 'My account', 'uruvo' ) ); echo '
'; foreach ( $labels as $label ) { printf( '', esc_html( $label ) ); } echo ''; foreach ( $names as $language => $name ) { printf( '', esc_html( $name ) ); foreach ( STORE_PAGES as $page => $shortcode ) { $original = absint( get_option( "woocommerce_{$page}_page_id" ) ); $target = store_page_in_language( $original, $language ); echo ''; } echo ''; } echo '
' . esc_html__( 'Language', 'uruvo' ) . '%s
%s'; if ( ! $original ) { echo esc_html__( 'Set the page in WooCommerce', 'uruvo' ); } elseif ( ! has_shortcode( (string) get_post_field( 'post_content', $original ), $shortcode ) ) { echo esc_html__( 'Classic shortcode required for this pilot', 'uruvo' ); } elseif ( ! $target ) { printf( '%s', esc_url( admin_url_for( array( 'source' => $original, 'wct_targets' => $language ) ) ), esc_html__( 'Create or publish translation', 'uruvo' ) ); } elseif ( ! has_shortcode( (string) get_post_field( 'post_content', $target ), $shortcode ) ) { printf( '%s', esc_url( get_edit_post_link( $target, 'raw' ) ), esc_html__( 'Check the shortcode', 'uruvo' ) ); } else { /* translators: %s: name of the WooCommerce page. */ printf( '%s', esc_url( get_permalink( $target ) ), esc_html( sprintf( __( 'Test %s', 'uruvo' ), $labels[ $page ] ) ) ); } echo '

' . esc_html__( 'Place a test order and check the order email in the same language. Review payment, shipping and privacy wording entered in WooCommerce settings separately.', 'uruvo' ) . '

'; }