概要
woocommerce_get_cancel_order_url
フィルタは、WooCommerceでの注文キャンセルURLをカスタマイズするために使用されます。このフィルタを通じて、開発者はキャンセルリンクのURLに変更を加えたり、条件に応じたリダイレクト先のURLを指定することができます。主に以下のようなケースで使用されます。
- 特定の条件に基づいてキャンセルリンクを変更する場合。
- 特定のユーザーグループに対して異なるキャンセルURLを表示する場合。
- 言語翻訳のために異なるURLを設定する場合。
- 独自のキャンセルページを作成する際にURLを指定する場合。
- クーポンの適用条件に応じてURLを変更する場合。
- 特定の商品のみキャンセル可能にするためにURLを制御する場合。
構文
add_filter('woocommerce_get_cancel_order_url', 'your_custom_function', 10, 2);
パラメータ
$cancel_url
(string): デフォルトのキャンセルURL。$order
(WC_Order): キャンセルする対象の注文オブジェクト。
戻り値
- 変更されたキャンセルURL(string)。
使用可能なWooCommerceのバージョン
- WooCommerce 2.0.0以降
使用可能なWordPressのバージョン
- 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_get_cancel_order_url', 'custom_cancel_url', 10, 2);
function custom_cancel_url($cancel_url, $order) {
// 特定の注文IDに基づいて異なるURLを返す
if ($order->get_id() === 123) {
return 'https://example.com/custom-cancel-page';
}
return $cancel_url; // デフォルトのURLを返す
}
このコードは、特定の注文ID(123)に対してカスタムキャンセルURLにリダイレクトします。
サンプルコード 2
add_filter('woocommerce_get_cancel_order_url', 'dynamic_cancel_url', 10, 2);
function dynamic_cancel_url($cancel_url, $order) {
// ユーザーの役割に応じてキャンセルURLを変更
if (current_user_can('administrator')) {
return 'https://example.com/admin-cancel';
}
return $cancel_url; // 他のユーザーにはデフォルトのURLを使用
}
このコードは、管理者ユーザーに特別なキャンセルURLを提供します。
サンプルコード 3
add_filter('woocommerce_get_cancel_order_url', 'localized_cancel_url', 10, 2);
function localized_cancel_url($cancel_url, $order) {
// ウェブサイトの言語に応じてキャンセルURLを変更
if (get_locale() === 'ja') {
return 'https://example.com/ja/cancel';
}
return $cancel_url; // デフォルトのURL
}
このコードは、サイトが日本語の場合に特定のキャンセルURLを使用します。
サンプルコード 4
add_filter('woocommerce_get_cancel_order_url', 'coupon_based_cancel_url', 10, 2);
function coupon_based_cancel_url($cancel_url, $order) {
// クーポンコードが適用されている場合にキャンセルURLを変更
if ($order->get_coupon_codes()) {
return 'https://example.com/coupon-cancel';
}
return $cancel_url; // デフォルトのURL
}
このコードは、クーポンが適用されている注文に対して特別なキャンセルURLを提供します。
サンプルコード 5
add_filter('woocommerce_get_cancel_order_url', 'conditional_cancel_url', 10, 2);
function conditional_cancel_url($cancel_url, $order) {
// 商品に特定のメタデータが含まれている場合、キャンセルURLを変更
$items = $order->get_items();
foreach ($items as $item) {
if ($item->get_meta('special_product')) {
return 'https://example.com/special-cancel';
}
}
return $cancel_url; // デフォルトのURL
}
このコードは、特定のメタデータを持つ商品が含まれている場合に異なるキャンセルURLを提供します。