You can easily increase your mailing list by adding a custom email subscription checkbox to your WooCommerce checkout page. A simple method to allow consumers to opt-in to receive your emails throughout the checkout process is presented in the snippet “Add a Custom Email Subscription Checkbox Field to the WooCommerce Checkout Form”.
Advantages of Including a Checkbox for Email Subscription
Giving clients the option to subscribe to your newsletters and promotional emails seamlessly occurs when you provide an email subscription checkbox at the point of sale. By doing this, you may greatly expand the size of your email list and reach a wider audience with your marketing efforts. You may make sure that your emails are received by engaged and interested recipients by piqueing their attention at the point of purchase.
Simplified Data Gathering and Google Sheets Integration
The snippet stores this preference in the order metadata in addition to adding the subscription option. A customer’s email address is stored in case they choose to subscribe, and the Google Sheets API allows for the automatic addition of that email address to a Google Sheet. Your subscriber list will always be current and readily available for use in future email marketing campaigns thanks to this connection.
Owners of WooCommerce stores can improve their checkout process, make it more engaging, and efficiently grow their email list for increased consumer engagement and retention by putting this snippet into practice.
function save_subscription_input( $order_id ) {
if( !empty( $_POST['subscribed'] ) && $_POST['subscribed'] == 1 ) {
update_post_meta( $order_id, 'subscribed', 'Yes' );
// Google Sheets API integration code to add a new row with the user's email
$email = $order->get_billing_email(); // Get the user's email from the order
$sheet_id = 'YOUR_SHEET_ID'; // Replace with your actual Google Sheets ID
$sheet_range = 'Sheet1!A1:B'; // Replace with the range of cells where you want to add the email
$access_token = 'YOUR_ACCESS_TOKEN'; // Replace with your actual Google Sheets API access token
$url = "https://sheets.googleapis.com/v4/spreadsheets/{$sheet_id}/values/{$sheet_range}:append?valueInputOption=RAW";
$data = array(
array($email),
);
$payload = json_encode(array(
'range' => $sheet_range,
'majorDimension' => 'ROWS',
'values' => $data
));
$headers = array(
'Authorization: Bearer ' . $access_token,
'Content-Type: application/json'
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
curl_close($ch);
} else {
update_post_meta( $order_id, 'subscribed', 'No' );
}
}
/**
* Snippet Name: Add a custom email subscription checkbox field to the WooCommerce checkout form.
* Snippet Author: ecommercehints.com
*/
add_action( 'woocommerce_after_order_notes', 'email_subscription_checkbox_field' );
add_action( 'woocommerce_checkout_update_order_meta', 'save_subscription_input' );
function email_subscription_checkbox_field( $checkout ) {
woocommerce_form_field( 'subscribed', array(
'type' => 'checkbox',
'class' => array('form-row-wide'),
'label' => 'Subscribe To Emails?',
), $checkout->get_value( 'subscribed' ) );
}
function save_subscription_input( $order_id ){
if( !empty( $_POST['subscribed'] ) && $_POST['subscribed'] == 1 ) {
update_post_meta( $order_id, 'subscribed', Yes );
} else {
update_post_meta( $order_id, 'subscribed', No );
}
}