【WordPress】WooCommerce支払い処理時の項目非表示方法

Wordpress

はじめに

WooCommerceは商品がバーチャルやダウンロードであっても決済時には購入者の住所等が表示され、入力を求められてしまいます。
発送する必要が無い商品の場合の住所入力は必要は無いので、購入者の入力の手間をなるべく省きたいところです。

実現方法

使用しているテーマのフォルダ内のfunctions.phpに以下コードを追記します。
標準では、wp-content/themes/テーマフォルダ/functions.phpという感じになっていると思います。

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
unset($fields['billing']['billing_postcode']);
unset($fields['billing']['billing_country']);
unset($fields['billing']['billing_state']);
unset($fields['billing']['billing_phone']);
unset($fields['order']['order_comments']);
unset($fields['billing']['billing_address_1']);
unset($fields['billing']['billing_address_2']);
unset($fields['billing']['billing_postcode']);
unset($fields['billing']['billing_company']);
//unset($fields['billing']['billing_first_name']);
//unset($fields['billing']['billing_last_name']);
unset($fields['billing']['billing_yomigana_first_name']);
unset($fields['billing']['billing_yomigana_last_name']);
//unset($fields['billing']['billing_email']);
unset($fields['billing']['billing_city']);
return $fields;
}

非表示にする項目を指定するのですが、非表示にしない項目はコメントアウトします。
上記例では、姓、名、メールアドレスの3行のみを表示させるようになっています。

まとめ

この設定で全商品の入力画面が固定されますが、発送が必要な商品と混在する場合は 追加情報として配送先住所を入力する事で対応可能です。

コードを埋め込み後コメントアウトで項目の表示/非表示を切り替える事ができますが、できれば標準設定で搭載して欲しい機能です。

日々のITエンジニアの日常作業を発信しています。
また、様々なレビュー記事も書いていますので、ぜひご活用ください。

また投稿は基本的に毎週月曜日です。
2025年2月中旬より入院の為投稿できませんが、
早く復帰できるように頑張ります!
その間、過去記事をご参照頂ければ幸いです。

では今週も頑張っていきましょう!
ガンバー!!

Wordpress

コメント