プラグインWooCommerceのwoocommerce_shipping_method_descriptionフィルタの使用方法・解説

概要

woocommerce_shipping_method_description フィルタは、WooCommerceで使用される配送方法の説明をカスタマイズするためのフックです。このフィルタは、様々な目的で使用されることが多く、以下のような機能を実装する際に特によく使われます。

  1. 配送方法に特定の条件を追加する。
  2. 客の地域に応じた配送方法の説明を変更する。
  3. 特定のプロモーションを配送方法に関連付けた説明を追加する。
  4. ユーザーのカスタムフィールドに基づいて配送方法を調整する。
  5. 異なる通貨や国における送料情報の表示を変更する。
  6. 配送方法の選択によって異なる注意書きを表示する。

このフィルタは、WooCommerceのさまざまなバージョンで利用可能で、基本的にはWordPress 4.0以降のバージョンでも使用可能です。

構文

add_filter( 'woocommerce_shipping_method_description', 'your_function_name', 10, 2 );

パラメータ

  • $method_description: 現在の配送方法の説明。
  • $method: 配送方法のインスタンス(オブジェクト)。

戻り値

  • 配送方法の新しい説明(文字列)。

サンプルコード

サンプル1: 特定の地域に基づく説明の変更

add_filter( 'woocommerce_shipping_method_description', 'custom_shipping_description_by_region', 10, 2 );

function custom_shipping_description_by_region( $method_description, $method ) {
    if ( WC()->customer->get_shipping_country() === 'JP' ) {
        $method_description .= ' (日本国内向けの配送料金が適用されています。)';
    }
    return $method_description;
}

このサンプルコードは、日本の顧客に対して配送方法の説明に特別なメッセージを追加します。

サンプル2: プロモーション情報の追加

add_filter( 'woocommerce_shipping_method_description', 'add_promotion_info_to_description', 10, 2 );

function add_promotion_info_to_description( $method_description, $method ) {
    $method_description .= ' 今なら全品送料無料キャンペーン中!';
    return $method_description;
}

このコードは、すべての配送方法にプロモーションメッセージを追加します。

サンプル3: ユーザーのカスタムフィールドを使用

add_filter( 'woocommerce_shipping_method_description', 'custom_description_based_on_user_meta', 10, 2 );

function custom_description_based_on_user_meta( $method_description, $method ) {
    $user_id = get_current_user_id();
    $custom_info = get_user_meta( $user_id, 'shipping_info', true );

    if ( ! empty( $custom_info ) ) {
        $method_description .= ' ' . esc_html( $custom_info );
    }
    return $method_description;
}

このサンプルは、ユーザーのカスタムフィールドに基づいて配送方法の説明を調整します。

サンプル4: 特定の配送方法にだけ情報を追加

add_filter( 'woocommerce_shipping_method_description', 'specific_method_description', 10, 2 );

function specific_method_description( $method_description, $method ) {
    if ( 'flat_rate' === $method->id ) {
        $method_description .= ' (フラットレート配送をお選びの際、追跡番号が提供されます。)';
    }
    return $method_description;
}

このコードは、特定の配送方法(フラットレート)に追加の情報を提供します。

サンプル5: 異なる言語に基づく説明の変更

add_filter( 'woocommerce_shipping_method_description', 'change_description_based_on_language', 10, 2 );

function change_description_based_on_language( $method_description, $method ) {
    if ( get_locale() === 'fr_FR' ) {
        $method_description .= ' (Nous offrons une expédition gratuite sur toutes les commandes!)';
    }
    return $method_description;
}

このサンプルコードは、フランス語のロケールを使用しているユーザーに対して、異なる配送説明を表示します。

この関数のアクションでの使用可能性

アクション 使用可能性
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

この関数について質問する


上の計算式の答えを入力してください