概要
wc_price_args
フィルタは、WooCommerceにおいて価格表示の設定を調整するために使用されます。このフィルタを利用することで、商品の価格を表示する際の引数を変更できます。以下のような機能を実装する際に特によく使われます:
- 価格のフォーマットを変更する
- 通貨記号の位置を調整する
- 小数点以下の桁数を変更する
- 消費税を含む価格表示にカスタマイズする
- 割引価格の表示スタイルを変更する
- 特定の条件下で価格を非表示にする
構文
add_filter('wc_price_args', 'custom_wc_price_args');
function custom_wc_price_args($args) {
// カスタマイズをここに記述
return $args;
}
パラメータ
$args
: 価格表示に関する引数が格納された配列。これを変更することで表示をカスタマイズできます。
戻り値
- 変更された引数を含む配列。
使用可能なバージョン
- WooCommerce: バージョン全般
- WordPress: バージョン全般
この関数のアクションでの使用可能性
アクション | 使用可能性 |
---|---|
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. 価格フォーマットの変更
このサンプルコードでは、商品の価格をカスタムフォーマットで表示するために、wc_price_args
を使用しています。
add_filter('wc_price_args', 'custom_price_format');
function custom_price_format($args) {
$args['format'] = '%1$s%2$s'; // 通貨記号を左に表示
return $args;
}
(参考URL: https://docs.woocommerce.com/wc-product-structure/)
2. 通貨記号の位置を変更
このコードでは、通貨記号を右側に表示するための設定を行っています。
add_filter('wc_price_args', 'change_currency_position');
function change_currency_position($args) {
$args['position'] = 'right'; // 通貨記号を右に表示
return $args;
}
(参考URL: https://www.wpbeginner.com/wp-tutorials/how-to-change-currency-symbol-in-woocommerce/)
3. 小数点以下の桁数をカスタマイズ
このサンプルでは、小数点以下の桁数を1桁に変更する設定を行っています。
add_filter('wc_price_args', 'set_decimal_places');
function set_decimal_places($args) {
$args['decimals'] = 1; // 小数点以下1桁
return $args;
}
(参考URL: https://woocommerce.wordpress.com/2020/06/25/currencies-in-woocommerce/)
4. 税込み価格の表示
このコードは、商品価格に消費税を含めて表示するための設定を行っています。
add_filter('wc_price_args', 'show_price_inclusive_tax');
function show_price_inclusive_tax($args) {
$args['tax_display'] = 'incl'; // 税込み価格を表示
return $args;
}
(参考URL: https://docs.woocommerce.com/document/setting-up-taxes/)
5. 割引価格のスタイルを変更
このサンプルでは、割引価格を特定のスタイルで表示します。
add_filter('wc_price_args', 'custom_discount_price_style');
function custom_discount_price_style($args) {
$args['discount_style'] = 'font-weight:bold;color:red;'; // 割引価格を赤色に太字表示
return $args;
}
(参考URL: https://woocommerce.com/document/markup-in-price-display/)