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

概要

woocommerce_shipping_show_shipping_calculator フィルタは、WooCommerceのカートやチェックアウトページにおいて、送料計算機の表示を制御するために使用されます。このフィルタを利用することで、特定の条件に基づいて送料計算機を表示させたり、非表示にすることができます。具体的には、次のような機能を実装する際によく使われます。

  1. 特定のユーザーにのみ送料計算機を表示する。
  2. 商品のカテゴリに応じて送料計算機を制御する。
  3. 送料計算機を特定のページで非表示にする。
  4. 地域や国ごとに表示を切り替える。
  5. ログイン状態に基づいて送料計算機を表示/非表示にする。
  6. クーポン適用時に送料計算機をダイナミックに調整する。

構文

apply_filters( 'woocommerce_shipping_show_shipping_calculator', $show_shipping_calculator, $cart );

パラメータ

  • $show_shipping_calculator (bool): 送料計算機を表示するかどうかの真偽値。
  • $cart (WC_Cart): 現在のカートの状態。

戻り値

  • (bool): 送料計算機が表示される場合は true、表示されない場合は false を返す。

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

  • 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: 特定のユーザーにのみ送料計算機を表示

add_filter('woocommerce_shipping_show_shipping_calculator', 'custom_show_shipping_calculator', 10, 2);
function custom_show_shipping_calculator($show, $cart) {
    if (!is_user_logged_in()) {
        return false; // ログインしていない場合は非表示にする
    }
    return $show; // それ以外はデフォルトの動作を維持
}

このコードは、ユーザーがログインしていない場合、送料計算機を非表示にします。

サンプル2: 特定の商品カテゴリに基づく表示制御

add_filter('woocommerce_shipping_show_shipping_calculator', 'custom_category_show_shipping_calculator', 10, 2);
function custom_category_show_shipping_calculator($show, $cart) {
    $category_id = 123; // カテゴリIDを指定
    foreach ($cart->get_cart() as $cart_item) {
        if (has_term($category_id, 'product_cat', $cart_item['product_id'])) {
            return true; // 指定のカテゴリが含まれている場合は表示
        }
    }
    return false; // 含まれていない場合は非表示
}

このコードは、特定のカテゴリに商品が含まれている場合のみ送料計算機を表示します。

サンプル3: 地域に基づく表示制御

add_filter('woocommerce_shipping_show_shipping_calculator', 'custom_region_based_show_shipping_calculator', 10, 2);
function custom_region_based_show_shipping_calculator($show, $cart) {
    $user_country = WC()->customer->get_shipping_country();
    if ($user_country === 'JP') { // 日本の場合
        return true; // 表示
    }
    return false; // 他の国では非表示
}

このコードは、日本のユーザーに対してのみ送料計算機を表示します。

サンプル4: 商品がカートに無い場合に非表示

add_filter('woocommerce_shipping_show_shipping_calculator', 'custom_no_products_show_shipping_calculator', 10, 2);
function custom_no_products_show_shipping_calculator($show, $cart) {
    if ($cart->is_empty()) {
        return false; // カートが空の場合は非表示
    }
    return $show;
}

このコードは、カートが空の場合に送料計算機を非表示にします。

サンプル5: クーポン適用時に表示切替

add_filter('woocommerce_shipping_show_shipping_calculator', 'custom_coupon_based_show_shipping_calculator', 10, 2);
function custom_coupon_based_show_shipping_calculator($show, $cart) {
    if ($cart->has_discount()) {
        return true; // クーポンが適用されている場合は表示
    }
    return false; // それ以外は非表示
}

このコードは、クーポンが適用されている場合にのみ送料計算機を表示します。

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


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