概要
woocommerce_order_get_total_discount
フィルタは、WooCommerceにおいて、特定のオーダーの総割引額をカスタマイズするために使用されるものです。このフィルタを利用することで、割引額を独自のロジックや条件に基づいて変更することが可能です。このフィルタは、次のような機能を実装する際によく使用されます:
- 特定の顧客グループに対する割引のカスタマイズ
- プロモーションコードやクーポンの適用条件の変更
- 大量購入時の割引の追加
- 特定の製品やカテゴリーに対する割引ルールの実装
- オーダー金額に基づく自動割引の設定
- 顧客の購買履歴に基づく個別割引オファーの提示
構文
add_filter('woocommerce_order_get_total_discount', 'your_function_name', 10, 2);
パラメータ
$discount
(float): オーダーの現在の割引額$order
(WC_Order): オーダーオブジェクト
戻り値
- float: 修正された割引額
使用可能なプラグインのバージョン
- WooCommerce: 3.0.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_order_get_total_discount', 'custom_discount_for_specific_group', 10, 2);
function custom_discount_for_specific_group($discount, $order) {
if (in_array('wholesale_customer', (array) $order->get_meta('user_roles'))) {
return $discount + 10; // 卸売顧客に追加で10ドルの割引を適用
}
return $discount;
}
このコードは、卸売顧客に対して追加の割引を適用します。
サンプル2: 特定のプロモーションコードによる割引
add_filter('woocommerce_order_get_total_discount', 'custom_discount_for_promo_code', 10, 2);
function custom_discount_for_promo_code($discount, $order) {
if ($order->get_coupon_codes() && in_array('PROMO2023', $order->get_coupon_codes())) {
return $discount + 5; // 特定のプロモコードを持っている場合、5ドルの割引を追加
}
return $discount;
}
これはプロモーションコードが適用されている時に割引を追加します。
サンプル3: 購入金額に応じた割引
add_filter('woocommerce_order_get_total_discount', 'discount_based_on_order_total', 10, 2);
function discount_based_on_order_total($discount, $order) {
if ($order->get_total() > 100) {
return $discount + ($order->get_total() * 0.1); // 100ドル以上の場合、10%の割引を適用
}
return $discount;
}
このコードは、オーダー総額が100ドルを超える場合に10%の割引を追加します。
サンプル4: 一定の製品に特別割引を追加
add_filter('woocommerce_order_get_total_discount', 'special_discount_on_specific_product', 10, 2);
function special_discount_on_specific_product($discount, $order) {
foreach ($order->get_items() as $item) {
if ($item->get_product_id() === 123) { // 製品IDが123の場合
return $discount + 20; // 20ドルの追加割引を適用
}
}
return $discount;
}
特定の製品を含むオーダーに対して追加の割引を提供します。
サンプル5: 購入履歴に基づく割引
add_filter('woocommerce_order_get_total_discount', 'discount_based_on_purchase_history', 10, 2);
function discount_based_on_purchase_history($discount, $order) {
$customer_id = $order->get_customer_id();
$purchase_count = wc_get_customer_order_count($customer_id);
if ($purchase_count > 5) {
return $discount + 15; // 購入回数が5回以上の場合、15ドルの割引を適用
}
return $discount;
}
このコードは、顧客の過去の購入回数に応じて割引額を調整します。