1
votes

In my custom order WooCommerce email templates to clients, I am inserting a custom list of shipping and billing data. The first list column with the label and the second with the respective data.

For the moment, I have inserted manually the label names, equal to those in woocommerce checkout, and then I have inserted by code the respective values/data. This implies that I have to translate all these labels to all the languages that I need, when I have already done that with the native Woocommerce checkout labels.

Also, as this is done twice, it can be the case that the translations are not exactly equal. This is not efficient and also increases code.

In order to avoid this, I would like to substitute the labels by the corresponding woocommerce checkout field labels.

Below my current code, where each string '$text_xx' represent a label that I have manually defined before:

# (...)
$text_42b = __('First name:', 'woocommerce_php_emails');
# (...)

   echo '<p id="Linha1">' . $line . '</p><p id="Shipping_title_completed">' . $text_41 . '</p>
           <div class="Shipping_table_completed">
                <div class="left_table" style="float: left; width: 25%;"><ul>
                    <li>' . $text_42a . '</li>
                    <li>' . $text_42b . '</li>
                    <li>' . $text_42c . '</li>
                    <li>' . $text_42d . '</li>
                    <li>' . $text_42e . '</li>
                    <li>' . $text_42f . '</li>
                    <li>&nbsp;</li>
                    <li>' . $text_42g . '</li>
                    <li>' . $text_42h . '</li>
                    <li>' . $text_42i . '</li>
                    <li>' . $text_42j . ':</li>
                </ul></div>    
                <div class="right_table" style="float: left; width: 75%;"><ul>
                    <li>' . $shipping_title . '&nbsp;</li>
                    <li>' . $order->get_shipping_first_name() . '&nbsp;</li>
                    <li>' . $order->get_shipping_last_name() . '&nbsp;</li>
                    <li>'. $order->get_shipping_company() . '&nbsp;</li>
                    <li>' . $tracking_num_s . '&nbsp;</li>
                    <li>' . $order->get_shipping_address_1() . '&nbsp;</li>
                    <li>' . $order->get_shipping_address_2() . '&nbsp;</li>
                    <li>' . $order->get_shipping_postcode() . '&nbsp;</li>
                    <li>' . $order->get_shipping_city() . '&nbsp;</li>
                    <li>' . $order->get_shipping_state() . '&nbsp;</li>
                    <li>' . $order->get_shipping_country() . '&nbsp;</li>
                </ul></div>
                <div class="Shipping_table_completed1"><img src="https://testing987654321hello.com/wp-content/uploads/2017/12/delivery-service.jpg" alt=""></div>
            </div><p id="Linha2">' . $line . '</p><div class="Last_text_completed"><p>' . $text_61 . '<br><br><br>' . $text_60 . '</p></div>';
1

1 Answers

2
votes

This can be done with this custom function that will display all existing shipping label fields sorted as you expect:

function get_checkout_label_fields( $type = 'shipping' ){
    $domain = 'my_theme_slug'; // <= define your theme domain

    // Setting custom label fields value
    $fields1 = array(
        'shipping_title' => array(
            'label' => __( 'Shipping title', $domain ), // <= define your title
        ),
        'shipping_traking_number' => array(
            'label' => __( 'Tracking number', $domain ), // <= define Tracking number label
        )
    );
    $label_sep = __( ':', $domain );

    $checkout = new WC_Checkout(); // Get an instance of the  WC_Checkout object
    $fields2 = $checkout->get_checkout_fields($type); // Get the fields

    $fields = array_merge( $fields1, $fields2 ); // Merge arrays
    $output = array();

    // Sorting field labels array
    $labels = array( 'title', 'first_name', 'last_name', 'company', 'traking_number',
        'address_1', 'address_2','postcode', 'city', 'state', 'country' );

    // Loop through labels array to set the correct label values
    foreach( $labels as $label ){
        $label = 'shipping_'.$label;

        if( empty($fields[$label]['label']) )
            $fields[$label]['label'] = '&nbsp;';

        $output[$label] = $fields[$label]['label'];
    }
    return $output;
}

Code goes in function.php file of your active child theme (or active theme).


Now you can use this function in your template code this way:

echo '<p id="Linha1">' . $line . '</p>
<p id="Shipping_title_completed">' . $text_41 . '</p>
<div class="Shipping_table_completed">
    <div class="left_table" style="float: left; width: 25%;">
        <ul>';

foreach(get_checkout_label_fields() as $label_field )            
    echo '<li>' . $label_field . '</li>';

echo '</ul>
    </div>    
    <div class="right_table" style="float: left; width: 75%;">
        <ul>
            <li>' . $shipping_title . '&nbsp;</li>
            <li>' . $order->get_shipping_first_name() . '&nbsp;</li>
            <li>' . $order->get_shipping_last_name() . '&nbsp;</li>
            <li>'. $order->get_shipping_company() . '&nbsp;</li>
            <li>' . $tracking_num_s . '&nbsp;</li>
            <li>' . $order->get_shipping_address_1() . '&nbsp;</li>
            <li>' . $order->get_shipping_address_2() . '&nbsp;</li>
            <li>' . $order->get_shipping_postcode() . '&nbsp;</li>
            <li>' . $order->get_shipping_city() . '&nbsp;</li>
            <li>' . $order->get_shipping_state() . '&nbsp;</li>
            <li>' . $order->get_shipping_country() . '&nbsp;</li>
        </ul>
    </div>
    <div class="Shipping_table_completed1">
        <img src="https://testing987654321hello.com/wp-content/uploads/2017/12/delivery-service.jpg" alt="">
    </div>
</div>
<p id="Linha2">' . $line . '</p>
<div class="Last_text_completed">
    <p>' . $text_61 . '<br><br><br>' . $text_60 . '</p>
</div>';

Tested and works.