Adding and Managing Custom Fields in Checkout Form

Let's assume, we're going to use some custom fields in our SureCart checkout form. For this example, we would add member_id and member_phone as some custom checkout fields.

How to Add custom fields in checkout.

In the Checkout form, edit that add a Text field block, and add the name as that would be handled while checkout. There are many more blocks from SureCart, that you could use to manage data like Textarea, Checkbox, and many more.

For the above example, we've added two custom fields - Member ID and Member Phone. For Member ID, we've added the name as member_id and also other labels and placeholders if needed.

And for Member phone, similarly member_phone as name and other respective values.

After adding those, the fields will be displayed on the checkout form.

How to Get the custom data after Checkout

So, When a customer purchases and gives some value to the checkout fields, then, we can grab them once checkout is confirmed, using the surecart/checkout_confirmed hook.

Here's an example of that hook

function addCustomData( $checkout, $request ) {
	// Handle anything with those checkout data.
}

add_action( 'surecart/checkout_confirmed', 'addCustomData', 10, 2 );

In the checkout, we've got many properties and data. For custom fields, there is metadata. Which would look like this -

[metadata] => stdClass Object
(
    [page_id] => 7
    [page_url] => http://wpex.local/checkout/
    [member_id] => 121
    [member_phone] => 1234567890
    [wp_created_by] => 1
)

And as it's an object, we could easily grab that data like so -

$member_id = $checkout->metadata->member_id ?? null;
$member_phone = $checkout->metadata->member_phone ?? null;

So, the full hook implementation would be like so -

function addCustomData( $checkout, $request ) {
    $member_id = $checkout->metadata->member_id ?? null;
    $member_phone = $checkout->metadata->member_phone ?? null;
   // Do something more with those data.
}

add_action( 'surecart/checkout_confirmed', 'addCustomData', 10, 2 );

Hope this would clear your confusions regarding implementing custom field and value in SureCart.