我想将自定义注册字段添加到标准WooCommerce注册表单中。它也在工作,你可以在这里看到。我现在还有其他问题......我实现了所有内容,它完全显示在我想要的地方。但没人能在那里注册......
即使只是电子邮件和密码字段......怎么可能?
如果您填写了字段并单击了注册,则可以在控制台中看到数据将通过$POST
发送。但为什么WooCommerce
不再注册任何人?
我写了echo Yes Address\n
或者echo Yes Phone\n
,如果地址或电话被填满,应该弹出...但是,任何人都无法向我解释...... < / p>
这是我的代码: -
function wooc_extra_register_fields() {
?>
<hr style="margin-top:20px;margin-bottom:5px;" class="woocommerce-expanding-custom-register-form">
<p class="form-row form-row-first woocommerce-expanding-custom-register-form">
<label for="reg_billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
</p>
<p class="form-row form-row-last woocommerce-expanding-custom-register-form">
<label for="reg_billing_last_name"><?php _e( 'Last name', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" />
</p>
<p class="form-row form-row-wide woocommerce-expanding-custom-register-form">
<label for="reg_billing_phone"><?php _e( 'Phone', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php if ( ! empty( $_POST['billing_phone'] ) ) esc_attr_e( $_POST['billing_phone'] ); ?>" />
</p>
<p class="form-row form-row-wide woocommerce-expanding-custom-register-form">
<label for="reg_billing_company"><?php _e( 'Company Name', 'woocommerce' ); ?></span></label>
<input type="text" class="input-text" name="reg_billing_company" id="reg_billing_company" value="<?php esc_attr_e( $_POST['billing_company'] ); ?>" />
</p>
<p class="form-row form-row-wide woocommerce-expanding-custom-register-form">
<label for="reg_billing_address_1"><?php _e( 'Address', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="reg_billing_address_1" id="reg_billing_address_1" value="<?php if ( ! empty( $_POST['billing_address_1'] ) ) esc_attr_e( $_POST['billing_address_1'] ); ?>" placeholder="Street Address" />
</p>
<p class="form-row form-row-wide woocommerce-expanding-custom-register-form">
<input type="text" class="input-text" name="reg_billing_address_2" id="reg_billing_address_2" value="<?php esc_attr_e( $_POST['billing_address_2'] ); ?>" placeholder="Apartmant, Suite, Unit, etc. (optional)" />
</p>
<p class="form-row form-row-wide woocommerce-expanding-custom-register-form">
<label for="reg_billing_city"><?php _e( 'Suburb', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="reg_billing_city" id="reg_billing_city" value="<?php if ( ! empty( $_POST['billing_city'] ) ) esc_attr_e( $_POST['billing_city'] ); ?>" />
</p>
<p class="form-row form-row-first woocommerce-expanding-custom-register-form">
<label for="reg_billing_state"><?php _e( 'State', 'woocommerce' ); ?><span class="required">*</span></label>
<select name="reg_billing_state" id="reg_billing_state" class="reg_billing_state" placeholder="Select a state…" value="<?php if ( ! empty( $_POST['billing_state'] ) ) esc_attr_e( $_POST['billing_state'] ); ?>">
<option value="ACT">Australian Capital Territory</option>
<option value="NSW">New South Wales</option>
<option value="NT">Northern Territory</option>
<option value="QLD">Queensland</option>
<option value="SA">South Australia</option>
<option value="TAS">Tasmania</option>
<option value="VIC">Victoria</option>
<option value="WA">Western Australia</option>
</select>
</p>
<p class="form-row form-row-last woocommerce-expanding-custom-register-form">
<label for="reg_billing_postcode"><?php _e( 'Postcode', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="reg_billing_postcode" id="reg_billing_postcode" value="<?php if ( ! empty( $_POST['billing_postcode'] ) ) esc_attr_e( $_POST['billing_postcode'] ); ?>" />
</p>
<?php
}
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );
function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {
if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
$validation_errors->add( 'billing_first_name_error', __( '<strong>Error</strong>: First name is required!', 'woocommerce' ) );
}
if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
$validation_errors->add( 'billing_last_name_error', __( '<strong>Error</strong>: Last name is required!.', 'woocommerce' ) );
}
if ( isset( $_POST['billing_phone'] ) && empty( $_POST['billing_phone'] ) ) {
$validation_errors->add( 'billing_phone_error', __( '<strong>Error</strong>: Phone number is required!.', 'woocommerce' ) );
}
if ( isset( $_POST['billing_address_1'] ) && empty( $_POST['billing_address_1'] ) ) {
$validation_errors->add( 'billing_address_1_error', __( '<strong>Error</strong>: Address is required!.', 'woocommerce' ) );
}
if ( isset( $_POST['billing_city'] ) && empty( $_POST['billing_city'] ) ) {
$validation_errors->add( 'billing_city_error', __( '<strong>Error</strong>: Suburb is required!.', 'woocommerce' ) );
}
if ( isset( $_POST['billing_state'] ) && empty( $_POST['billing_state'] ) ) {
$validation_errors->add( 'billing_state_error', __( '<strong>Error</strong>: State is required!.', 'woocommerce' ) );
}
if ( isset( $_POST['billing_postcode'] ) && empty( $_POST['billing_postcode'] ) ) {
$validation_errors->add( 'billing_postcode_error', __( '<strong>Error</strong>: Postcode is required!.', 'woocommerce' ) );
}
return $validation_errors;
}
add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );
function wooc_save_extra_register_fields( $customer_id ) {
if ( isset( $_POST['billing_phone'] ) ) {
// Phone input filed which is used in WooCommerce
update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
echo 'Yes Phone\n';
}
if ( isset( $_POST['billing_first_name'] ) ) {
//First name field which is by default
update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
// First name field which is used in WooCommerce
update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
echo 'Yes Name\n';
}
if ( isset( $_POST['billing_last_name'] ) ) {
// Last name field which is by default
update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
// Last name field which is used in WooCommerce
update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
echo 'Yes Last\n';
}
if ( isset( $_POST['billing_company'] ) ) {
update_user_meta( $customer_id, 'billing_company', sanitize_text_field( $_POST['billing_company'] ) );
echo 'Yes Company\n';
}
if ( isset( $_POST['billing_address_1'] ) ) {
update_user_meta( $customer_id, 'billing_address_1', sanitize_text_field( $_POST['billing_address_1'] ) );
echo 'Yes Address1\n';
}
if ( isset( $_POST['billing_address_2'] ) ) {
update_user_meta( $customer_id, 'billing_address_2', sanitize_text_field( $_POST['billing_address_2'] ) );
echo 'Yes Address2\n';
}
if ( isset( $_POST['billing_city'] ) ) {
update_user_meta( $customer_id, 'billing_city', sanitize_text_field( $_POST['billing_city'] ) );
echo 'Yes City\n';
}
if ( isset( $_POST['billing_state'] ) ) {
update_user_meta( $customer_id, 'billing_state', sanitize_text_field( $_POST['billing_state'] ) );
echo 'Yes State\n';
}
if ( isset( $_POST['billing_postcode'] ) ) {
update_user_meta( $customer_id, 'billing_postcode', sanitize_text_field( $_POST['billing_postcode'] ) );
echo 'Yes Postcode\n';
}
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
?>
非常感谢您的帮助!