WordPressのプラグイン「WooCommerce」には、メールのテンプレートが用意されています。

変更を加えなくても、必要最低限の内容のメールを送信することが可能です。

しかし今回は、メール本文の頭に「おうち IT様」のように注文者名を追加したかったため、カスタマイズしてみました。

 

前提

・プラグイン「WooCommerce」インストール済み

・プラグイン「WooCommerce For Japan」インストール済み

・プラグイン「WooCommerce Email Test」インストール済み

・子テーマ作成済み

 

WooCommerce→設定→メール

WordPress管理画面から、WooCommerce→設定→メールを選択します。

今回は「処理中の注文」テンプレートを選択し、注文者名を追加してみます。

HTMLテンプレートの「テーマにファイルをコピー」ボタンを押下し、コピーが完了したら「テンプレートを表示」ボタンを押下すると、ソースコードが現れます。

 

テンプレート修正

修正前

<?php
/**
 * Customer processing order email
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/emails/customer-processing-order.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see 	    https://docs.woocommerce.com/document/template-structure/
 * @author 		WooThemes
 * @package 	WooCommerce/Templates/Emails
 * @version     2.5.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * @hooked WC_Emails::email_header() Output the email header
 */
do_action( 'woocommerce_email_header', $email_heading, $email ); ?>

<p><?php _e( "Your order has been received and is now being processed. Your order details are shown below for your reference:", 'woocommerce' ); ?></p>

<?php

/**
 * @hooked WC_Emails::order_details() Shows the order details table.
 * @hooked WC_Structured_Data::generate_order_data() Generates structured data.
 * @hooked WC_Structured_Data::output_structured_data() Outputs structured data.
 * @since 2.5.0
 */

修正後

<?php
/**
 * Customer processing order email
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/emails/customer-processing-order.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see 	    https://docs.woocommerce.com/document/template-structure/
 * @author 		WooThemes
 * @package 	WooCommerce/Templates/Emails
 * @version     2.5.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * @hooked WC_Emails::email_header() Output the email header
 */
do_action( 'woocommerce_email_header', $email_heading, $email ); ?>

<p><?php echo $order->get_formatted_billing_full_name(); ?>様</p>
<p><?php _e( "Your order has been received and is now being processed. Your order details are shown below for your reference:", 'woocommerce' ); ?></p>

<?php

/**
 * @hooked WC_Emails::order_details() Shows the order details table.
 * @hooked WC_Structured_Data::generate_order_data() Generates structured data.
 * @hooked WC_Structured_Data::output_structured_data() Outputs structured data.
 * @since 2.5.0
 */

28行目に1行追加しました。

$order->get_formatted_billing_full_name()で氏名を取得し、「様」を付与しています。

WooCommerceドキュメントを見てみると、他にも様々な項目を取得出来そうです。
https://docs.woocommerce.com/wc-apidocs/class-WC_Order.html

 

メール画面プレビュー

変更を保存後、プラグイン「WooCommerce Email Test」を使ってメール画面をプレビューしてみます。

無事に「おうち IT様」と追加されました。