我跟踪了the instructions 用于向WooCommerce注册表中添加额外字段,字段将显示并成功将值添加到数据库中。
但是,我无法验证新表单字段,因为woocommerce_register_post 行动似乎不起作用。
我试着用add_filter(\'woocommerce_registration_errors\',… 和add_filter(\'registration_errors\',… 也一样,但似乎没有人开火。
有人有什么见解吗?
这是WP 4.2.4和WC 2.4.3。
参考代码如下:
class Registration
{
public function __construct()
{
$this->actions();
}
public function actions()
{
add_action(\'show_user_profile\', [$this, \'admin_customer_code\']);
add_action(\'edit_user_profile\', [$this, \'admin_customer_code\']);
add_action(\'personal_options_update\', [$this, \'save_admin_customer_code\']);
add_action(\'edit_user_profile_update\', [$this, \'save_admin_customer_code\']);
add_action(\'woocommerce_register_form_start\', [$this, \'registration_form\']);
add_action(\'woocommerce_register_post\', [$this, \'registration_errors\'], 10, 3);
add_action(\'woocommerce_created_customer\', [$this, \'user_register\']);
//add_filter(\'registration_errors\', [$this, \'registration_errors\'], 10, 3);
//add_filter(\'woocommerce_registration_errors\', [$this, \'registration_errors\'], 10, 3);
}
public function admin_customer_code($user)
{
$customercode = esc_attr(get_the_author_meta(\'customer_code\', $user->ID));
$r = \'<h3>Customer Code</h3>
<table class="form-table">
<tr>
<th><label for="customer_code">Customer Code</label></th>
<td>
<input type="text" name="customer_code" id="customer_code" value="\' . $customercode . \'">
</td>
</tr>
</table>\';
echo $r;
}
public function save_admin_customer_code($user_id)
{
update_user_meta($user_id, \'customer_code\', sanitize_text_field($_POST[\'customer_code\']));
}
////////////////////////////////////////////////////////////////////////////
public function registration_form()
{
$customercode = (! empty($_POST[\'customer_code\'])) ? trim($_POST[\'customer_code\']) : \'\';
$r = \'<p class="form-row form-row-wide">
<label for="customer_code">\' . __(\'Customer Code\', \'textdomain\') . \' <span class="required">*</span></label>
<input type="text" name="customer_code" id="customer_code" class="input" value="\' . esc_attr(wp_unslash($customercode)) . \'" size="25" />
</p>\';
echo $r;
}
public function registration_errors($username, $email, $validation_errors)
{
echo "registration_errors";
var_dump($_POST);
exit(); // Never happens :(
if (empty($_POST[\'customer_code\']) || ! empty($_POST[\'customer_code\']) && trim($_POST[\'customer_code\']) == \'\') {
$validation_errors->add(\'customer_code_error\', __(\'<strong>Error</strong>: Please provide your customer code.\', \'textdomain\'));
}
return $validation_errors;
}
public function user_register($user_id)
{
if (! empty($_POST[\'customer_code\'])) {
update_user_meta($user_id, \'customer_code\', trim($_POST[\'customer_code\']));
}
}
}