[英]Customize addresses fields on WooCommerce My account and Checkout
[英]wordpress woocommerce - show and modify account fields on checkout page
我了解您可以在WooCommerce的結帳頁面上添加自定義字段,但是我想在帳單明細之前顯示的是帳戶字段,該字段已經按照文檔中的說明存在 。 這些字段的名稱為:
account_username
account_password
account_password-2
但是默認情況下不顯示它們。 我只能通過將它們放在函數中列表的頂部以重新排序帳單字段(如在我的主題的function.php
來使其可見。
add_filter("woocommerce_checkout_fields", "order_fields");
function order_fields($fields) {
$order = array(
"account_username",
"account_password",
"account_password-2",
"billing_first_name",
"billing_last_name",
// other billing fields go here
);
foreach($order as $field)
{
$ordered_fields[$field] = $fields["billing"][$field];
}
$fields["billing"] = $ordered_fields;
return $fields;
}
這與在結帳時創建帳戶的功能配合得很好,但是我在修改其標簽和占位符時遇到了麻煩。 這是我嘗試做的:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['account']['account_username']['label'] = '* Username: ';
$fields['account']['account_username']['placeholder'] = 'Enter username here...';
}
但這不會讓我更改字段的標簽和占位符,因此我在想,這可能與我如何顯示它和/或修改它們有關。
想法,有人嗎? 提前致謝。
我已經找到了答案,因此,如果有人遇到同樣的問題,這是最好的解決方案。 在本例中,與其嘗試使帳戶字段可見,還不如手動輸出所需的字段,因為我仍然不需要大多數默認字段,因此效率更高。
我所做的是覆蓋form-billing.php
模板。 我刪除了這部分字段的循環:
<?php foreach ( $checkout->checkout_fields['billing'] as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
並將其替換為單獨添加到頁面中:
<?php
woocommerce_form_field( 'billing_first_name', $checkout->checkout_fields['billing']['billing_first_name'], $checkout->get_value( 'billing_first_name') );
woocommerce_form_field( 'billing_email', $checkout->checkout_fields['billing']['billing_email'], $checkout->get_value( 'billing_email') );
woocommerce_form_field( 'account_username', $checkout->checkout_fields['account']['account_username'], $checkout->get_value( 'account_username') );
woocommerce_form_field( 'account_password', $checkout->checkout_fields['account']['account_password'], $checkout->get_value( 'account_password') );
woocommerce_form_field( 'account_password-2', $checkout->checkout_fields['account']['account_password-2'], $checkout->get_value( 'account_password-2') );
//...other fields that I need
?>
從那里,對標簽,占位符等的修改就可以了。 希望它也適用於同樣問題的其他人。 干杯! :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.