Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
Custom order status not appearing translated in the Frontend.

Solution:
Need to apply __() function when registering the custom order status in WooCommerce to make it translatable. Then the string needs to be translated in WPML -> Strings Translation and marked as completed.

This is the final code to add a translatable custom order status.

function register_awaiting_shipment_order_status() {
    register_post_status( 'wc-awaiting-shipment', array(
        'label'                     => __('Awaiting shipment','my_theme'),
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Awaiting shipment <span class="count">(%s)</span>', 'Awaiting shipment <span class="count">(%s)</span>' )
    ) );
}
add_action( 'init', 'register_awaiting_shipment_order_status' );
 
// Add to list of WC Order statuses
function add_awaiting_shipment_to_order_statuses( $order_statuses ) {
 
    $new_order_statuses = array();
 
    // add new order status after processing
    foreach ( $order_statuses as $key => $status ) {
 
        $new_order_statuses[ $key ] = $status;
 
        if ( 'wc-processing' === $key ) {
            $new_order_statuses['wc-awaiting-shipment'] = __('Awaiting shipment','my_theme');
        }
    }
 
    return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_awaiting_shipment_to_order_statuses' );

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 3 replies, has 2 voices.

Last updated by Bernat Torras 4 years, 4 months ago.

Assigned support staff: Bernat Torras.

Author Posts
July 11, 2016 at 7:09 am #950975

Jurgen

I am trying to: translate a custom made order status (WooCommerce)

URL of (my) website where problem appears: hidden link

I expected to see: Translated order status

Instead, I got: Only English order status

Steps to duplicate the issue:

I created a custom order status for WooCommerce using this example code: hidden link . This works fine, but now I want to translate the labels too.

- Scanning the theme for translatable strings, does not find the "_n_noop" statement on line 12 (even if you add a domain as 3rd parameter). Changing it to _ or _e, results into printed strings all over the site (front end and back end), so it needs this _n_noop statement appartently...

'label_count' => _n_noop( 'Awaiting shipment <span class="count">(%s)</span>', 'Awaiting shipment <span class="count">(%s)</span>' )
'label_count' => _n_noop( 'Awaiting shipment <span class="count">(%s)</span>', 'Awaiting shipment <span class="count">(%s)</span>', 'my-domain' )

- The order status on line 28 returns blank in the order detail if you change it to _ or _e (to indicate the text domain using a 2nd paramter)...

$new_order_statuses['wc-awaiting-shipment'] = 'Awaiting shipment';

Please advise on how to translate these texts.

Thanks in advance!
Jurgen

July 11, 2016 at 12:34 pm #951621

Bernat Torras

Hi Jurgen,

Thanks for contacting support, I'll do my best to help you! 🙂

I've checked your code and I think the problem is not the _n_noop. I think you should also wrap the 'Awaiting shipment' string in __() call when defining the label and specially when adding this new status to the list of WooCommerce status.

To do so I would suggest you to use this code instead (you can change 'my_theme' string with the id of your theme):

function register_awaiting_shipment_order_status() {
    register_post_status( 'wc-awaiting-shipment', array(
        'label'                     => __('Awaiting shipment','my_theme'),
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Awaiting shipment <span class="count">(%s)</span>', 'Awaiting shipment <span class="count">(%s)</span>' )
    ) );
}
add_action( 'init', 'register_awaiting_shipment_order_status' );

// Add to list of WC Order statuses
function add_awaiting_shipment_to_order_statuses( $order_statuses ) {

    $new_order_statuses = array();

    // add new order status after processing
    foreach ( $order_statuses as $key => $status ) {

        $new_order_statuses[ $key ] = $status;

        if ( 'wc-processing' === $key ) {
            $new_order_statuses['wc-awaiting-shipment'] = __('Awaiting shipment','my_theme');
        }
    }

    return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_awaiting_shipment_to_order_statuses' );

Then, scan your theme again in WPML>Theme and plugins localization and translate it (marking it as complete) in WPML>Strings Translation.

Let us know if you're able to see the translated status names now. 🙂

Thanks,
Bernat

July 11, 2016 at 1:00 pm #951690

Jurgen

Hi Bernat,

Aaaargh, I missed that line!

To make it 100% translated (so as well in the back end when filtering the orders), I modified the _n_noop( ) statement like this and all works like a charm 🙂

'label_count' => _n_noop( __('Awaiting shipment <span class="count">(%s)</span>', 'my-theme'), __('Awaiting shipment <span class="count">(%s)</span>', 'my-theme') )
July 11, 2016 at 1:29 pm #951750

Bernat Torras

Hi Jurgen,

Glad to know its working! Thanks for providing your updated code, I'm sure it will help other clients. 🙂

Thanks,
Bernat