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

概要

フィルタ woocommerce_cart_item_is_purchasable は、WooCommerce のカート内の商品が購入可能かどうかを判定するために使われるフックです。このフィルタを使うことで、特定の条件に基づいて商品の購入可能性を動的に変更することができます。具体的には、次のような機能を実装する際に利用されることが多いです。

  1. 特定のユーザーグループに対して商品の購入を制限
  2. 在庫がない商品をカートから非表示にする
  3. セール中の商品の購入を制限する
  4. 特定の地域に基づいて商品の購入を制限
  5. 商品ごとに異なる購入条件を設定
  6. サブスクリプション商品が特定の条件下でのみ購入可能にする

構文

add_filter('woocommerce_cart_item_is_purchasable', 'custom_purchasable_filter', 10, 2);

パラメータ

  • $purchasable (bool): 商品が購入可能かどうかのフラグ。
  • $cart_item (array): カート内の商品情報。

戻り値

  • bool: 商品が購入可能であれば true、そうでなければ false。

使用可能なプラグインとバージョン

  • WooCommerce: 2.0.0 以降
  • WordPress: 3.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_cart_item_is_purchasable', 'restrict_purchase_based_on_user_role', 10, 2);
    function restrict_purchase_based_on_user_role($purchasable, $cart_item) {
       if (!current_user_can('customer')) {
           return false; // すべての非顧客ユーザーの購入を制限
       }
       return $purchasable;
    }
    

    このサンプルコードは、顧客以外のユーザーがカート内の商品を購入できないように制限します。

  2. 在庫がない商品をカートから非表示にする

    add_filter('woocommerce_cart_item_is_purchasable', 'hide_out_of_stock_products', 10, 2);
    function hide_out_of_stock_products($purchasable, $cart_item) {
       $product = $cart_item['data'];
       if (!$product->is_in_stock()) {
           return false; // 在庫がない場合は購入不可
       }
       return $purchasable;
    }
    

    このコードは、在庫がない商品をカート内で購入できないようにします。

  3. 特定の地域に基づいて商品の購入を制限

    add_filter('woocommerce_cart_item_is_purchasable', 'restrict_purchase_based_on_location', 10, 2);
    function restrict_purchase_based_on_location($purchasable, $cart_item) {
       $user_country = WC()->customer->get_shipping_country();
       if ($user_country === 'JP') { // 日本の場合
           return $purchasable; // 購入可能
       }
       return false; // 日本以外では購入不可
    }
    

    このサンプルコードは、日本に住むユーザーのみが商品を購入できるように制限します。

  4. セール中の商品の購入を制限する

    add_filter('woocommerce_cart_item_is_purchasable', 'restrict_sale_items', 10, 2);
    function restrict_sale_items($purchasable, $cart_item) {
       $product = $cart_item['data'];
       if ($product->is_on_sale()) {
           return false; // セール中の商品は購入不可能
       }
       return $purchasable;
    }
    

    このコードは、セール中の商品が購入できないように設定します。

  5. サブスクリプション商品が特定の条件下でのみ購入可能にする

    add_filter('woocommerce_cart_item_is_purchasable', 'allow_subscription_purchase_only_under_conditions', 10, 2);
    function allow_subscription_purchase_only_under_conditions($purchasable, $cart_item) {
       $product = $cart_item['data'];
       if ($product->is_type('subscription') && !current_user_can('subscriber')) {
           return false; // サブスクリプション商品は、サブスクライバー以外には購入不可
       }
       return $purchasable;
    }
    

    このサンプルは、サブスクリプション商品をサブスクライバーにのみ購入可能とします。

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


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