Skip Navigation

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 6 replies, has 2 voices.

Last updated by Andreas W. 1 month, 1 week ago.

Assigned support staff: Andreas W..

Author Posts
October 14, 2021 at 2:32 pm #9791163

lariH

Hi.

When I try to edit a custom code snippet on the checkout page (see attached) through the String Translation.
It saves the changes in the editor but does not show the translations at the Front-End.

Also, I have tried to enter the website through a new window (Guest window) and tried to add a product to the cart and it doesn't add any product with a variation at all - I have only three products on the website...

I have somehow bypassed it but only through the admin user when I am logged in and I can't see the variations on the packing slip of the credit company - when I am at the checkout page of the, at the payment.

Before all that, we have translated the whole website and nothing was showing so we started to edit through Elementor in the English language and inserting the translations through the editor and this was not the reason we have purchased the plugin...

Look, there are too many problems and it is kind of disappointing...

I really need help on the matter,
waiting to hear from you ๐Ÿ™‚

9790677-Screenshot_2021_10_14_164908.png
9790677-Screenshot_2021_10_14_164821.png
9790677-Screenshot_2021_10_14_164715.png
9790677-Screenshot_2021_10_14_164630.png
October 16, 2021 at 8:21 am #9801881

Andreas W.
Supporter

Languages: German (Deutsch )

Timezone: America/Lima (GMT-05:00)

Hello,

I am afraid that it will not work to translate PHP code with String Translation in this manner.

Could you please send me the actual snippet top have a better look at the strings?

Usually, you will need to edit the theme template, setup a GetText Call for each string and translate only the string on String Translation.

How to set a GetText Call in PHP:
https://codex.wordpress.org/I18n_for_WordPress_Developers#Introduction_to_Gettext

Alternatively, you could inside a child theme or use the child theme's functions.php file and setup a condition while running the following hook:

https://wpml.org/wpml-hook/wpml_current_language/

Example:
<?php
$my_current_lang = apply_filters( 'wpml_current_language', NULL );
if( $my_current_lang =='en' ) {
<---English Content--->
}
if ( $my_current_lang =='he' ) {
<---Hebrew Content--->
}
?>

Best regards
Andreas

October 17, 2021 at 12:28 pm #9805215

lariH

Hey Andreas!

The following is the php code that I am trying to implement on the website:


add_action( 'woocommerce_review_order_before_submit', 'bt_add_checkout_checkbox', 10 );
/**
 * Add WooCommerce additional Checkbox checkout field
 */
function bt_add_checkout_checkbox() {
   
    woocommerce_form_field( 'checkout_checkbox', array( // CSS ID
       'type'          => 'checkbox',
       'class'         => array('form-row mycheckbox'), // CSS Class
       'label_class'   => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
       'input_class'   => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
       'required'      => true, // Mandatory or Optional
       'label'         => 'I agree to the <a href="<em><u>hidden link</u></em>" target="_blank" rel="noopener">Regulations and Policies of the Company's Website</a>', // Label and Link
    ));    
}

add_action( 'woocommerce_checkout_process', 'bt_add_checkout_checkbox_warning' );
/**
 * Alert if checkbox not checked
 */ 
