Skip to content

Instantly share code, notes, and snippets.

@nazrul-kabir
Forked from voboghure-dev/functions.php
Last active February 26, 2021 14:30
Show Gist options
  • Save nazrul-kabir/26e325b6f69fb256828f8ec50ed983dd to your computer and use it in GitHub Desktop.
Save nazrul-kabir/26e325b6f69fb256828f8ec50ed983dd to your computer and use it in GitHub Desktop.
Add a checkbox in checkout page for customer whether wants to be a member & send customer details to admin email
/**
* ADD BECOME MEMBER CHECKBOX AT CHECKOUT PAGE FOR CUSTOMER
*/
function tkd_add_checkout_checkbox() {
woocommerce_form_field( 'checkout-checkbox', array( // CSS ID
'type' => 'checkbox',
'class' => array('form-row mycheckbox'), // CSS Class
'label_class' => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
'input_class' => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
// 'required' => true, // Mandatory or Optional
'label' => 'Want to be a member?', // Label and Link
));
}
add_action( 'woocommerce_review_order_before_submit', 'tkd_add_checkout_checkbox', 10 );
/**
* Alert if checkbox not checked
*/
function tkd_add_checkout_checkbox_warning() {
// its just a check that this function can get the POST value and do the necessary.
if ( (int) isset( $_POST['checkout-checkbox'] ) ) {
$to = 'yourmail@here';
$subject = 'Mail Subject Here';
$headers = array('Content-Type: text/html; charset=UTF-8');
$msg = '';
$data = [];
foreach($_POST as $key => $value) {
$data[$key] = $value;
$msg .= $key . ' : ' . $value . '<br>';
}
$mail_body = 'Name:'.$data['billing_first_name'].' '.$data['billing_last_name'].'<br>Address:'.$data['billing_address_1'].' '.$data['billing_address_2'].' '.$data['billing_city'].' '.$data['billing_postcode'].'<br>Phone:'.$data['billing_phone'].' <br>Email: '.$data['billing_email'].'<br>';
// We can use wp_mail with POST value for sending mail instead of update_option function
wp_mail($to, $subject, $mail_body, $headers);
}
}
add_action( 'woocommerce_checkout_process', 'tkd_add_checkout_checkbox_warning' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment