プラグインWooCommerceのwoocommerce_coupon_get_items_to_validateフックの使用方法・解説

概要

woocommerce_coupon_get_items_to_validateフックは、WooCommerceでクーポンの適用可能なアイテムを検証する際に使用されるフックです。主に、以下のようなシナリオで利用されます:

  1. 特定の商品のみがクーポンの適用対象になるよう制御する
  2. カート内の数量や金額に基づいてクーポンの有効性を確認する
  3. 商品のカテゴリやタグに合わせてクーポンの適用範囲を制限する
  4. クーポンの重複適用を防ぐための条件を設定する
  5. 特定条件下でのクーポンの適用を追加的に処理する
  6. その他のプラグインとの互換性を保つためにカスタマイズを行う

構文

add_filter( 'woocommerce_coupon_get_items_to_validate', 'custom_coupon_items_validation', 10, 3 );

パラメータ

  • $items_to_validate (配列): クーポンが適用されるアイテムの配列
  • $coupon (オブジェクト): 使用されるクーポンのオブジェクト
  • $cart (オブジェクト): 現在のカートのオブジェクト

戻り値

  • 配列: 検証に合格したアイテムの配列

対応バージョン

  • 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

サンプルコード

サンプルコード 1

add_filter( 'woocommerce_coupon_get_items_to_validate', 'limit_coupon_to_specific_category', 10, 3 );

function limit_coupon_to_specific_category( $items_to_validate, $coupon, $cart ) {
    $allowed_category = 'special-category';

    foreach ( $items_to_validate as $key => $item ) {
        if ( ! has_term( $allowed_category, 'product_cat', $item['product_id'] ) ) {
            unset( $items_to_validate[ $key ] );
        }
    }

    return $items_to_validate;
}
  • 説明: 特定のカテゴリーの商品に対してのみクーポンが適用されるようにします。

サンプルコード 2

add_filter( 'woocommerce_coupon_get_items_to_validate', 'validate_coupon_by_cart_total', 10, 3 );

function validate_coupon_by_cart_total( $items_to_validate, $coupon, $cart ) {
    if ( $cart->subtotal < 50 ) {
        return array(); // カート合計が50未満の場合、適用されない
    }

    return $items_to_validate;
}
  • 説明: カートの合計が50未満の場合、クーポンの適用を無効にします。

サンプルコード 3

add_filter( 'woocommerce_coupon_get_items_to_validate', 'apply_coupon_to_quantity_limit', 10, 3 );

function apply_coupon_to_quantity_limit( $items_to_validate, $coupon, $cart ) {
    foreach ( $items_to_validate as $key => $item ) {
        if ( $item['quantity'] > 3 ) {
            unset( $items_to_validate[ $key ] ); // 数量が3を超える場合はクーポンを適用しない
        }
    }
    return $items_to_validate;
}
  • 説明: 特定のアイテムが3つ以上の場合、クーポンの適用を無効にします。

サンプルコード 4

add_filter( 'woocommerce_coupon_get_items_to_validate', 'restrict_coupon_by_product_tag', 10, 3 );

function restrict_coupon_by_product_tag( $items_to_validate, $coupon, $cart ) {
    $restricted_tag = 'no-discount';

    foreach ( $items_to_validate as $key => $item ) {
        if ( has_term( $restricted_tag, 'product_tag', $item['product_id'] ) ) {
            unset( $items_to_validate[ $key ] ); // タグがno-discountの場合、適用しない
        }
    }
    return $items_to_validate;
}
  • 説明: no-discountタグが付与された商品にはクーポンを適用しません。

サンプルコード 5

add_filter( 'woocommerce_coupon_get_items_to_validate', 'block_coupon_with_valid_date', 10, 3 );

function block_coupon_with_valid_date( $items_to_validate, $coupon, $cart ) {
    $current_date = current_time( 'Y-m-d' );
    $expiry_date = $coupon->date_expires ? date( 'Y-m-d', strtotime( $coupon->date_expires ) ) : null;

    if ( $expiry_date && $current_date > $expiry_date ) {
        return array(); // 有効期限切れの場合、適用されない
    }

    return $items_to_validate;
}
  • 説明: クーポンの有効期限が切れている場合は、クーポンの適用を無効にします。

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


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