function bt_add_checkout_checkbox_warning() {
    if ( ! (int) isset( $_POST['checkout_checkbox'] ) ) {
        wc_add_notice( __( 'You have to agree to the Regulations and Policies of the Company's Website to complete the payment on the website' ), 'error' );
    }
}

add_action( 'woocommerce_checkout_update_order_meta', 'bt_checkout_field_order_meta_db' );
/**
 * Add custom field as order meta with field value to database
 */
function bt_checkout_field_order_meta_db( $order_id ) {
    if ( ! empty( $_POST['checkout_checkbox'] ) ) {
        update_post_meta( $order_id, 'checkout_checkbox', sanitize_text_field( $_POST['checkout_checkbox'] ) );
    }
}

add_action( 'woocommerce_admin_order_data_after_billing_address', 'bt_checkout_field_display_admin_order_meta', 10, 1 );
/**
 * Display field value on the backend WooCOmmerce order
 */
function bt_checkout_field_display_admin_order_meta($order){
    echo '<p><strong>'.__('I agree to the <a href="<em><u>hidden link</u></em>" target="_blank" rel="noopener">Regulations and Policies of the Company's Website</a> ').':</strong> ' . get_post_meta( $order->get_id(), 'checkout_checkbox', true ) . '<p>'; 
}

add_filter( 'woocommerce_email_order_meta_fields', 'bt_woocommerce_email_order_meta_fields', 10, 3 );
/**
 * Add a custom field (in an order) to the emails with text and label
 */

function bt_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
    
    $checkbox = get_post_meta( $order->get_id(), 'checkout_checkbox', true );
    
    if( $checkbox == 1 ) {
        $fields['checkout_checkbox'] = array(
            'label' => __( 'You have agreed to the :' ),
            'value' => __( '<a href="<em><u>hidden link</u></em>" target="_blank" rel="noopener">Regulations and Policies of the Company's Website</a> Carroself.com' ),
        );
        return $fields;
    }
    else {
        $fields['checkout_checkbox'] = array(
            'label' => __( 'You haven't agreed to the ' ),
            'value' => __( '<a href="<em><u>hidden link</u></em>" target="_blank" rel="noopener">Regulations and Policies of the Company's Website</a> Carroself.com' ),
        );
        return $fields;
    }
}

I don't know yet how to program in PHP unfortunately.

Waiting to hear from you ๐Ÿ™‚
Lari

October 19, 2021 at 10:50 am #9817203

lariH

Hey Andreas!

Just wanted to check if you found any solution for the current issue by any chance ๐Ÿ™‚

Waiting to hear from you ๐Ÿ™‚
Lari

October 19, 2021 at 7:11 pm #9821443

Andreas W.
Supporter

Languages: German (Deutsch )

Timezone: America/Lima (GMT-05:00)

Hi Lari,

Sorry for the delay, as I do not work on Sundays and Mondays and have quiet many tickets to reply today.

Would you mind if you please connect to this test site and try to recreate the issue there?

I will then try ot assist you in making those strings translatable.

WooCommerce and WPML are pre-installed in their latest versions.

One-Click-Login:
hidden link

Ther eis not FTP access available but we have installed the WP File Manager which will allow you to upload and files or folders.

Please leave me short notification once the site is ready for testing.

Best regards
Andreas

October 20, 2021 at 9:39 am #9824895

lariH

Hey Andreas!
I am happy to hear from you.

I did what you asked me to do. And I recreated the issue on the website exactly as you can see in the images attached.

Waiting to hear from you ๐Ÿ™‚
Lari

Screenshot 2021-10-20 123219.png
Screenshot 2021-10-20 123510.png
Screenshot 2021-10-20 123041.png
Screenshot 2021-10-20 122933.png
October 20, 2021 at 2:17 pm #9827787

Andreas W.
Supporter

Languages: German (Deutsch )

Timezone: America/Lima (GMT-05:00)

Hello Lari,

As mentioned, this will properly not work when using a plugin.

I took your code and added it to the functions.php file of the theme and further took some minor edits, regarding text-domains and GetText calls.
https://codex.wordpress.org/I18n_for_WordPress_Developers#Introduction_to_Gettext

After adding the code to the theme you need to scan the theme for new strings at WPML - Theme & Plugin Localization.

The current code looks like that:

add_action( 'woocommerce_review_order_before_submit', 'bt_add_checkout_checkbox', 10 );
/**
 * Add WooCommerce additional Checkbox checkout field 
 */
function bt_add_checkout_checkbox() {
   
    woocommerce_form_field( 'checkout_checkbox', array( // CSS ID
       'type'          => 'checkbox',
       'class'         => array('form-row mycheckbox'), // CSS Class
       'label_class'   => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
       'input_class'   => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
       'required'      => true, // Mandatory or Optional
       'label'         => esc_html_e('ืื ื™ ืžืกื›ื™ื ืœ<a href="<em><u>hidden link</u></em>" target="_blank" rel="noopener">ืชืงื ื•ืŸ ื•ืžื“ื™ื ื™ื•ืช ื”ื—ื‘ืจื”</a> ื‘ืืชืจ', 'custom-functions') // Label and Link
    ));    
}

add_action( 'woocommerce_checkout_process', 'bt_add_checkout_checkbox_warning' );
/**
 * Alert if checkbox not checked
 */ 
function bt_add_checkout_checkbox_warning() {
    if ( ! (int) isset( $_POST['checkout_checkbox'] ) ) {
        wc_add_notice( _e( 'ื™ืฉ ืœืืฉืจ ืฉืงืจืืช ืืช ืชืงื ื•ืŸ ื•ืžื“ื™ื ื™ื•ืช ื”ื—ื‘ืจื” ืขืœ ืžื ืช ืœื”ืฉืœื™ื ืืช ื”ืชืฉืœื•ื ื‘ืืชืจ' ), 'custom-functions' );
    }
}

add_action( 'woocommerce_checkout_update_order_meta', 'bt_checkout_field_order_meta_db' );
/**
 * Add custom field as order meta with field value to database
 */
function bt_checkout_field_order_meta_db( $order_id ) {
    if ( ! empty( $_POST['checkout_checkbox'] ) ) {
        update_post_meta( $order_id, 'checkout_checkbox', sanitize_text_field( $_POST['checkout_checkbox'] ) );
    }
}

add_action( 'woocommerce_admin_order_data_after_billing_address', 'bt_checkout_field_display_admin_order_meta', 10, 1 );
/**
 * Display field value on the backend WooCOmmerce order
 */
function bt_checkout_field_display_admin_order_meta($order){
    echo '<p><strong>'.esc_html_e('ืื ื™ ืžืกื›ื™ื ืœ<a href="<em><u>hidden link</u></em>" target="_blank" rel="noopener">ืชืงื ื•ืŸ ื•ืžื“ื™ื ื™ื•ืช ื”ื—ื‘ืจื”</a> ื‘ืืชืจ').':</strong> ' . get_post_meta( $order->get_id(), 'checkout_checkbox', true ) . '<p>', 'custom-functions'; 
}

add_filter( 'woocommerce_email_order_meta_fields', 'bt_woocommerce_email_order_meta_fields', 10, 3 );
/**
 * Add a custom field (in an order) to the emails with text and label
 */

function bt_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
    
    $checkbox = get_post_meta( $order->get_id(), 'checkout_checkbox', true );
    
    if( $checkbox == 1 ) {
        $fields['checkout_checkbox'] = array(
            'label' => _e( 'ื”ืกื›ืžืช ืœ:', 'custom-functions' ),
            'value' => esc_html_e( '<a href="<em><u>hidden link</u></em>" target="_blank" rel="noopener">ืชืงื ื•ืŸ ื•ืžื“ื™ื ื™ื•ืช ื”ื—ื‘ืจื”</a> ื‘ืืชืจ Carroself.com', 'custom-functions' ),
        );
        return $fields;
    }
    else {
        $fields['checkout_checkbox'] = array(
            'label' => _e( 'ืœื ื”ืกื›ืžืช ืœ', 'custom-functions' ),
            'value' => esc_html_e( '<a href="<em><u>hidden link</u></em>" target="_blank" rel="noopener">ืชืงื ื•ืŸ ื•ืžื“ื™ื ื™ื•ืช ื”ื—ื‘ืจื”</a> ื‘ืืชืจ Carroself.com', 'custom-functions' ),
        );
        return $fields;
    }
}

Further, after locating the strings on WPML String Translation I had to assign them to Hebrew, as those by default are registered as English strings. Maybe you might even wish to consider adjusting the code to use English text and links instead and then translate with String Translation to Hebrew.

Please have a look at the sandbox.

The strings which I was able to locate are found in the text-domian 'custom-functions'.

Even though I was not yet able to locate all the string yet, I could locate four but somehow I do have an issue with strings that are mixing PHP and HTML yet and the code would need to get adjusted further.

Anyhow, I hope I already was able to guide on the right path.

Please have a review of the code and we will keep working from there, if needed.

In order to apply code to the theme's functions.php file you further should create a child theme.
https://developer.wordpress.org/themes/advanced-topics/child-themes/

Best regards
Andreas

The topic ‘[Closed] Split: Changes are saved in WPML String Translation but not showing updated on the front-end’ is closed to new replies.