首页 > 解决方案 > 在 WooCommerce 上为格式化的计费电话和电子邮件添加标签谢谢

问题描述

我想在“谢谢”页面上为电话和电子邮件信息添加自定义标签。使用 YITH WooCommerce Checkout Manager,我自定义了一些字段并将以下代码添加到 functions.php:

function woo_custom_order_formatted_billing_address( $address , $WC_Order ) {
$address = array(
            'first_name' => 'First Name: ' . $WC_Order->billing_first_name,
            'address_1'  => 'Address: ' . $WC_Order->billing_address_1,
            'city'      => 'City: ' . $WC_Order->billing_city,
        );

        if(!empty($ship_date)) $address['order_time'] = 'Shipping time: ' . $ship_date;
        if(!empty($WC_Order->billing_corpus)) $address['house'] = 'House: ' . $WC_Order->billing_house;
        if(!empty($WC_Order->billing_corpus)) $address['corpus'] = 'Corpus: ' . $WC_Order->billing_corpus;
        if(!empty($WC_Order->billing_flat)) $address['flat'] = 'Room: ' . $WC_Order->billing_flat;
            if(!empty($WC_Order->billing_phone)) $address['billing_phone'] = 'Phone: ' . $WC_Order->billing_phone;

        return $address;
}

但出于某种原因,不想添加标签“电话”。

结果

有什么解决办法吗?

标签: phpwordpresstemplateswoocommercehook-woocommerce

解决方案


为此,您可以使用 2 种不同的方式进行操作:

1)。通过您的主题覆盖 WooCommerce 模板

您必须复制/编辑order/order-details-customer.php模板文件,因为帐单格式的地址功能不处理帐单电话和帐单电子邮件。

对于计费电话,您需要更换线路37

<p class="woocommerce-customer-details--phone"><?php echo esc_html( $order->get_billing_phone() ); ?></p>

通过以下行:

<p class="woocommerce-customer-details--phone"><?php _e("Phone: ", "woocommerce"); echo esc_html( $order->get_billing_phone() ); ?></p>

对于账单电子邮件,您需要替换以下行41

<p class="woocommerce-customer-details--email"><?php echo esc_html( $order->get_billing_email() ); ?></p>

通过以下行:

<p class="woocommerce-customer-details--email"><?php _e("Email: ", "woocommerce"); echo esc_html( $order->get_billing_email() ); ?></p>

2)。使用一些复合过滤器钩子 (对于收到的订单 - 谢谢页面)

// Phone
add_filter('woocommerce_order_get_billing_phone', 'wc_order_get_billing_phone_filter' );
function wc_order_get_billing_phone_filter( $billing_phone ) {
    // Only on Order Received page (thankyou)
    if ( is_wc_endpoint_url( 'order-received' ) && $billing_phone ) {
        return __("Phone:", "woocommerce") . ' ' . $billing_phone;
    }
    return $billing_phone;
}

// Email
add_filter('woocommerce_order_get_billing_email', 'wc_order_get_billing_email_filter' );
function wc_order_get_billing_email_filter( $billing_email ) {
    // Only on Order Received page (thankyou)
    if ( is_wc_endpoint_url( 'order-received' ) && $billing_email ) {
        return __("Email:", "woocommerce") . ' ' . $billing_email;
    }
    return $billing_email;
}

代码在您的活动子主题(或活动主题)的functions.php 文件中。测试和工作。


推荐阅读