Created
November 16, 2026 08:32
-
-
Save plugin-republic/97a965573e7ec4a0870757fee34952e5 to your computer and use it in GitHub Desktop.
WooCommerce Product Add-Ons Ultimate + WPML: load translated global groups assigned at product level
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * WooCommerce Product Add-Ons Ultimate + WPML | |
| * Load the translated version of global groups that are assigned on the product itself. | |
| * Add to your child theme's functions.php or a code snippets plugin. | |
| * | |
| * Requirements: | |
| * - In WPML > Settings > Custom Fields Translation, set 'pewc_global_groups_by_product' to "Copy". | |
| * - Each translated global group needs its own fields, conditions and calculations. | |
| */ | |
| add_filter( 'pewc_filter_product_extra_groups', 'prefix_translate_product_level_global_groups', 20, 2 ); | |
| function prefix_translate_product_level_global_groups( $groups, $product_id ) { | |
| if ( ! defined( 'ICL_LANGUAGE_CODE' ) || empty( $groups ) || ! is_array( $groups ) ) { | |
| return $groups; | |
| } | |
| // Nothing to do in the default language | |
| $current_lang = apply_filters( 'wpml_current_language', null ); | |
| $default_lang = apply_filters( 'wpml_default_language', null ); | |
| if ( ! $current_lang || $current_lang === $default_lang ) { | |
| return $groups; | |
| } | |
| // Global groups assigned via the 'Assign global group' setting on the product | |
| $assigned = get_post_meta( $product_id, 'pewc_global_groups_by_product', true ); | |
| if ( ! $assigned ) { | |
| return $groups; | |
| } | |
| $assigned = array_map( 'absint', explode( ',', $assigned ) ); | |
| $translated_groups = array(); | |
| foreach ( $groups as $group_id => $group ) { | |
| if ( in_array( (int) $group_id, $assigned, true ) ) { | |
| $translated_id = (int) apply_filters( 'wpml_object_id', $group_id, 'pewc_group', true, $current_lang ); | |
| // Only swap if a published translation exists and it has fields | |
| if ( $translated_id && $translated_id !== (int) $group_id && 'publish' === get_post_status( $translated_id ) ) { | |
| $items = pewc_get_group_fields( $translated_id ); | |
| if ( ! empty( $items ) ) { | |
| $group['items'] = $items; | |
| $translated_groups[ $translated_id ] = $group; | |
| continue; | |
| } | |
| } | |
| } | |
| $translated_groups[ $group_id ] = $group; | |
| } | |
| return $translated_groups; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment