Newsletter checkbox at checkout
Adds an unticked “Send me news and offers by email” checkbox to the checkout. The choice is saved on the order and shown in the order screen.
<?php
add_action(
'woocommerce_review_order_before_submit',
static function () {
woocommerce_form_field(
'snipfire_newsletter',
array(
'type' => 'checkbox',
'class' => array( 'form-row-wide' ),
'label' => 'Send me news and offers by email',
),
0
);
}
);
add_action(
'woocommerce_checkout_create_order',
static function ( $order ) {
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Checked by WooCommerce.
$order->update_meta_data( '_snipfire_newsletter', empty( $_POST['snipfire_newsletter'] ) ? 'no' : 'yes' );
}
);
add_action(
'woocommerce_admin_order_data_after_billing_address',
static function ( $order ) {
if ( 'yes' === $order->get_meta( '_snipfire_newsletter' ) ) {
echo '<p><strong>Newsletter:</strong> yes, the customer opted in.</p>';
}
}
);
Good to know
For the classic cart and checkout (the [woocommerce_cart] and [woocommerce_checkout] shortcodes). The block cart and checkout don’t use these hooks. Connect it to your newsletter tool, or export the orders to see who opted in. Under the GDPR the box must stay unticked.
In these packs
- WooCommerce: cart and checkout: Free-shipping progress, coupon links, quantity limits and a cleaner checkout.