Skip to content Skip to sidebar

Tagged: 

This topic contains 0 reply, has 1 voice.

Last updated by marcoP-130 1 year, 4 months ago.

Assisted by: Osama Mersal.

Author Posts
June 5, 2024 at 9:31 am #15705960

marcoP-130

Hello Osama
I created a code on function.php, that need to be shown on all the languages, right now, only the english language, main website language, are showing this code, I was able to get the string translated, but i believe i done something wrong, because i only need to translate the "title" the value is the same. this is the code I created

// Add GTIN / EAN column to product list view
add_filter( 'manage_edit-product_columns', 'add_gtin_ean_column_to_product_list' );

function add_gtin_ean_column_to_product_list( $columns ) {
// Add new column
$columns['gtin_ean'] = __( 'GTIN / EAN', 'woocommerce' );
return $columns;
}

// Populate GTIN / EAN column with value
add_action( 'manage_product_posts_custom_column', 'populate_gtin_ean_column', 10, 2 );

function populate_gtin_ean_column( $column, $post_id ) {
if ( $column == 'gtin_ean' ) {
$gtin_ean = get_post_meta( $post_id, '_gtin_ean', true );
echo esc_html( $gtin_ean );
}
}

// Register GTIN/EAN meta key with WPML
function wpml_register_gtin_ean_meta() {
if ( function_exists( 'icl_register_string' ) ) {
$products = get_posts( array( 'post_type' => 'product', 'numberposts' => -1 ) );
foreach ( $products as $product ) {
$gtin_ean = get_post_meta( $product->ID, '_gtin_ean', true );
icl_register_string( 'GTIN EAN', 'gtin_ean_' . $product->ID, $gtin_ean );
}
}
}
add_action( 'init', 'wpml_register_gtin_ean_meta' );

// Sync GTIN/EAN meta field across translations
function sync_gtin_ean_with_translations($post_id, $post, $update) {
if ($post->post_type != 'product') {
return;
}

// Get GTIN/EAN value
$gtin_ean = get_post_meta($post_id, '_gtin_ean', true);

// Get translated post IDs
$translations = apply_filters('wpml_get_element_translations', null, apply_filters('wpml_element_type', 'post_product'), $post_id);

foreach ($translations as $lang => $translation) {
// Skip the original language
if ($translation->element_id == $post_id) {
continue;
}

// Update translated post meta
update_post_meta($translation->element_id, '_gtin_ean', $gtin_ean);
}
}
add_action('save_post', 'sync_gtin_ean_with_translations', 10, 3);