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

概要

woocommerce_get_variation_regular_price フィルタは、WooCommerceのバリエーション商品の通常価格を取得する際に使用されるフックです。このフィルタを使用することで、バリエーション商品の価格を動的に変更したり、追加のロジックを実装することができます。具体的には以下のような機能を実装する際によく使われます。

  1. ロイヤリティプログラムに基づく価格の調整
  2. セールやキャンペーンによる価格変更
  3. 特定のユーザーグループに対する特別価格の適用
  4. 外部データベースから取得した価格情報の統合
  5. 商品の在庫状況に応じた価格の自動更新
  6. 地域に応じた価格設定の適用

構文

add_filter('woocommerce_get_variation_regular_price', 'custom_function_name', 10, 3);

パラメータ

  • price (float): 変更前の通常価格。
  • product (WC_Product): 現在の商品のWC_Productオブジェクト。
  • variation (WC_Product_Variation): 現在のバリエーション商品のWC_Product_Variationオブジェクト。

戻り値

このフィルタは変更された通常価格(float)を返します。

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

  • WooCommerce: 3.0以降
  • WordPress: 4.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: ロイヤリティプログラムによる価格調整

このコードは特定のユーザーに対してロイヤリティプログラムを適用し、価格を10%割引します。

add_filter('woocommerce_get_variation_regular_price', 'adjust_price_for_loyalty_program', 10, 3);
function adjust_price_for_loyalty_program($price, $product, $variation) {
    if (is_user_logged_in() && user_can(wp_get_current_user(), 'loyal_customer')) {
        return $price * 0.9; // 10%引き
    }
    return $price;
}

(出典: WooCommerceのフックドキュメント)

サンプルコード2: セール時の価格変更

このコードはセール時に特別割引を適用します。

add_filter('woocommerce_get_variation_regular_price', 'apply_sale_discount', 10, 3);
function apply_sale_discount($price, $product, $variation) {
    if ($variation->is_on_sale()) {
        return $price * 0.85; // 15%引き
    }
    return $price;
}

(出典: WooCommerceのフックドキュメント)

サンプルコード3: 地域に応じた価格設定

このコードは異なる地域に対する異なる価格を設定します。

add_filter('woocommerce_get_variation_regular_price', 'adjust_price_for_region', 10, 3);
function adjust_price_for_region($price, $product, $variation) {
    $user_region = get_user_meta(get_current_user_id(), 'region', true);
    if ($user_region === 'JP') {
        return $price * 1.1; // 日本の場合、10%追加
    }
    return $price;
}

(出典: WooCommerceのフックドキュメント)

サンプルコード4: 在庫状況に応じて価格を自動更新

このコードは在庫が少ない商品に対して価格を変更します。

add_filter('woocommerce_get_variation_regular_price', 'update_price_based_on_stock', 10, 3);
function update_price_based_on_stock($price, $product, $variation) {
    if ($variation->get_stock_quantity() < 5) {
        return $price * 1.2; // 在庫が5未満の場合20%引き上げ
    }
    return $price;
}

(出典: WooCommerceのフックドキュメント)

サンプルコード5: 特定のユーザーに対する特別価格の適用

このコードは特定のメールドメインを持つユーザーに特別価格を設定します。

add_filter('woocommerce_get_variation_regular_price', 'special_price_for_specific_email_domain', 10, 3);
function special_price_for_specific_email_domain($price, $product, $variation) {
    $user_email = wp_get_current_user()->user_email;
    if (strpos($user_email, '@example.com') !== false) {
        return $price * 0.85; // 特定のドメインに対して15%引き
    }
    return $price;
}

(出典: WooCommerceのフックドキュメント)

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


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