Created a code snippet to add a newsletter checkbox to my Woocommerce Checkout, does it look ok to you?

[ad_1]

The code:

​

// Add newsletter subscription checkbox to WooCommerce checkout
function add_newsletter_subscription_checkbox() {
// Get the selected payment method
$chosen_payment_method = WC()->session->get(‘chosen_payment_method’);

// Display the checkbox after the payment methods selector
if ($chosen_payment_method !== ‘newsletter_subscription’) {
echo ‘<div class=”newsletter-checkbox”>’;
echo ‘<p class=”form-row form-row-wide”>’;
woocommerce_form_field(‘newsletter_subscription’, array(
‘type’ => ‘checkbox’,
‘class’ => array(‘input-checkbox’),
‘label’ => __(‘Would you like to receive the latest news from our Store?’, ‘Store name’),
‘default’ => 0, // Set the default value to 0 (unchecked)
), false); // Set the current value to false
echo ‘</p>’;
echo ‘</div>’;
}
}

add_action(‘woocommerce_review_order_before_payment’, ‘add_newsletter_subscription_checkbox’, 10);
add_action(‘woocommerce_after_checkout_form’, ‘add_newsletter_subscription_checkbox’, 10);

// Send newsletter subscription preference via email
function send_newsletter_subscription_preference($order_id)
{
$order = wc_get_order($order_id);

// Check nonce
if (isset($_POST[‘_wpnonce’]) && wp_verify_nonce($_POST[‘_wpnonce’], ‘newsletter_subscription_nonce’)) {
// Check if the checkbox is checked
$subscription_preference = isset($_POST[‘newsletter_subscription’]) && $_POST[‘newsletter_subscription’] === ‘1’ ? ‘Yes’ : ‘No’;

// Get the customer’s email address from the order and sanitize it
$customer_email = $order->get_billing_email();
$customer_email = is_email($customer_email) ? sanitize_email($customer_email) : ”;

// Validate the checkbox value and email address
if ($subscription_preference === ‘Yes’ && !empty($customer_email)) {
$email_subject = ‘Newsletter Subscription Preference’;
$email_content = ‘Newsletter Subscription Preference: ‘ . $subscription_preference . “\n”;
$email_content .= ‘Customer Email: ‘ . $customer_email;

// Refresh nonce after processing
$new_nonce = wp_create_nonce(‘newsletter_subscription_nonce’);
update_post_meta($order_id, ‘_newsletter_subscription_nonce’, $new_nonce);

wp_mail(‘[email protected]’, $email_subject, $email_content);
}
}
}

add_action(‘woocommerce_checkout_order_processed’, ‘send_newsletter_subscription_preference’);

In simple terms, it adds a checkbox for our clients to be able to subscribe to our newsletter in our checkout, if they press it we get their email in our inbox and know we can subscribe them manually in our Newsletter solution (Which currently lacks this functionality/integration with wordpress.)

What do you think, is it secure enough?

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer