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

概要

woocommerce_matched_rates フィルタは、WooCommerce の配送オプションをカスタマイズするために使用されるフックです。このフィルタを利用することで、利用可能な配送料金を動的に変更したりフィルタリングしたりすることができます。主に次のような機能を実装する際に用いられます。

  1. カスタム配送方法の追加
  2. 特定の商品やカートの条件に基づく送料の割引
  3. 地域や顧客属性に応じた送料の調整
  4. 複数の配送業者からの送料の選択肢の提供
  5. プロモーションに基づく送料の変更
  6. データ分析に基づく送料設定の調整

構文

add_filter( 'woocommerce_matched_rates', 'custom_function_name', 10, 2 );

パラメータ

  • $matched_rates: 配送方法の配列。
  • $package: 配送パッケージ情報の配列。

戻り値

  • array: 変更された配送方法の配列。

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

  • 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_matched_rates', 'discount_shipping_for_specific_region', 10, 2 );

function discount_shipping_for_specific_region( $matched_rates, $package ) {
    if ( $package['destination']['country'] === 'JP' ) { // 日本の地域の確認
        foreach ( $matched_rates as $rate_key => $rate ) {
            $matched_rates[ $rate_key ]->cost *= 0.9; // 送料を10%割引
        }
    }
    return $matched_rates;
}
  • 見ている地域が日本の場合、送料を10%割引します。
  • 引用元: https://woocommerce.com/document/introduction-to-hooks/

サンプル 2: 配送方法の除外

特定の配送方法を除外するコードです。

add_filter( 'woocommerce_matched_rates', 'exclude_shipping_method', 10, 2 );

function exclude_shipping_method( $matched_rates, $package ) {
    foreach ( $matched_rates as $rate_key => $rate ) {
        if ( 'flat_rate' === $rate->method_id ) { // フラットレートの除外
            unset( $matched_rates[ $rate_key ] );
        }
    }
    return $matched_rates;
}
  • フラットレートの配送方法をマッチした送料リストから除外します。
  • 引用元: https://docs.woocommerce.com/wc-apidocs/class-WC_Shipping_Method.html

サンプル 3: 新しい配送方法の追加

カスタム配送方法をマッチした配送オプションに追加します。

add_filter( 'woocommerce_matched_rates', 'add_custom_shipping_method', 10, 2 );

function add_custom_shipping_method( $matched_rates, $package ) {
    $new_rate = new stdClass();
    $new_rate->id = 'custom_shipping'; // メソッドID
    $new_rate->label = 'Custom Shipping'; // メソッド名
    $new_rate->cost = 500; // コスト
    $matched_rates[] = $new_rate; // 配送料に追加
    return $matched_rates;
}
  • 新しい配送方法「Custom Shipping」を追加します。
  • 引用元: https://woocommerce.com/document/adding-custom-shipping-methods/

サンプル 4: カート内商品数に応じた送料変更

カート内の対象商品数に応じて価格を変動させるコードです。

add_filter( 'woocommerce_matched_rates', 'adjust_shipping_based_on_cart_items', 10, 2 );

function adjust_shipping_based_on_cart_items( $matched_rates, $package ) {
    $item_count = WC()->cart->get_cart_contents_count();
    foreach ( $matched_rates as $rate_key => $rate ) {
        if ( $item_count > 5 ) {
            $matched_rates[ $rate_key ]->cost += 100; // 6アイテム以上なら送料が100円上乗せ
        }
    }
    return $matched_rates;
}
  • 6個以上のアイテムがカートに入っている場合、送料を100円上乗せします。
  • 引用元: https://woocommerce.com/document/using-the-woocommerce-cart-api/

サンプル 5: カスタマーフィードバックに基づく送料の調整

顧客のフィードバックを基に送料を調整する例です。

add_filter( 'woocommerce_matched_rates', 'adjust_shipping_based_on_feedback', 10, 2 );

function adjust_shipping_based_on_feedback( $matched_rates, $package ) {
    // フィードバックによるスコアを取得する仮定。、
    $feedback_score = get_customer_feedback_score(); // この関数はデモ用
    foreach ( $matched_rates as $rate_key => $rate ) {
        if ( $feedback_score < 3 ) {
            $matched_rates[ $rate_key ]->cost += 200; // 評価が低い場合送料を200上乗せ
        }
    }
    return $matched_rates;
}
  • 顧客のフィードバックスコアが低い場合に送料を200円上乗せします。
  • 引用元: https://woocommerce.com/document/woocommerce-hooks/

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


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