備忘録
WordPressで税込価格を自動的に表示させる
WordPressで税抜価格をカスタムフィールドに設定しておき、自動的に8%の消費税込価格を表示させる方法です。
カスタムフィールドはAdvanced Custom Fieldsを使って、「entryPrice」という名前で設定しています。
1 | <?php the_field( 'entryPrice',$post->ID); ?>円(税込価格: |
2 | <?php |
3 | $hontai = get_field('entryPrice'); |
4 | $nedan = $hontai * 1.08; |
5 | print round($nedan,0); |
6 | ?> |
7 | 円) |
<解説>
$hontai = get_field('entryPrice');
カスタムフィールドに設定した「税抜価格(entryPrice)」をhontaiという変数に入れる。
$nedan = $hontai * 1.08;
本体価格に1.08を掛けた「税込価格」をnedanという変数に入れる。
print round($nedan,0);
最後に、税込価格の変数 nedan の小数点以下1桁目を四捨五入にして整数にします。
四捨五入する「round」にオプションとして「0」を設定すると、小数点以下1桁目を四捨五入してくれます。
Dec 26, 2014 10:49
