概要
woocommerce_shipping_chosen_method
フィルタは、WooCommerce において、選択された配送方法をカスタマイズするために使用されるフックです。このフィルタを利用することで、顧客がチェックアウト時に選択した配送方法を変更したり、特定の条件に基づいて異なる配送方法を表示することができます。以下は、woocommerce_shipping_chosen_method
フィルタを使用する際によく使われる機能の例です。
- 特定の地域に基づいた配送方法のフィルタリング
- 特定のカートアイテムに基づく配送方法の調整
- 顧客のユーザーグループに応じた配送オプションの表示
- 特定のプロモーションに基づく送料の変更
- 配送方法の選択肢にカスタムメッセージを追加
- 配送方法や手数料の条件付き表示
構文
フィルタフックは次のように使用されます:
add_filter( 'woocommerce_shipping_chosen_method', 'custom_function_name', 10, 2 );
パラメータ
$method
(string) – 選択された配送方法の名前。$available_methods
(array) – 利用可能な配送方法の配列。
戻り値
フィルタによって変更された配送方法 (string)。
使用可能なプラグインバージョン
- WooCommerce バージョン: 3.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_shipping_chosen_method', 'change_shipping_method_based_on_location', 10, 2 );
function change_shipping_method_based_on_location( $method, $available_methods ) {
if ( WC()->customer->get_shipping_country() === 'JP' ) {
if ( isset( $available_methods['flat_rate:1'] ) ) {
return 'flat_rate:1'; // 日本の顧客に特定の配送方法を強制
}
}
return $method;
}
出所: https://woocommerce.com/
サンプルコード 2: 特定のカートアイテムに基づく配送方法の調整
このコードは、カート内のアイテム数に応じて選択された配送方法を変更する例です。
add_filter( 'woocommerce_shipping_chosen_method', 'adjust_shipping_method_based_on_cart_items', 10, 2 );
function adjust_shipping_method_based_on_cart_items( $method, $available_methods ) {
if ( WC()->cart->get_cart_contents_count() > 2 ) {
return 'free_shipping:1'; // 2つ以上のアイテムがある場合は無料配送を適用
}
return $method;
}
出所: https://woocommerce.com/
サンプルコード 3: ユーザーグループに基づく配送オプションを表示
このコードは、顧客がログインしているかどうかで異なる配送方法を表示します。
add_filter( 'woocommerce_shipping_chosen_method', 'show_shipping_option_based_on_user_role', 10, 2 );
function show_shipping_option_based_on_user_role( $method, $available_methods ) {
if ( is_user_logged_in() && current_user_can( 'wholesale_customer' ) ) {
return 'flat_rate:2'; // 卸売顧客向けの特定の配送方法
}
return $method;
}
出所: https://woocommerce.com/
サンプルコード 4: 特定のプロモーションに基づく配送方法の変更
このコードは、特定のプロモーションコードが適用されている場合に配送方法を変更します。
add_filter( 'woocommerce_shipping_chosen_method', 'change_shipping_method_based_on_promo_code', 10, 2 );
function change_shipping_method_based_on_promo_code( $method, $available_methods ) {
if ( WC()->session->get('promo_code') === 'FREESHIP' ) {
return 'free_shipping:1'; // プロモコードがフリーシップの場合に無料配送を選択
}
return $method;
}
出所: https://woocommerce.com/
サンプルコード 5: 配送方法の選択肢にカスタムメッセージを追加
このコードは、選択された配送方法にカスタムメッセージを追加する例です。
add_filter( 'woocommerce_shipping_chosen_method', 'add_custom_message_to_shipping_method', 10, 2 );
function add_custom_message_to_shipping_method( $method, $available_methods ) {
if ( $method === 'flat_rate:1' ) {
wc_add_notice( 'この配送方法は2-3日で到着します。', 'notice' ); // 特定の配送方法にメッセージを追加
}
return $method;
}
出所: https://woocommerce.com/