概要
woocommerce_structured_data_product
フィルタは、WooCommerceの製品に関連する構造化データをカスタマイズするために使用されるフックの一つです。このフィルタを使用することにより、開発者やサイトオーナーは、商品情報をJSON-LD形式で出力し、SEOの向上やリッチスニペットの表示を促進できます。具体的には、次のような機能実装でよく使用されます。
- 商品の価格情報の修正
- 在庫状況のカスタマイズ
- 製品の評価やレビューの追加
- 商品の画像やビデオリンクの追加
- 特殊な属性やカスタムフィールドの統合
- 複数の商品タイプのサポート
構文
add_filter( 'woocommerce_structured_data_product', 'custom_function_name', 10, 2 );
パラメータ
$data
: 既存の構造化データ配列。$product
: 現在表示中のWooCommerce製品のオブジェクト。
戻り値
- 修正された構造化データ配列。
対応プラグイン/バージョン
- WooCommerce: バージョン3.0.0以降
- WordPress: バージョン4.0.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 | 〇 |
サンプルコード
以下に、woocommerce_structured_data_product
フィルタを使用したいくつかのサンプルコードを示します。
サンプル1: 商品価格のカスタマイズ
add_filter( 'woocommerce_structured_data_product', function( $data, $product ) {
$data['offers']['price'] = 29.99; // カスタム価格に変更
return $data;
});
このコードは、製品の価格を29.99にカスタマイズしています。
サンプル2: 商品の在庫状況を追加
add_filter( 'woocommerce_structured_data_product', function( $data, $product ) {
$data['offers']['itemCondition'] = 'http://schema.org/NewCondition'; // 商品の状態を新しい状態に設定
return $data;
});
このサンプルでは、製品の状態を「新しい状態」に設定しています。
サンプル3: 評価を追加
add_filter( 'woocommerce_structured_data_product', function( $data, $product ) {
$data['aggregateRating'] = [
'@type' => 'AggregateRating',
'ratingValue' => '4.5',
'bestRating' => '5',
'ratingCount' => '200',
];
return $data;
});
このコードは、製品に4.5の評価を設定し、評価の件数を200件としています。
サンプル4: 特殊属性の追加
add_filter( 'woocommerce_structured_data_product', function( $data, $product ) {
$data['additionalType'] = 'http://www.product-schema.org/CustomType'; // カスタム属性を追加
return $data;
});
このサンプルでは、製品にカスタム属性を追加しています。
サンプル5: 商品リンクのカスタマイズ
add_filter( 'woocommerce_structured_data_product', function( $data, $product ) {
$data['url'] = 'https://custom-url.com/product'; // 商品リンクをカスタマイズ
return $data;
});
このコードでは、製品のリンクを指定されたURLに変更しています。
引用元のページは、WooCommerceの公式文書やGitHubのリポジトリで見つけることができます。具体的なURLは公開されていないため、最新の情報は公式ドキュメントを参照してください。