WooCommerce 訂單顯示自定義文字 以綠界超商取貨門市資訊為例

2017-07-06

使用外掛後 有時候會需要在訂單顯示額外的資訊

這邊以綠界超商取貨門市資訊為例
依序介紹在
1. 管理者後台的訂單詳細頁面
2. 使用者前台的訂單詳細頁面
3. 使用者收到的訂單 email
顯示自定義文字

如果這筆訂單是使用超商取貨
每筆訂單會多出四個資訊
_purchaserStore : 門市名稱
_purchaserAddress : 門市地址
_purchaserPhone : 門市電話
_CVSStoreID : 門市店號

或是有可能為開頭是 _shipping 的資訊
_shipping_purchaserStore : 門市名稱
_shipping_purchaserAddress : 門市地址
_shipping_purchaserPhone : 門市電話
_shipping_CVSStoreID : 門市店號

可以使用以下方式依據訂單編號取得

$_purchaserStore = (array_key_exists('_shipping_purchaserStore', get_post_meta($order->id))) ? get_post_meta( $order->id, '_shipping_purchaserStore', true ) : get_post_meta( $order->id, '_purchaserStore', true );

接著就是在 wp-includes/functions.php 中加入以下程式

// 後台訂單詳細頁面 顯示超商取貨門市資訊
function my_custom_field_admin_info( $order ){
    $_purchaserStore = (array_key_exists('_shipping_purchaserStore', get_post_meta($order->id))) ? get_post_meta( $order->id, '_shipping_purchaserStore', true ) : get_post_meta( $order->id, '_purchaserStore', true );
    if( !empty($_purchaserStore) ){  
        $ecPay_shipping = get_post_meta( $order->id, 'ecPay_shipping', true );
        $shop_type = $ecPay_shipping;
        if ($ecPay_shipping == 'UNIMART' || $ecPay_shipping == 'UNIMART_Collection') {
            $shop_type = '7-11';
        } else if ($ecPay_shipping == 'FAMI' || $ecPay_shipping == 'FAMI_Collection') {
            $shop_type = '全家';
        } else if ($ecPay_shipping == 'HILIFE' || $ecPay_shipping == 'HILIFE_Collection') {
            $shop_type = '萊爾富';
        }

        $_purchaserAddress = (array_key_exists('_shipping_purchaserAddress', get_post_meta($order->id))) ? get_post_meta( $order->id, '_shipping_purchaserAddress', true ) : get_post_meta( $order->id, '_purchaserAddress', true );
        $_purchaserPhone = (array_key_exists('_shipping_purchaserPhone', get_post_meta($order->id))) ? get_post_meta( $order->id, '_shipping_purchaserPhone', true ) : get_post_meta( $order->id, '_purchaserPhone', true );
        $_CVSStoreID = (array_key_exists('_shipping_CVSStoreID', get_post_meta($order->id))) ? get_post_meta( $order->id, '_shipping_CVSStoreID', true ) : get_post_meta( $order->id, '_CVSStoreID', true );

        echo '

門市資訊

'; echo "

". $shop_type ." ".$_purchaserStore." (".$_CVSStoreID .")

"; echo "

門市地址: ".$_purchaserAddress ."

"; if (!empty($_purchaserPhone)) { echo "

門市電話: ".$_purchaserPhone ."

"; } } } add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_field_admin_info' ); // 前台 使用者 訂單詳細頁面 顯示超商取貨門市資訊 function my_custom_field_display_on_order_received( $order ){ $_purchaserStore = (array_key_exists('_shipping_purchaserStore', get_post_meta($order->id))) ? get_post_meta( $order->id, '_shipping_purchaserStore', true ) : get_post_meta( $order->id, '_purchaserStore', true ); if( !empty($_purchaserStore) ){ $ecPay_shipping = get_post_meta( $order->id, 'ecPay_shipping', true ); $shop_type = $ecPay_shipping; if ($ecPay_shipping == 'UNIMART' || $ecPay_shipping == 'UNIMART_Collection') { $shop_type = '7-11'; } else if ($ecPay_shipping == 'FAMI' || $ecPay_shipping == 'FAMI_Collection') { $shop_type = '全家'; } else if ($ecPay_shipping == 'HILIFE' || $ecPay_shipping == 'HILIFE_Collection') { $shop_type = '萊爾富'; } $_purchaserAddress = (array_key_exists('_shipping_purchaserAddress', get_post_meta($order->id))) ? get_post_meta( $order->id, '_shipping_purchaserAddress', true ) : get_post_meta( $order->id, '_purchaserAddress', true ); $_purchaserPhone = (array_key_exists('_shipping_purchaserPhone', get_post_meta($order->id))) ? get_post_meta( $order->id, '_shipping_purchaserPhone', true ) : get_post_meta( $order->id, '_purchaserPhone', true ); $_CVSStoreID = (array_key_exists('_shipping_CVSStoreID', get_post_meta($order->id))) ? get_post_meta( $order->id, '_shipping_CVSStoreID', true ) : get_post_meta( $order->id, '_CVSStoreID', true ); echo '

超商取貨門市資訊

'; echo "

". $shop_type ." ".$_purchaserStore." (".$_CVSStoreID .")

"; echo "

門市地址: ".$_purchaserAddress ."

"; if (!empty($_purchaserPhone)) { echo "

門市電話: ".$_purchaserPhone ."

"; } } } add_action('woocommerce_order_details_after_order_table', 'my_custom_field_display_on_order_received' ); // 訂單 email 顯示超商取貨門市資訊 function my_custom_field_order_email( $order, $sent_to_admin ){ $_purchaserStore = (array_key_exists('_shipping_purchaserStore', get_post_meta($order->id))) ? get_post_meta( $order->id, '_shipping_purchaserStore', true ) : get_post_meta( $order->id, '_purchaserStore', true ); if( !empty($_purchaserStore) ){ $ecPay_shipping = get_post_meta( $order->id, 'ecPay_shipping', true ); $shop_type = $ecPay_shipping; if ($ecPay_shipping == 'UNIMART' || $ecPay_shipping == 'UNIMART_Collection') { $shop_type = '7-11'; } else if ($ecPay_shipping == 'FAMI' || $ecPay_shipping == 'FAMI_Collection') { $shop_type = '全家'; } else if ($ecPay_shipping == 'HILIFE' || $ecPay_shipping == 'HILIFE_Collection') { $shop_type = '萊爾富'; } $_purchaserAddress = (array_key_exists('_shipping_purchaserAddress', get_post_meta($order->id))) ? get_post_meta( $order->id, '_shipping_purchaserAddress', true ) : get_post_meta( $order->id, '_purchaserAddress', true ); $_purchaserPhone = (array_key_exists('_shipping_purchaserPhone', get_post_meta($order->id))) ? get_post_meta( $order->id, '_shipping_purchaserPhone', true ) : get_post_meta( $order->id, '_purchaserPhone', true ); $_CVSStoreID = (array_key_exists('_shipping_CVSStoreID', get_post_meta($order->id))) ? get_post_meta( $order->id, '_shipping_CVSStoreID', true ) : get_post_meta( $order->id, '_CVSStoreID', true ); echo '

超商取貨門市資訊

'; echo "

". $shop_type ." ".$_purchaserStore." (".$_CVSStoreID .")

"; echo "

門市地址: ".$_purchaserAddress ."

"; if (!empty($_purchaserPhone)) { echo "

門市電話: ".$_purchaserPhone ."

"; } } } add_action( 'woocommerce_email_after_order_table', 'my_custom_field_order_email', 10, 2 );

以上三個函式都是使用 hook 的方式在預設的內容中插入自定義文字
所以可以依據自身情況修改其內的程式

以下為設置後會顯示的地方及樣式

管理者後台的訂單詳細頁面

使用者前台的訂單詳細頁面

使用者收到的訂單 email

ref:

WooCommerce添加自定义结账字段图文详解