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

概要

woocommerce_calculated_shipping フィルタは、WooCommerce の配送料金が計算される際にカスタマイズを行うために使用されます。このフィルタは、条件に応じて配送料金を変更するためのフックであり、特に以下のような機能を実装する際に役立ちます。

  1. 配送料金のマージンを追加または削除する
  2. 特定の地域に対して異なる送料を設定する
  3. 特定の条件に基づいたプロモーション料金を適用する
  4. 配送方法ごとの手数料を追加する
  5. 特定の商品のための特別な配送料金を計算する
  6. ユーザーの役割に応じた送料割引を適用する

構文

add_filter('woocommerce_calculated_shipping', 'custom_calculated_shipping', 10, 2);

パラメータ

  • $rates (配送料金の配列) – 計算された配送料金。
  • $package (配送パッケージ情報の配列) – 配送されるアイテムの詳細情報。

戻り値

  • フィルタされた配送料金の配列。

WooCommerce バージョン

  • 3.0 以降のバージョンが必要です。

WordPress バージョン

  • 4.7 以降のバージョンが必要です。

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

アクション 使用可能性
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_calculated_shipping', 'add_margin_to_shipping', 10, 2);
function add_margin_to_shipping($rates, $package) {
    foreach ($rates as $rate_key => $rate) {
        $rates[$rate_key]->cost = $rate->cost * 1.1; // 10%のマージンを追加
    }
    return $rates;
}

このコードは、計算された全ての配送料金に10%のマージンを追加します。

出典: WooCommerce Documentation

サンプルコード2: 特定地域の送料を変更

add_filter('woocommerce_calculated_shipping', 'change_shipping_for_specific_region', 10, 2);
function change_shipping_for_specific_region($rates, $package) {
    if ($package['destination']['country'] === 'JP') {
        foreach ($rates as $rate_key => $rate) {
            $rates[$rate_key]->cost = 500; // 日本向けの送料を500円に変更
        }
    }
    return $rates;
}

このコードは、配送先が日本の場合に送料を固定で500円に変更します。

出典: WooCommerce Documentation

サンプルコード3: プロモーション料金の適用

add_filter('woocommerce_calculated_shipping', 'apply_promotion_discount', 10, 2);
function apply_promotion_discount($rates, $package) {
    if (!empty($package['contents']) && count($package['contents']) > 3) {
        foreach ($rates as $rate_key => $rate) {
            $rates[$rate_key]->cost -= 100; // 3商品以上購入時に100円引き
        }
    }
    return $rates;
}

このコードは、カート内に3品以上の商品がある場合、送料を100円引きにします。

出典: WooCommerce Documentation

サンプルコード4: 商品による特別な送料

add_filter('woocommerce_calculated_shipping', 'special_shipping_for_product', 10, 2);
function special_shipping_for_product($rates, $package) {
    foreach ($package['contents'] as $item) {
        if ($item['product_id'] == 123) { // 商品ID 123の場合
            foreach ($rates as $rate_key => $rate) {
                $rates[$rate_key]->cost += 200; // 送料に200円追加
            }
        }
    }
    return $rates;
}

このコードは、特定の商品(ID 123)がカートに含まれている場合に送料を200円増加させます。

出典: WooCommerce Documentation

サンプルコード5: ユーザーの役割に応じた送料割引

add_filter('woocommerce_calculated_shipping', 'user_role_based_discount', 10, 2);
function user_role_based_discount($rates, $package) {
    if (current_user_can('subscriber')) { // サブスクライバーの役割を持つユーザーの場合
        foreach ($rates as $rate_key => $rate) {
            $rates[$rate_key]->cost -= 150; // 送料を150円引き
        }
    }
    return $rates;
}

このコードは、サブスクライバーのユーザーに対して送料を150円引きにします。

出典: WooCommerce Documentation

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


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