Skip to content

Instantly share code, notes, and snippets.

@voboghure-dev
Last active February 23, 2021 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save voboghure-dev/28025cf9fd11355ab76de22ff222b23e to your computer and use it in GitHub Desktop.
Save voboghure-dev/28025cf9fd11355ab76de22ff222b23e to your computer and use it in GitHub Desktop.
Use a checkbox in checkout page to chose customer send a mail for subscription
/**
* Add WooCommerce Checkbox checkout
*/
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' => 'Ok I agree with <a href="/checkout-checkbox" target="_blank" rel="noopener">Checkout Checkbox Page</a>', // 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 update in option table.
if ( (int) isset( $_POST['checkout-checkbox'] ) ) {
// update_option('CheckoutPage', $_POST);
$to = 'voboghure.dev@gmail.com';
$subject = 'Test email from checkout';
$msg = '';
foreach($_POST as $key => $value) {
$msg .= $key . ' : ' . $value . '<br>';
}
wp_mail($to, $subject, $msg);
// We can use wp_mail with POST value for sending mail instead of update_option function
}
}
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