WordPress(以下wp)とWooCommerce(以下wc)で、オンラインショップを作る下記でのメモ。
注文完了ページの請求先住所のとこを修正したい。
テンプレートやフィルターが多々絡んでたので整理する。
まずは、注文完了ページのテンプレート。
基本ディレクトリは/plugins/woocommerce/。
/templates/checkout/thankyou.php
請求書住所の出力部分は78行目。
<?php do_action( 'woocommerce_thankyou', $order->get_id() ); ?>
アクションフック指定箇所は260行目。
/includes/wc-template-hooks.php
add_action( 'woocommerce_thankyou', 'woocommerce_order_details_table', 10 );
注文の詳細用のテンプレート出力。
/includes/wc-template-functions.php
請求住所用の出力は2549行目。
if ( ! function_exists( 'woocommerce_order_details_table' ) ) {
/**
* Displays order details in a table.
*
* @param mixed $order_id Order ID.
*/
function woocommerce_order_details_table( $order_id ) {
if ( ! $order_id ) {
return;
}
wc_get_template(
'order/order-details.php',
array(
'order_id' => $order_id,
)
);
}
}
/templates/order/order-details.php
顧客情報の表示個所は105行目。
if ( $show_customer_details ) {
wc_get_template( 'order/order-details-customer.php', array( 'order' => $order ) );
}
/templates/order/order-details-customer.php
電話とメアド以外は34行目で取得。
<address>
<?php echo wp_kses_post( $order->get_formatted_billing_address( esc_html__( 'N/A', 'woocommerce' ) ) ); ?>
<?php if ( $order->get_billing_phone() ) : ?>
<p class="woocommerce-customer-details--phone"><?php echo esc_html( $order->get_billing_phone() ); ?></p>
<?php endif; ?>
<?php if ( $order->get_billing_email() ) : ?>
<p class="woocommerce-customer-details--email"><?php echo esc_html( $order->get_billing_email() ); ?></p>
<?php endif; ?>
</address>
/includes/class-wc-order.php
データを配列にまとめてるのが897行目。
独自の項目追加をしたい場合は、
woocommerce_order_formatted_billing_address
のフィルターをフックする。
そして、表示フォーマットの指定が898行目。
/**
* Get a formatted billing address for the order.
*
* @param string $empty_content Content to show if no address is present. @since 3.3.0.
* @return string
*/
public function get_formatted_billing_address( $empty_content = '' ) {
$raw_address = apply_filters( 'woocommerce_order_formatted_billing_address', $this->get_address( 'billing' ), $this );
$address = WC()->countries->get_formatted_address( $raw_address );
/**
* Filter orders formatterd billing address.
*
* @since 3.8.0
* @param string $address Formatted billing address string.
* @param array $raw_address Raw billing address.
* @param WC_Order $order Order data. @since 3.9.0
*/
return apply_filters( 'woocommerce_order_get_formatted_billing_address', $address ? $address : $empty_content, $raw_address, $this );
}
ここからが複雑。
/includes/class-wc-countries.php
国ごとの表示フォーマット設定は554行目。
/**
* Get country address format.
*
* @param array $args Arguments.
* @param string $separator How to separate address lines. @since 3.5.0.
* @return string
*/
public function get_formatted_address( $args = array(), $separator = '<br/>' ) {
$default_args = array(
'first_name' => '',
'last_name' => '',
'company' => '',
'address_1' => '',
'address_2' => '',
'city' => '',
'state' => '',
'postcode' => '',
'country' => '',
);
$args = array_map( 'trim', wp_parse_args( $args, $default_args ) );
$state = $args['state'];
$country = $args['country'];
// Get all formats.
$formats = $this->get_address_formats();
// Get format for the address' country.
$format = ( $country && isset( $formats[ $country ] ) ) ? $formats[
その関数は482行目。
/**
* Get country address formats.
*
* These define how addresses are formatted for display in various countries.
*
* @return array
*/
public function get_address_formats() {
if ( empty( $this->address_formats ) ) {
$this->address_formats = apply_filters(
'woocommerce_localisation_address_formats',
array(
'default' => "{name}\n{company}\n{address_1}\n{address_2}\n{city}\n{state}\n{postcode}\n{country}",
'AU' => "{name}\n{company}\n{address_1}\n{address_2}\n{city} {state} {postcode}\n{country}",
'AT' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
'BE' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
'CA' => "{company}\n{name}\n{address_1}\n{address_2}\n{city} {state_code} {postcode}\n{country}",
'CH' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
'CL' => "{company}\n{name}\n{address_1}\n{address_2}\n{state}\n{postcode} {city}\n{country}",
'CN' => "{country} {postcode}\n{state}, {city}, {address_2}, {address_1}\n{company}\n{name}",
'CZ' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
'DE' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
'EE' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
'FI' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
'DK' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
'FR' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city_upper}\n{country}",
'HK' => "{company}\n{first_name} {last_name_upper}\n{address_1}\n{address_2}\n{city_upper}\n{state_upper}\n{country}",
'HU' => "{name}\n{company}\n{city}\n{address_1}\n{address_2}\n{postcode}\n{country}",
'IN' => "{company}\n{name}\n{address_1}\n{address_2}\n{city} {postcode}\n{state}, {country}",
'IS' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
'IT' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode}\n{city}\n{state_upper}\n{country}",
'JP' => "{postcode}\n{state} {city} {address_1}\n{address_2}\n{company}\n{last_name} {first_name}\n{country}",
'TW' => "{company}\n{last_name} {first_name}\n{address_1}\n{address_2}\n{state}, {city} {postcode}\n{country}",
506行目に日本語設定があるが変更したいので、
485行目にあるフィルターをフックし、
フォーマットを指定すれば解決。