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

概要

woocommerce_coupon_get_apply_quantity フィルタは、WooCommerce におけるクーポンの適用数量を変更するためのフックです。このフィルタを使用することで、特定の条件に基づいてクーポンの適用可能数量を制御することができます。一般的には、以下のような機能を実装する際に使用されます。

  1. クーポンの適用回数を制限する
  2. 特定のユーザーグループに対するクーポンの適用設定
  3. クーポンの適用可能な商品カテゴリーの制限
  4. ショッピングカートの内容に基づくクーポンの条件設定
  5. 定期購買に適用するクーポンの制御
  6. 特定のプロモーションやイベントに応じたクーポンの適用制限

構文

add_filter('woocommerce_coupon_get_apply_quantity', 'custom_coupon_apply_quantity', 10, 4);

パラメータ

  • $apply_quantity : クーポンの適用可能数量(整数)
  • $coupon : クーポンオブジェクト
  • $cart : カートオブジェクト
  • $user : ユーザーオブジェクト

戻り値

フィルタの戻り値は、変更後のクーポンの適用数量(整数)です。

使用可能なバージョン

  • WooCommerce: 3.0 以降
  • WordPress: 4.0 以降

サンプルコード

サンプルコード1: クーポンの適用回数を制限

add_filter('woocommerce_coupon_get_apply_quantity', 'limit_coupon_usage', 10, 4);
function limit_coupon_usage($apply_quantity, $coupon, $cart, $user) {
    return 1; // クーポンを一回のみ適用可能にする
}

こちらのコードは、指定したクーポンを一度だけ適用可能に制限します。

サンプルコード2: ロールに基づいた適用数量の変更

add_filter('woocommerce_coupon_get_apply_quantity', 'role_based_coupon_quantity', 10, 4);
function role_based_coupon_quantity($apply_quantity, $coupon, $cart, $user) {
    if (in_array('premium_members', (array) $user->roles)) {
        return 2; // プレミアムメンバーには2回適用可能
    }
    return $apply_quantity; // その他のユーザーには既定の数量を使用
}

このコードは、ユーザーのロールが「premium_members」の場合、クーポンを二回適用できるようにします。

サンプルコード3: カテゴリーに基づいたクーポン条件

add_filter('woocommerce_coupon_get_apply_quantity', 'category_based_coupon_quantity', 10, 4);
function category_based_coupon_quantity($apply_quantity, $coupon, $cart, $user) {
    foreach ($cart->get_cart() as $cart_item) {
        $product_id = $cart_item['product_id'];
        $terms = get_the_terms($product_id, 'product_cat');
        if ($terms && !is_wp_error($terms) && in_array('special-category', wp_list_pluck($terms, 'slug'))) {
            return 3; // 特定のカテゴリーの商品がある場合、3回適用可能
        }
    }
    return $apply_quantity;
}

指定したカテゴリーの商品がカートに含まれている場合に、クーポンの適用可能数量を増やす例です。

サンプルコード4: 定期購買に対する適用制限

add_filter('woocommerce_coupon_get_apply_quantity', 'subscription_coupon_quantity_limit', 10, 4);
function subscription_coupon_quantity_limit($apply_quantity, $coupon, $cart, $user) {
    if (WC_Subscriptions_Product::is_subscription($cart->get_cart())) {
        return 1; // 定期購買には1回のみ適用
    }
    return $apply_quantity;
}

このコードは、カート内に定期購入アイテムが含まれている場合、クーポンの適用を一度に制限します。

サンプルコード5: 特定プロモーション期間内のクーポン制御

add_filter('woocommerce_coupon_get_apply_quantity', 'promotional_coupon_quantity', 10, 4);
function promotional_coupon_quantity($apply_quantity, $coupon, $cart, $user) {
    $promo_start = '2023-11-01';
    $promo_end = '2023-11-30';
    if (current_time('Y-m-d') >= $promo_start && current_time('Y-m-d') <= $promo_end) {
        return 5; // プロモーション期間中は5回適用可能
    }
    return $apply_quantity;
}

特定のプロモーション期間中にクーポンの適用回数を増やす例です。

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

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

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


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