WooCommerce Subscriptions Customization: Restore Additional Profile Field Added by PMP Register Helper

Can’t find a good title for this one. Here is the background. On a membership website, the membership feature was achieved by using Paid Membership Pro (PMP) plugin. An extra profile field was added using PMP Register Helper. Few months ago, a WooCommerce online store integration project brought in WooCommerce Subscriptions plugin to replace the membership purchase and recurring billing function which were provided by PMP. If you happen to be on the same boat, you know exactly what happens: The PMP check out page is replaced by WooCommerce checkout page. The additional profile filed is also bypassed.

The task: to restore the additional profile field using WooCommerce filter & hook.

First of all, here is the code to add the additional profile field using PMP Register Helper plugin.

$text = new PMProRH_Field(
	"new_member_name",
	"text",
	array(
		"label"		=> "New Member's Name",
		"size"		=> 30,
		"class"		=> "member_name",
		"profile"	=> true,
		"required"	=> true,
	)
);

pmprorh_add_registration_field("after_email", $text);

The code shows how to add additional field called “New Member’s Name” in PMP. The additional field is saved in usermeta table, meta_key is “new_member_name”.

Because it can be and should be treated as part of user account information. I utilized an existing account field,Ā  just added a new variable called “new_member_name”.

// Adding custom profile field to the checkout form
function mycustom_profile_field( $checkout_fields ) {
	// The field is only available when subscription product is present in shopping cart
	if ( WC_Subscriptions_Cart::cart_contains_subscription() && ! is_user_logged_in() ) {

		$checkout_fields['account']['new_member_name'] = array(
			'type' => 'text',
			'label' => __('New Member\'s Name', 'woocommerce'),
			'placeholder' => _x('member\'s name', 'placeholder', 'woocommerce'),
			'required'	=> true
        );
	}

	return $checkout_fields;
}

add_filter( 'woocommerce_checkout_fields' , 'mycustom_profile_field' );

Normally in WooCommerce, we should have form field validation upon form submission. In this case, “new_member_name” field is set to required (line 10), WooCommerce checkout page will take care of the validation. The warning message is pretty straightforward, “New Member’s Name is required.”

In case you would like to add some extra info, it can be achieved by using wc_add_notice() and “woocommerce_checkout_process” filter.

//Validation registration form  after submission using the filter registration_errors
function hlm_custom_checkout_field_process() {
    // Check if set, if its not set add an error.
	if ( WC_Subscriptions_Cart::cart_contains_subscription() && ! is_user_logged_in() ) {
	    if ( ! $_POST['new_member_name'] )
	        wc_add_notice( __( 'Please enter New Member Name.' ), 'error' );
	}
}

add_filter('woocommerce_checkout_process', 'mycustom_custom_checkout_field_process', 10,3);

Final step, we need to save the user input. This is done by using “woocommerce_created_customer” action.

//Updating user meta after new user account is created
function mycustom_adding_extra_reg_fields($user_id) {
	extract($_POST);
	update_user_meta($user_id, 'new_member_name', $new_member_name);
}

add_action('woocommerce_created_customer','mycustom_adding_extra_reg_fields');

I am really impressed by how simple this could be done using just two hooks. In case you don’t have PMP & PMP Register Helper, but want to add extra profile filter, extra customization is needed to display it on user profile page.