概要
woocommerce_find_rates
フィルタは、WooCommerceの配送オプションの取得プロセスに干渉するためのフックです。このフィルタを使用することで、ユーザーがカートに商品を追加した際に表示される配送方法や料金をカスタマイズすることができます。改善点や新しい機能を追加したいときによく利用されるケースには以下のようなものがあります。
- 特定の条件に基づいた特別料金の適用
- 地域ごとに異なる配送オプションの追加
- プロモーションのための割引料金の設定
- 配送業者に基づく料金の調整
- 特定のユーザーグループ向けのカスタム配送方法の提供
- カート内容に基づいた動的な配送料金の設定
構文
add_filter('woocommerce_find_rates', 'custom_find_rates', 10, 2);
パラメータ
array $rates
: 既存の配送オプションの配列。WC_Shipping_Method $shipping_method
: 使用されている配送方法オブジェクト。
戻り値
- 配送オプションの修正された配列。変更を加えることで、最終的にWooCommerceが返す配送方法の一覧をカスタマイズできます。
使用可能なプラグインおよびバージョン
- WooCommerce バージョン: 2.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_find_rates', 'apply_discount_for_specific_region', 10, 2);
function apply_discount_for_specific_region($rates, $shipping_method) {
$user_region = WC()->customer->get_shipping_state();
if ($user_region === 'CA') { // カナダの地域
foreach ($rates as $rate_key => $rate) {
$rates[$rate_key]->cost -= 5; // すべての配送方法から5ドル割引
}
}
return $rates;
}
このサンプルコードは、カナダの地域に配送する際にすべての配送オプションから5ドルを割引します。
サンプルコード 2: 商品ごとに異なる配送方法を追加する
add_filter('woocommerce_find_rates', 'add_custom_shipping_method_for_product', 10, 2);
function add_custom_shipping_method_for_product($rates, $shipping_method) {
foreach (WC()->cart->get_cart() as $cart_item) {
$product_id = $cart_item['product_id'];
if ($product_id === 123) { // 商品ID 123 に対して
$rates['custom_method'] = new WC_Shipping_Rate('custom_method', '特別配送', 10.00, [], 'custom');
break;
}
}
return $rates;
}
このコードは、特定の商品の場合に特別な配送オプションを追加します。
サンプルコード 3: 無料配送を特定の条件で提供する
add_filter('woocommerce_find_rates', 'offer_free_shipping_based_on_cart_total', 10, 2);
function offer_free_shipping_based_on_cart_total($rates, $shipping_method) {
if (WC()->cart->get_subtotal() > 100) { // カートの合計が100ドル以上
if (isset($rates['free_shipping'])) {
$rates['free_shipping']->cost = 0; // 無料配送のコストを0に設定
}
}
return $rates;
}
このコードは、カートの合計が100ドルを超えた場合に無料配送のオプションを提供します。
サンプルコード 4: 配送業者の料金を調整する
add_filter('woocommerce_find_rates', 'adjust_shipping_rates_based_on_shipping_method', 10, 2);
function adjust_shipping_rates_based_on_shipping_method($rates, $shipping_method) {
if ($shipping_method->id === 'flat_rate') {
foreach ($rates as $rate_key => $rate) {
$rates[$rate_key]->cost *= 1.1; // flat_rateの料金を10%増加させる
}
}
return $rates;
}
このサンプルコードは、特定の配送方式に対して料金を10%増加させます。
サンプルコード 5: ユーザーロールに基づいて配送方法をカスタマイズする
add_filter('woocommerce_find_rates', 'customize_shipping_for_user_role', 10, 2);
function customize_shipping_for_user_role($rates, $shipping_method) {
if (current_user_can('wholesale_customer')) { // 卸売顧客
foreach ($rates as $rate_key => $rate) {
$rates[$rate_key]->cost *= 0.9; // 卸売顧客には10%の割引
}
}
return $rates;
}
このコードは、卸売顧客に対してすべての配送オプションの料金を10%割引します。