Reputation: 11
I have been editing the checkout page on Woocommerce, and wanted to set up a checkbox, which is essentially saying "Tick the checkbox if you agree to receiving future emails".
My coding experience is lacking so I am having trouble piecing together some code to make this work.
Ideally I had intended to set up the checkbox so that when checked, the email address the customer entered gets collected and added to a MailChimp email list, however this is just way to complicated for me to even get close to achieving (although I wouldn't complain if someone could give me some pointers as to how I would possibly achieve this).
But essentially at the moment all I have been trying to do is set up some code in my functions.php just so that if someone leaves the box ticked, I will receive an email saying that someone has agreed, and I can then manually add their email to Mailchimp.
Although this isn't the ideal solution to what I want to achieve, I thought I'd find it a lot simpler as a coding rookie, however... I'm still struggling!
This is what I've got at the moment:
add_action( 'woocommerce_after_checkout_billing_form', 'add_content_after_billing', 15 );
function add_content_after_billing () {
echo '<input type="checkbox" name="emailconfirm" value="confirm" checked="checked"> Leave this box ticked if you give permission to be contacted via email in the future.';
}
if( $_POST ){
if ( (int)$_POST['emailconfirm'] == 1 ){
mail("*********@gmail.com","SubscribeAgreed","Email sub allowed");
}
}
As you have probably guessed, this isn't working and I do not receive an email!
Any help anyone can offer is much appreciated.
Upvotes: 1
Views: 1962
Reputation: 1
Woocommerce has a plugin that will allow you to have a checkbox asking the user to subscribe on your checkout page.
No coding required ;-)
https://docs.woocommerce.com/document/newsletter-subscription/
"There's always an easy way"
Michael
Upvotes: 0
Reputation: 254363
You can try the following code that will display a checkbox on checkout page and on My account "Edit billing address". If customer has not subscribed yet to emails, the field will be displayed in checkout. If customer subscribe and place the order, a subscription email will be sent to a specific email address:
// Display a subscribing email checkbox in checkout and on My account "Edit billing address".
add_action('woocommerce_billing_fields', 'checkout_email_confirmation_checkbox', 20, 1);
function checkout_email_confirmation_checkbox ( $fields ) {
if( is_checkout() )
$label = __("I want and agree to receiving future emails");
else
$label = __("Email subscription");
$is_registered = get_user_meta( get_current_user_id(), 'billing_emailing', true );
// Avoid displaying the checkbox in checkout when user is already registered
if( is_checkout() && $is_registered )
return $fields; // Exit
$fields['billing_emailing'] = array(
'type' => 'checkbox',
'class' => array('form-row-wide'),
'label' => $label,
'required' => false,
'clear' => true
);
return $fields;
}
// Send an email when a customer has subscribed
add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta', 20, 2 );
function custom_checkout_field_update_order_meta( $order_id, $data ) {
// Get an instance of the WC_Order Object
$order = wc_get_order($order_id);
// get user data to check if user has been already registered
$billing_emailing = get_user_meta( $order->get_customer_id(), 'billing_emailing', true );
// Checking email registration and sending an email to a specific address
if ( $order->get_meta('_billing_emailing') && ! $billing_emailing ) {
// User name + email
$first_name = $order->get_billing_first_name();
$last_name = $order->get_billing_last_name();
$user_email = $order->get_billing_email();
$headers .= 'From: ' . $first_name . ' ' . $last_name . ' <' . $user_email . '>\r\n';
// HERE set the recipient email address
$to = '[email protected]';
// Here the email subject
$subject = 'Email subscription';
// Here the email message
$message = 'First name: ' . $first_name . '\r\n';
$message = 'Last name: ' . $last_name . '\r\n';
$message = 'Email address: ' . $user_email . '\r\n';
// Send email:
wp_mail( $to, $subject, $message, $headers );
// Update user meta data (to avoid repetitions)
update_user_meta( $order->get_customer_id(), 'billing_emailing', true );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Checkbox is displayed in checkout page if customer has not subscribed yet:
Display the checkbox in My account > Adresses > Edit Billing Address, allowing customer to unsubscribe.
Now this code is not perfect (or not complete).
But there is free Mailchimp plugins that make the integration and automate everything.
You can use for example MailChimp for WordPress plugin that also integrate Woocommerce.
You will be able to set the mail checkbox in checkout page (and everything will be automated).
Upvotes: 1