概要
woocommerce_order_tracking_status
は、WooCommerce における注文の追跡ステータスをカスタマイズするためのフィルターフックです。このフィルターを使用することで、特定の条件に基づいてカスタムのステータスを追加したり、既存のステータスを変更したりすることができます。このフィルタは、以下のような機能を実装する際によく使われます。
- 追加の配送ステータスを登録する。
- 特定の条件によりステータスの表示を変更する。
- 通知メールの内容にステータスを反映させる。
- 注文のステータスに基づくカスタムメッセージの表示。
- 外部APIとの連携による自動ステータス更新。
- 管理画面でのカスタムフィルターを追加する。
構文
add_filter( 'woocommerce_order_tracking_status', 'your_custom_function', 10, 2 );
パラメータ
$status
:現在の注文の追跡ステータス。$order
:対象のWC_Order
オブジェクト。
戻り値
フィルターの戻り値は、修正されたステータスの文字列です。
使用可能バージョン
- WooCommerce バージョン:3.0.0以上
- WordPress バージョン:4.0以上
この関数のアクションでの使用可能性
アクション | 使用可能性 |
---|---|
mu_plugin_loaded | |
registered_post_type | |
plugins_loaded | |
wp_roles_init | |
setup_theme | |
after_setup_theme | |
set_current_user | |
init | |
register_sidebar | |
wp_loaded | |
send_headers | |
parse_query | |
pre_get_posts | |
wp | |
template_redirect | |
get_header | |
wp_head |
サンプルコード
サンプルコード1
add_filter( 'woocommerce_order_tracking_status', 'custom_order_tracking_status', 10, 2 );
function custom_order_tracking_status( $status, $order ) {
if ( $order->get_status() == 'completed' ) {
return 'Your order has been shipped!';
}
return $status;
}
このコードは、完了した注文に対してカスタムの追跡ステータス「Your order has been shipped!」を設定します。
引用元: https://woocommerce.com/
サンプルコード2
add_filter( 'woocommerce_order_tracking_status', 'add_custom_tracking_status', 10, 2 );
function add_custom_tracking_status( $status, $order ) {
if ( $order->get_meta( 'custom_status' ) ) {
return $order->get_meta( 'custom_status' );
}
return $status;
}
このコードは、注文メタデータに保存されたカスタムステータスをチェックし、存在する場合はそれを返します。
引用元: https://woocommerce.com/
サンプルコード3
add_filter( 'woocommerce_order_tracking_status', 'modify_tracking_status_for_supplier', 10, 2 );
function modify_tracking_status_for_supplier( $status, $order ) {
if ( 'Supplier 1' === $order->get_meta('supplier_name') ) {
return 'Supplier 1 processing';
}
return $status;
}
このコードは、特定のサプライヤーによる注文に対して異なる追跡ステータスを設定します。
引用元: https://woocommerce.com/
サンプルコード4
add_filter( 'woocommerce_order_tracking_status', 'replace_tracking_status_based_on_shipping_method', 10, 2 );
function replace_tracking_status_based_on_shipping_method( $status, $order ) {
if ( 'free_shipping' === $order->get_shipping_method() ) {
return 'Free shipping - your order will take longer';
}
return $status;
}
このコードは、配送方法が「無料配送」の場合に追跡ステータスを変更します。
引用元: https://woocommerce.com/
サンプルコード5
add_filter( 'woocommerce_order_tracking_status', 'add_custom_message_for_banned_products', 10, 2 );
function add_custom_message_for_banned_products( $status, $order ) {
if ( in_array( 'banned_product_id', $order->get_items() ) ) {
return 'Order contains banned products - review required.';
}
return $status;
}
このコードは、禁止された製品を含む注文に対して特別なメッセージを設定します。
引用元: https://woocommerce.com/