Last updated on October 18, 2023

WooCommerce email marketing integration

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Integrate email marketing with WooCommerce for better outreach.

To integrate WooCommerce with email marketing services, you can use the woocommerce_email_recipient_ filter hook to add additional email recipients for specific WooCommerce emails. This can be useful if you want to send order notifications or other WooCommerce emails to your email marketing service.

Here’s an example code snippet that demonstrates how to add an additional email recipient for the “New Order” email:

function wpsnippets_add_email_recipient( $recipient, $order ) {
    $additional_recipient = 'marketing@example.com';
    $recipient .= ',' . $additional_recipient;
    return $recipient;
}
add_filter( 'woocommerce_email_recipient_new_order', 'wpsnippets_add_email_recipient', 10, 2 );

In this example, the wpsnippets_add_email_recipient function takes two parameters: $recipient (the original email recipient) and $order (the WooCommerce order object). It adds an additional email recipient ($additional_recipient) by appending it to the original $recipient using a comma separator.

The add_filter function is used to hook into the woocommerce_email_recipient_new_order filter, which is specific to the “New Order” email. The priority is set to 10, and the number of accepted parameters is 2.

You can modify this code snippet to add additional email recipients for other WooCommerce emails by changing the filter hook and adjusting the logic inside the wpsnippets_add_email_recipient function.

This code snippet can be useful when you want to automatically send order notifications or other WooCommerce emails to your email marketing service, allowing you to keep your customer data synchronized and easily manage your email marketing campaigns.

Examples

Example 1: Adding a Custom Field to WooCommerce Order Emails

This use case demonstrates how to add a custom field to WooCommerce order emails. By adding a custom field, you can collect additional information from customers during the checkout process and include it in the order confirmation email.

function wpsnippets_add_custom_field_to_order_email( $order, $sent_to_admin, $plain_text, $email ) {
    $custom_field = get_post_meta( $order->get_id(), 'custom_field', true );
    if ( $custom_field ) {
        echo '<p><strong>Custom Field:</strong> ' . $custom_field . '</p>';
    }
}
add_action( 'woocommerce_email_order_details', 'wpsnippets_add_custom_field_to_order_email', 10, 4 );

This code adds a custom field to the WooCommerce order email template. The wpsnippets_add_custom_field_to_order_email function retrieves the value of the custom field from the order using get_post_meta and displays it in the order email using echo. The custom field is only displayed if it has a value.

Example 2: Sending WooCommerce Order Data to an Email Marketing Service

This use case demonstrates how to send WooCommerce order data to an email marketing service. By integrating with an email marketing service, you can automatically add customers to your email list and send them targeted marketing campaigns based on their purchase history.

function wpsnippets_send_order_data_to_email_marketing_service( $order_id ) {
    $order = wc_get_order( $order_id );
    $customer_email = $order->get_billing_email();
    $order_total = $order->get_total();

    // Send order data to email marketing service
    // Code for integrating with the email marketing service goes here
}
add_action( 'woocommerce_thankyou', 'wpsnippets_send_order_data_to_email_marketing_service' );

This code sends WooCommerce order data to an email marketing service after a successful purchase. The wpsnippets_send_order_data_to_email_marketing_service function retrieves the customer’s email address and the order total from the order object using appropriate methods. You can then use this data to integrate with your preferred email marketing service and send the order data for further processing.

Example 3: Adding a Subscription Checkbox to WooCommerce Checkout

This use case demonstrates how to add a subscription checkbox to the WooCommerce checkout page. By adding a subscription checkbox, you can give customers the option to subscribe to your email marketing list during the checkout process.

function wpsnippets_add_subscription_checkbox_to_checkout() {
    woocommerce_form_field( 'subscribe', array(
        'type'          => 'checkbox',
        'class'         => array('form-row-wide'),
        'label'         => __('Subscribe to our newsletter', 'woocommerce'),
        'required'      => false,
    ), WC()->checkout->get_value( 'subscribe' ));
}
add_action( 'woocommerce_after_order_notes', 'wpsnippets_add_subscription_checkbox_to_checkout' );

function wpsnippets_save_subscription_checkbox( $order_id ) {
    if ( isset( $_POST['subscribe'] ) ) {
        update_post_meta( $order_id, 'subscribe', 'yes' );
    }
}
add_action( 'woocommerce_checkout_update_order_meta', 'wpsnippets_save_subscription_checkbox' );

This code adds a subscription checkbox to the WooCommerce checkout page. The wpsnippets_add_subscription_checkbox_to_checkout function uses the woocommerce_form_field function to add a checkbox field with the label “Subscribe to our newsletter”. The wpsnippets_save_subscription_checkbox function saves the subscription status as post meta when the order is placed, based on the value of the checkbox.

Last updated on October 18, 2023. Originally posted on December 18, 2023.

Leave a Reply