How do I add a marketing opt-in to the checkout?

This article covers how to insert a checkbox into the checkout flow, that when ticked, will allow you to send information to a third party system.


In order to add a marketing optionally opt-in to the checkout we first need to extend the CheckoutCustomerPage to inject our custom view that will show the checkbox for the customer to interact with. This can be done by adding the following code to the project's AppServiceProvider boot method:

\Aero\Checkout\Http\Responses\CheckoutCustomerPage::extend(function ($page) {
    $sections = $page->getData('form_sections');

    $sections = $sections->splice(0, $sections->keys()->search('customer', true) + 1)
        ->put('opt_in', 'checkout.opt-in')

    $page->setData('form_sections', $sections);

    $order = $page->cart->order();

    if ($order && ($optIn = $order->additional('opted_in'))) {
        $page->setData('opted_in', $optIn);

We then need to create our Twig view file, which should be placed in resources/views/checkout/opt-in.twig:

<div class="checkout__section">
    <div class="checkout__list">
                    <input type="checkbox" name="opted_in" value="1" {{ old('opted_in', opted_in) ? 'checked' : null }} />
                    <span><strong>Keep me up-to-date with news and special offers</strong></span>

This will result in the checkbox showing on the first step of the checkout. We now need to ensure that the checkbox choice is remembered and stored against the order. Add the following code to the AppServiceProvider boot method just below the code that was added above.

\Aero\Checkout\Http\Responses\CheckoutCustomerSet::extend(function ($page) {
    $data = \Illuminate\Support\Facades\Validator::make($page->request->all(), [
        'opted_in' => 'sometimes|bool',

    $optedIn = $data['opted_in'] ?? null;

    if (($order = $page->cart->order()) && ($optedIn !== null || $order->additional('opted_in'))) {
        $order->additional('opted_in', $optedIn);

The checkbox value will now be stored against the order as an additional attribute, which we can pick up once the customer has ordered using an event listener. Finally, add the following code into the AppServiceProvider boot method to listen to the OrderPlaced event: 

\Illuminate\Support\Facades\Event::listen(\Aero\Cart\Events\OrderPlaced::class, function ($event) {
    if ($event->order->additional('opted_in')) {
        $email = $event->order->email;
        // send $email to a third party...

Within this event listener, you can add your code to send information about the order, such as the email address of the customer, to a third party system.

Articles in this section

Was this article helpful?
0 out of 0 found this helpful