概要
woocommerce_product_coupon_types
フィルタは、WooCommerceで使用されるクーポンタイプをカスタマイズするためのフックです。このフィルタを使用することで、特定の条件に応じた独自のクーポンタイプを追加したり、既存のクーポンタイプを変更したりすることができます。これにより、ショップオーナーは販促活動をさまざまな方法で強化し、顧客に魅力的なオファーを提供することが可能です。
通常、woocommerce_product_coupon_types
フィルタは以下のような機能を実装する際によく使用されます:
- 新しいクーポンタイプの追加
- 既存のクーポンタイプの編集
- クーポンの条件をカスタマイズ
- 特定の商品やカテゴリ専用のクーポン作成
- クーポンの有効期限や使用回数制限の設定
- 特定の顧客向けの専用クーポンの作成
構文
add_filter( 'woocommerce_product_coupon_types', 'your_custom_coupon_types_function' );
パラメータ
$coupon_types
(array) – 既存のクーポンタイプを含む配列
戻り値
- array – 修正されたクーポンタイプの配列
使用可能なプラグインバージョン
このフィルタは、WooCommerce で利用可能です。WooCommerce のバージョンは 2.6.0以降のものに対応しています。
使用可能なWordPressバージョン
このフィルタは、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_product_coupon_types', 'add_custom_coupon_type' );
function add_custom_coupon_type( $coupon_types ) {
$coupon_types['custom_discount'] = 'カスタム割引';
return $coupon_types;
}
このサンプルコードは、WooCommerce に新しいクーポンタイプ “カスタム割引” を追加します。
サンプルコード 2
add_filter( 'woocommerce_product_coupon_types', 'modify_existing_coupon_type' );
function modify_existing_coupon_type( $coupon_types ) {
// 'percentage' にカスタムラベルを追加します
$coupon_types['percentage'] = 'パーセンテージ割引 (カスタム)';
return $coupon_types;
}
このコードは、既存の “percentage” クーポンタイプのラベルを変更します。
サンプルコード 3
add_filter( 'woocommerce_product_coupon_types', 'remove_coupon_type' );
function remove_coupon_type( $coupon_types ) {
unset( $coupon_types['fixed_cart'] ); // 固定カート割引を削除
return $coupon_types;
}
このコードは、カート全体の固定割引型のクーポンタイプを除去します。
サンプルコード 4
add_filter( 'woocommerce_product_coupon_types', 'conditional_coupon_type' );
function conditional_coupon_type( $coupon_types ) {
// 条件付きでクーポンタイプを追加
if ( is_user_logged_in() ) {
$coupon_types['loyalty_discount'] = 'ロイヤリティ割引';
}
return $coupon_types;
}
このサンプルコードは、ユーザーがログインしている場合にのみ、新しい “ロイヤリティ割引” クーポンタイプを追加します。
サンプルコード 5
add_filter( 'woocommerce_product_coupon_types', 'duplicate_coupon_type_label' );
function duplicate_coupon_type_label( $coupon_types ) {
// 既存のクーポンタイプを複製
$coupon_types['duplicate_discount'] = '複製割引';
$coupon_types['duplicate_discount_2'] = '複製割引2';
return $coupon_types;
}
このコードは、同じ割引スタイルの異なる2つのクーポンタイプを追加します。
これらのサンプルコードは、WooCommerce内でのクーポンタイプの拡張およびカスタマイズ方法を示しています。