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

概要

woocommerce_product_variation_title_attributes_separator フィルタは、WooCommerce の商品バリエーションにおいて、タイトルの属性をセパレートする際の文字列を変更するために用いられます。このフィルタは、カスタマイズされたバリエーションタイトルの表示を実現するために非常に便利です。具体的には以下のような機能でよく使用されます。

  1. バリエーションタイトルにカスタムセパレーターを追加
  2. 商品のバリエーション情報を視覚的に分かりやすくする
  3. 複数の属性を持つ商品でも見やすいタイトルを実現
  4. SEOの観点からタイトルを調整する
  5. 特定の条件に基づいてセパレーターを変更する
  6. 国や地域ごとに異なる表示ルールに対応する

構文

add_filter( 'woocommerce_product_variation_title_attributes_separator', 'custom_variation_title_separator' );

パラメータ

  • separator: デフォルトのセパレーター文字列(例:-

戻り値

カスタマイズされたセパレーター文字列。

使用可能なプラグインバージョン

WooCommerce 3.0 以降

使用可能な WordPress バージョン

WordPress 4.0 以降

サンプルコード

サンプルコード 1: デフォルトセパレーターの変更

add_filter( 'woocommerce_product_variation_title_attributes_separator', 'custom_variation_title_separator' );

function custom_variation_title_separator( $separator ) {
    return ' / '; // セパレーターをスラッシュに変更
}

このコードは、商品バリエーションタイトルのデフォルトのセパレーターをスラッシュ (/) に変更します。

サンプルコード 2: コンディションによるセパレーターの変更

add_filter( 'woocommerce_product_variation_title_attributes_separator', 'conditional_variation_title_separator' );

function conditional_variation_title_separator( $separator ) {
    if ( is_product() ) {
        return '|'; // 商品ページではパイプに変更
    }
    return $separator;
}

このコードは、商品ページで表示されるバリエーションタイトルのセパレーターをパイプ (|) に変更します。

サンプルコード 3: セパレーターのカスタマイズ

add_filter( 'woocommerce_product_variation_title_attributes_separator', 'custom_separator_with_text' );

function custom_separator_with_text( $separator ) {
    return ' - ' . __( 'Variation', 'text-domain' ); // 変化に関するテキストを付加
}

このコードは、バリエーションタイトルに「Variation」というテキストを付加したセパレーターを設定します。

サンプルコード 4: プラグインによる様々なセパレーターの提供

add_filter( 'woocommerce_product_variation_title_attributes_separator', 'dynamic_variation_separator' );

function dynamic_variation_separator( $separator ) {
    return ( date('N') % 2 ) ? '; ' : ', '; // 曜日に基づいてセパレーターを変更
}

このコードは、曜日によって異なるセパレーターを使用することで、動的な表示を実現します。

サンプルコード 5: 特定のカテゴリでのセパレーター変更

add_filter( 'woocommerce_product_variation_title_attributes_separator', 'category_based_separator' );

function category_based_separator( $separator ) {
    if ( has_term( 'special-category', 'product_cat' ) ) {
        return ' — '; // 特定カテゴリではダッシュのセパレーター
    }
    return $separator;
}

このコードは、特定のカテゴリーに属する商品のバリエーションタイトルのセパレーターを変更します。

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

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

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


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