- 상태
- 개발자가 해결함
- 해결된 버전
- WooCommerce Product Options 2.6.0
- 관련 플러그인/테마
- WooCommerce Product Options
- 주제 태그
- Compatibility, WCML
문제 개요
WPML과 함께 Barn2의 WooCommerce Product Options를 사용할 때, 상품 페이지에서는 올바르게 번역되더라도 장바구니, 결제 및 미니 장바구니에서는 상품 옵션 문자열이 번역되지 않은 상태로 표시됩니다.
임시 해결 방법
계속 진행하기 전에 사이트의 전체 사이트 백업을 수행했는지 확인하세요.
- 테마의 functions.php 파일에 다음 코드를 붙여넣으세요:
/** * Translate WooCommerce Product Options texts when adding to cart * using the plugin's own WPML integration. */ add_filter( 'woocommerce_add_cart_item_data', 'my_wpo_translate_cart_meta_with_wpml', 20, 4 ); function my_wpo_translate_cart_meta_with_wpml( $cart_item_data, $product_id, $variation_id, $quantity ) { // If there are no WPO options in this cart item, do nothing. if ( empty( $cart_item_data['wpo_options'] ) ) { return $cart_item_data; } // Make sure the plugin classes exist (plugin is active and loaded). if ( ! class_exists( 'Barn2PluginWC_Product_OptionsPlugin_Factory' ) || ! class_exists( 'Barn2PluginWC_Product_OptionsIntegrationWPML' ) || ! class_exists( 'Barn2PluginWC_Product_OptionsModelOption' ) ) { return $cart_item_data; } // Get the plugin instance and the WPML integration helper. $plugin = Barn2PluginWC_Product_OptionsPlugin_Factory::create( '', '' ); $wpml_integration = new Barn2PluginWC_Product_OptionsIntegrationWPML( $plugin ); foreach ( $cart_item_data['wpo_options'] as $option_id => &$option_data ) { // Load the option model so we can access the original data. $option = Barn2PluginWC_Product_OptionsModelOption::find( $option_data['option_id'] ?? 0 ); if ( ! $option ) { continue; } // 1) Translate the option name (the "key" shown in cart/checkout). $translated_name = $wpml_integration->translate_string( $option->name, $option, 'option_name' ); $option_data['name'] = $translated_name; // 2) Translate each choice label, if any. if ( empty( $option_data['choice_data'] ) || empty( $option->choices ) ) { continue; } // Build a map: choice_id => index, to reproduce the same index WPML uses. $index_by_id = []; foreach ( $option->choices as $idx => $choice ) { if ( isset( $choice['id'] ) ) { $index_by_id[ $choice['id'] ] = $idx; } } // Normalize the stored value to an array (covers both single and multi). $values = is_array( $option_data['value'] ) ? $option_data['value'] : [ $option_data['value'] ]; foreach ( $option_data['choice_data'] as $i => &$choice_data ) { if ( ! isset( $choice_data['label'] ) ) { continue; } // For free-text fields, there are usually no predefined choices to translate. if ( empty( $index_by_id ) ) { continue; } $value_for_this_choice = $values[ $i ] ?? null; if ( ! isset( $index_by_id[ $value_for_this_choice ] ) ) { continue; } $choice_index = $index_by_id[ $value_for_this_choice ]; $original_label = $option->choices[ $choice_index ]['label']; // Translate the choice label using the same context and index as when registered. $choice_data['label'] = $wpml_integration->translate_string( $original_label, $option, 'choice_label', $choice_index ); } } return $cart_item_data; } - 참고: 장바구니에 상품을 추가한 후 언어를 변경하면 레이블이 “번역”되지 않습니다. 상품을 추가할 때 활성화되어 있던 언어로 유지됩니다(고정 텍스트를 저장하는 다른 메타데이터와 마찬가지입니다). 하지만 이 단계에서 언어를 전환하는 경우는 드뭅니다. Barn2에서 곧 영구적인 수정 사항을 적용할 예정이므로, 배포되면 추가된 임시 해결 방법을 제거하세요.