Custom Checkout metadata: working demo (ticket 1523386)
This page demonstrates custom metadata being attached to a SureCart Checkout from PHP and actually persisting on the platform. Everything below runs on a live SureCart store in test mode.
Step 1. Arrive with campaign parameters
Open the link below. A small script stores the parameters in a first party cookie named bsm_attribution, the same way your site does.
Open this page with the test campaign parameters
Step 2. Buy the test product
The store runs on the Test Processor, so no card details are needed. Enter any email and name and press Purchase.
Step 3. What SureCart actually stored
Come back to this page after the purchase and reload it. Panel 3 is re-fetched live from the SureCart API using the completed checkout ID, which is the same check you ran on surecart/checkout_confirmed.
(cookie not set - open this page with the campaign parameters first)
47c0f8e1-0673-479e-8ef8-87a5dc5fa80d
{
"page_id": "753",
"page_url": "https://sure.ivacode.com/popup-text-fixed-1546907/"
}The code running on this page
What does not work, and is what the developer docs example shows:
$model['metadata'] = array_merge( (array) $model['metadata'], $attribution ); // never leaves WordPress
What does work:
add_filter(
'surecart/request/model',
function ( $model, $request ) {
// Only touch checkouts.
if ( ! ( $model instanceof \SureCart\Models\Checkout ) ) {
return $model;
}
$attribution = my_attribution_data(); // your cookie reader, returns [ 'key' => 'string value' ]
if ( empty( $attribution ) ) {
return $model;
}
$existing = (array) ( $request->get_param( 'metadata' ) ?? array() );
// Merge into the REQUEST PARAM, not into $model['metadata'].
$request->set_param( 'metadata', array_merge( $attribution, $existing ) );
return $model;
},
10,
2
);
Reason: the checkout REST controller sends $request->get_json_params() to the SureCart API, not the model attributes, so anything written to $model['metadata'] is overwritten before the request is built and never leaves WordPress.