Purchase Actions Reference
surecart/purchase_created
Occurs whenever a purchase is created. This typically happens when a customer successfully pays for an order at checkout. This event can be triggered for both manual payments and gateway payments.
do_action(
'surecart/purchase_created',
(\SureCart\Models\Purchase::class) $purchase,
(array) $webhook_data
)
How to use
add_action( 'surecart/purchase_created', 'purchaseCreated' );
function purchaseCreated($purchase, $webhook_data = []) {
//
}
surecart/purchase_revoked
Occurs whenever a purchase is revoked. This can happen if an order's line-item is refunded,
or a subscription is canceled or expired.
do_action(
'surecart/purchase_revoked',
(\SureCart\Models\Purchase::class) $purchase,
(array) $webhook_data
)
surecart/purchase_invoked
Occurs whenever a purchase is invoked. (This is the opposite of revoked). This can only occur if the purchase has been previously revoked.
do_action(
'surecart/purchase_invoked',
(\SureCart\Models\Purchase::class) $purchase,
(array) $webhook_data
)
surecart/purchase_updated
Occurs whenever a purchase is updated. This only occurs when a subscription's product or quantity changes.
do_action(
'surecart/purchase_updated',
(\SureCart\Models\Purchase::class) $purchase,
(array) $webhook_data
)
Real life Example - Adding a zoom link after purchase created in metadata.
We can use 'surecart/purchase_created' action hook with SureCart Models functionality to achieve that. Here is a quick example -
add_action('surecart/purchase_created', function($purchase, $webhook_data) {
$zoom_link = 'https://us04web.zoom.us/j/sample-zoom-meet-link';
$purchase_data = \SureCart\Models\Purchase::with( [ 'initial_order', 'order.checkout' ] )->find( $purchase->id );
$checkout = $purchase_data->initial_order->checkout ?? null;
if ($checkout) {
\SureCart\Models\Checkout::update([
'id' => $checkout->id,
'metadata' => [
'zoom_link' => $zoom_link
]
]);
}
}, 10, 2);
In the above example, after purchase created, we've first grab the purchase data with some more options with it, like initial order checkout. Then, we just update the checkout using SureCart's model functionality.
Updated 5 days ago