0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WooCommerceで特定の商品ページにいるかを判定するカスタム関数

Posted at

WooCommerceを使ったECサイト開発で、「特定の商品ページでのみ表示したいコンテンツがある」という場面はよくあります。例えば:

  • 特定商品のみに表示するバナー
  • 商品固有の JavaScript コード
  • 限定商品への特別なスタイリング

WordPress にはis_product()という関数がありますが、これは「商品ページかどうか」しか分からず、「どの商品のページか」までは判定できません。

そこで今回は、特定の商品 ID のページにいるかどうかを判定する関数を作成してみました。

実装コード

<?php
/**
 * 特定の商品 ID のページにいるかどうかを判定する関数
 * 
 * @param int $product_id 判定したい商品 ID(省略可能)
 * @return bool 指定した商品 IDのページにいる場合はtrue、そうでなければfalse
 */
function is_wc_product_page( $product_id = null ) {
    // WooCommerceがアクティブでない場合はfalseを返す
    if ( ! class_exists( 'WooCommerce' ) ) {
        return false;
    }
    
    // 商品ページでない場合はfalseを返す
    if ( ! is_product() ) {
        return false;
    }
    
    // 商品IDが指定されていない場合は、単純に商品ページかどうかを返す
    if ( null === $product_id ) {
        return true;
    }
    
    // 現在の商品IDを取得
    global $post;
    $current_product_id = get_the_ID();
    
    // バリエーション商品の場合は親商品IDを取得
    if ( $post && 'product_variation' === $post->post_type ) {
        $current_product_id = wp_get_post_parent_id( $current_product_id );
    }
    
    // 指定された商品IDと現在の商品IDを比較
    return (int) $product_id === (int) $current_product_id;
}

関数の特徴

1. エラーハンドリング

// WooCommerceがアクティブでない場合の対応
if ( ! class_exists( 'WooCommerce' ) ) {
    return false;
}

// 商品ページでない場合の早期リターン
if ( ! is_product() ) {
    return false;
}

WooCommerceが無効化されている場合や、商品ページ以外でのエラーを防ぎます。

2. 柔軟な使い方

// 商品IDが指定されていない場合は、単純に商品ページかどうかを返す
if ( $product_id === null ) {
    return true;
}

引数を省略すると、既存のis_product()と同じように動作します。

3. バリエーション商品への対応

// バリエーション商品の場合は親商品IDを取得
if ( $post && $post->post_type === 'product_variation' ) {
    $current_product_id = wp_get_post_parent_id( $current_product_id );
}

バリエーション商品(色違い、サイズ違いなど)の場合も、親商品のIDで正しく判定できます。

使用例

基本的な使い方

// 商品ID 123のページにいるかチェック
if ( is_wc_product_page( 123 ) ) {
    echo '<div class="special-banner">限定セール実施中!</div>';
}

商品IDを指定しない場合

// 単純に商品ページかどうかチェック
if ( is_wc_product_page() ) {
    echo '商品ページにいます';
}

複数の商品IDをまとめてチェック

// 特定の商品群でのみ表示したい場合
$special_products = array( 123, 456, 789 );
foreach ( $special_products as $product_id ) {
    if ( is_wc_product_page( $product_id ) ) {
        echo '<div class="premium-badge">プレミアム商品</div>';
        break;
    }
}

フックと組み合わせた実用例

// 特定商品のみでCSSを読み込む
add_action( 'wp_head', function() {
    if ( is_wc_product_page( 123 ) ) {
        echo '<link rel="stylesheet" href="/path/to/special-product.css">';
    }
});

// 特定商品のみでJavaScriptを実行
add_action( 'wp_footer', function() {
    if ( is_wc_product_page( 456 ) ) {
        echo '<script>console.log("Special product page loaded!");</script>';
    }
});

まとめ

このis_wc_product_page()関数を使うことで:

  • 特定商品ページでのみ表示したいコンテンツの制御が簡単に
  • バリエーション商品にも対応した堅牢な判定
  • エラーハンドリングにより安全な動作を保証

WooCommerce サイトのカスタマイズでぜひ活用してみてください!

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?