The code snippet provided below demonstrates how to implement WooCommerce abandoned cart recovery functionality using WordPress hooks and functions. This functionality can be useful for e-commerce websites built with WooCommerce to recover potential sales by sending reminder emails to customers who have added items to their cart but did not complete the purchase.
/**
* Schedule abandoned cart recovery email
*/
function wpsnippets_schedule_abandoned_cart_email( $order_id ) {
// Get the order object
$order = wc_get_order( $order_id );
// Get the customer email
$customer_email = $order->get_billing_email();
// Schedule the email to be sent after a specific time period
wp_schedule_single_event( time() + 1 * HOUR_IN_SECONDS, 'wpsnippets_send_abandoned_cart_email', array( $customer_email ) );
}
add_action( 'woocommerce_checkout_order_processed', 'wpsnippets_schedule_abandoned_cart_email' );
/**
* Send abandoned cart recovery email
*/
function wpsnippets_send_abandoned_cart_email( $customer_email ) {
// Create the email subject
$subject = 'Reminder: You have items in your cart';
// Create the email content
$message = 'Dear customer, you have items in your cart. Please complete your purchase by clicking the link below: ' . esc_url( wc_get_cart_url() );
// Send the email
wp_mail( $customer_email, $subject, $message );
}
add_action( 'wpsnippets_send_abandoned_cart_email', 'wpsnippets_send_abandoned_cart_email' );
In this code snippet, we have two functions. The first function wpsnippets_schedule_abandoned_cart_email
is hooked to the woocommerce_checkout_order_processed
action, which is triggered when an order is processed during the checkout. This function retrieves the customer email from the order object and schedules the wpsnippets_send_abandoned_cart_email
function to be executed after a specific time period (in this case, 1 hour).
The second function wpsnippets_send_abandoned_cart_email
is responsible for sending the actual abandoned cart recovery email. It takes the customer email as a parameter and uses the wp_mail
function to send an email with a subject and message. In this example, the subject is set to “Reminder: You have items in your cart” and the message includes a personalized greeting and a link to the customer’s cart.
By implementing these functions and hooks, you can automatically send reminder emails to customers who have abandoned their carts, increasing the chances of recovering those sales.
Examples
Example 1: Sending an email reminder for abandoned carts
This use case demonstrates how to send an email reminder to customers who have abandoned their carts in WooCommerce. The code example uses the woocommerce_cart_is_empty
and woocommerce_before_checkout_form
hooks to check if the cart is empty and display a notice to the customer. It also uses the woocommerce_order_status_pending
hook to send an email reminder when the order status is set to pending.
function wpsnippets_send_abandoned_cart_email( $order_id ) {
$order = wc_get_order( $order_id );
$email = $order->get_billing_email();
// Send abandoned cart reminder email to the customer
wp_mail( $email, 'Reminder: Complete your purchase', 'Please complete your purchase within 24 hours.' );
}
add_action( 'woocommerce_order_status_pending', 'wpsnippets_send_abandoned_cart_email' );
In this code example, we define a custom function wpsnippets_send_abandoned_cart_email
that accepts the $order_id
parameter. Inside the function, we retrieve the order object using wc_get_order
and get the customer’s email address. Then, we use the wp_mail
function to send an email reminder to the customer.
Example 2: Adding a countdown timer to the cart page
This use case demonstrates how to add a countdown timer to the WooCommerce cart page to create a sense of urgency and encourage customers to complete their purchase. The code example uses the woocommerce_before_cart
hook to add the countdown timer HTML markup and JavaScript code.
function wpsnippets_add_cart_countdown_timer() {
echo '<div id="countdown-timer"></div>';
// Output JavaScript code to initialize the countdown timer
echo '<script>
jQuery(function($) {
var countdownDate = new Date();
countdownDate.setMinutes(countdownDate.getMinutes() + 30); // Set countdown time to 30 minutes from now
$("#countdown-timer").countdown(countdownDate, function(event) {
$(this).text(event.strftime("%H:%M:%S")); // Format the countdown timer
});
});
</script>';
}
add_action( 'woocommerce_before_cart', 'wpsnippets_add_cart_countdown_timer' );
In this code example, we define a custom function wpsnippets_add_cart_countdown_timer
that outputs the HTML markup for the countdown timer and the JavaScript code to initialize it. The countdown timer is set to 30 minutes from the current time using the setMinutes
method of the Date
object. The countdown
function from the jQuery Countdown plugin is used to display and update the timer on the page.
Example 3: Offering a discount for completing an abandoned cart
This use case demonstrates how to offer a discount to customers who have abandoned their carts in WooCommerce, in order to incentivize them to complete their purchase. The code example uses the woocommerce_before_cart_totals
hook to add a discount field to the cart totals section and applies the discount when the customer clicks the “Apply Discount” button.
function wpsnippets_add_abandoned_cart_discount_field() {
echo '<tr class="cart-discount">
<th>' . __( 'Discount', 'woocommerce' ) . '</th>
<td>
<input type="text" name="cart_discount" id="cart_discount" class="input-text" placeholder="' . __( 'Enter discount code', 'woocommerce' ) . '" />
<button type="button" class="button" id="apply_discount">' . __( 'Apply Discount', 'woocommerce' ) . '</button>
</td>
</tr>';
// Apply the discount when the "Apply Discount" button is clicked
echo '<script>
jQuery(function($) {
$("#apply_discount").on("click", function() {
var discountCode = $("#cart_discount").val();
// Apply the discount code using AJAX
$.ajax({
url: "' . admin_url( 'admin-ajax.php' ) . '",
type: "POST",
data: {
action: "apply_discount",
discount_code: discountCode
},
success: function(response) {
// Handle the response from the server
console.log(response);
}
});
});
});
</script>';
}
add_action( 'woocommerce_before_cart_totals', 'wpsnippets_add_abandoned_cart_discount_field' );
function wpsnippets_apply_discount() {
$discountCode = $_POST['discount_code'];
// Apply the discount code logic here
wp_send_json_success( 'Discount applied successfully.' );
}
add_action( 'wp_ajax_apply_discount', 'wpsnippets_apply_discount' );
add_action( 'wp_ajax_nopriv_apply_discount', 'wpsnippets_apply_discount' );
In this code example, we define a custom function wpsnippets_add_abandoned_cart_discount_field
that outputs the HTML markup for the discount field and the JavaScript code to handle the discount application. When the “Apply Discount” button is clicked, an AJAX request is sent to the server with the discount code. The server-side logic for applying the discount code is handled in the wpsnippets_apply_discount
function, which sends a JSON response back to the client.