【问题标题】:Show programmatically checkout field labels in Woocommerce emails notifications在 Woocommerce 电子邮件通知中以编程方式显示结帐字段标签
【发布时间】:2017-12-29 10:40:48
【问题描述】:

在我给客户的自定义订单 WooCommerce 电子邮件模板中,我插入了一个自定义的运输和账单数据列表。第一个列表列带有标签,第二个带有相应的数据。

目前,我手动插入了标签名称,与 woocommerce 结帐中的名称相同,然后我通过代码插入了相应的值/数据。这意味着我必须将所有这些标签翻译成我需要的所有语言,而我已经使用原生 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>';

【问题讨论】:

    标签: php wordpress woocommerce labels email-notifications


    【解决方案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>';
    

    经过测试并且有效。

    【讨论】:

    • 像魅力伴侣一样工作。再次非常感谢您。
    猜你喜欢
    • 2019-03-27
    • 2023-03-18
    • 2020-01-10
    • 2018-04-24
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    • 2018-08-31
    • 2020-11-05
    相关资源
    最近更新 更多