Skip to content

Early-bird lifetime licence: $199 once, for the first 200 buyers only. See the offer

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.

  • Type PHP
  • Runs Everywhere (runs as WordPress loads)
  • Needs WooCommerce
  • Category WooCommerce
  • Tested with WordPress 7.1, WooCommerce 11.1
PHP
<?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

Move your snippets over this afternoon.

Importing changes nothing until you switch over, and your old shortcodes keep working.