首页 文章

在Woocommerce电子邮件通知中以编程方式显示结帐字段标签

提问于
浏览
1

在我的自定义订单WooCommerce电子邮件模板到客户端,我正在插入一个自定义的运输和计费数据列表 . 带有标签的第一个列表列和带有相应数据的第二个列表列 .

目前,我手动插入了标签名称,等于woocommerce checkout中的标签名称,然后我按代码插入了相应的值/数据 . 这意味着我必须将所有这些标签翻译成我需要的所有语言,当我已经使用原生Woocommerce结帐标签完成时 .

此外,由于这样做了两次,因此翻译可能不完全相同 . 这不是有效的,也增加了代码 .

为了避免这种情况,我想用相应的woocommerce结帐字段标签替换标签 .

在我当前的代码下面,每个字符串'$ text_xx'代表我之前手动定义的标签:

# (...)
$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

    这可以通过此自定义函数完成,该函数将显示按预期排序的所有现有运输标签字段:

    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;
    }
    

    代码位于活动子主题(或活动主题)的function.php文件中 .


    现在,您可以通过以下方式在模板代码中使用此函数:

    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>';
    

    经过测试和工作 .

相关问题