概要
woocommerce_order_update_fee
フィルタは、WooCommerceでの注文の手数料を更新する際に使用されます。このフィルタを使用することで、特定の条件に基づいて手数料の金額を変更したり、新しい手数料を追加したりすることができます。
このフィルタは、以下のような機能を実装する際によく使用されます:
- 定期的なキャンペーンやプロモーションに基づく手数料の調整
- 顧客の購買履歴に応じた手数料の変更
- 特定の地域や国に基づく手数料の異なる設定
- チケット・予約システムにおける手数料の追加
- 顧客ターゲティングに基づく料金プランの適用
- 支払い方式による手数料変更
構文
add_filter('woocommerce_order_update_fee', 'custom_order_fee', 10, 3);
パラメータ
$fee
: 更新される手数料の金額$order
: 対象の WooCommerce 注文オブジェクト$new_fee
: 新しく設定する手数料オブジェクト
戻り値
- フィルタを通過した後の更新された手数料の金額
使用可能なバージョン
- WooCommerce バージョン: 2.0.0 以降
- WordPress バージョン: 3.0.0 以降
サンプルコード
- 特定のユーザーグループに基づく手数料の割引
add_filter('woocommerce_order_update_fee', 'custom_discount_fee', 10, 2);
function custom_discount_fee($fee, $order) {
if (in_array('wholesale', (array) $order->get_user()->roles)) {
$fee *= 0.9; // 10%割引
}
return $fee;
}
このコードは、ユーザーが「wholesale」グループに属している場合、手数料に10%の割引を適用します。
引用元: https://www.wpbeginner.com
- 特定の地域に基づく手数料の調整
add_filter('woocommerce_order_update_fee', 'regional_fee_adjustment', 10, 2);
function regional_fee_adjustment($fee, $order) {
if ($order->get_billing_country() === 'JP') {
$fee += 500; // 日本の場合、追加の手数料を加算
}
return $fee;
}
このサンプルコードは、日本の配送先に対して500円の追加手数料を加算します。
引用元: https://docs.woocommerce.com
- 支払い方法による手数料の追加
add_filter('woocommerce_order_update_fee', 'payment_method_fee', 10, 2);
function payment_method_fee($fee, $order) {
if ($order->get_payment_method() === 'credit_card') {
$fee += 300; // クレジットカード手数料の追加
}
return $fee;
}
このコードは、クレジットカードでの支払いを選択した場合に300円の手数料を追加します。
引用元: https://woocommerce.com
- 特定の商品がカートにある場合の手数料変更
add_filter('woocommerce_order_update_fee', 'product_based_fee_adjustment', 10, 2);
function product_based_fee_adjustment($fee, $order) {
if ($order->get_items()) {
foreach ($order->get_items() as $item) {
if ($item->get_product_id() === 123) { // 商品ID 123の場合
$fee += 200; // 手数料を200加算
}
}
}
return $fee;
}
このサンプルでは、特定の商品をカートに含む場合に手数料が200円追加されます。
引用元: https://www.cloudways.com
- 無料配送条件での手数料の無効化
add_filter('woocommerce_order_update_fee', 'disable_fee_for_free_shipping', 10, 2);
function disable_fee_for_free_shipping($fee, $order) {
if ($order->get_shipping_total() === 0) {
return 0; // 無料配送の場合は手数料を0に
}
return $fee;
}
このコードは、配送が無料の場合に手数料を無効化します。
引用元: https://www.sitepoint.com
この関数のアクションでの使用可能性
アクション名 | 使用可能性 |
---|---|
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 |
この表は、woocommerce_order_update_fee
フィルタがどのアクションで使用可能かを示しています。必要に応じて、これを参考にしてください。