DEV Community

Cover image for How to Boost Customer Loyalty with Automatic Discount Codes in WooCommerce
Ali Karbasi
Ali Karbasi

Posted on • Updated on

How to Boost Customer Loyalty with Automatic Discount Codes in WooCommerce

Customer retention is critical for long-term business growth, and one efficient strategy to encourage repeat purchases is to provide discount codes for future sales. This article will explore how to use automatic discount code generation in WooCommerce to enhance client loyalty and sales.

Implementing Automatic Discount Codes

Adding automatic discount codes to your WooCommerce online store is a straightforward process. By inserting the following code into your theme’s functions.php file, you can automatically generate a unique discount code when a customer completes a purchase. This code can then be used by the customer on their next order.
Here’s the code to achieve this:

/**
 * Automatic Discount Codes in WooCommerce
 * Visit AliKarbasi.dev for more codes.
**/

add_action('woocommerce_order_status_processing', 'create_discount_code_with_order_id', 10, 1);

function create_discount_code_with_order_id($order_id) {
    $order = wc_get_order($order_id);
    $email = $order->get_billing_email();
    // Order number with "solo" prefix as discount code
    $coupon_code = 'solo' . $order_id;
    // Discount percentage
    $amount = '25';
    // Discount type
    $discount_type = 'percent';

    // Check if the discount code with this order number and prefix already exists
    if (get_page_by_title($coupon_code, OBJECT, 'shop_coupon')) {
        return;
    }

    $coupon = array(
        'post_title' => $coupon_code,
        'post_content' => '',
        'post_status' => 'publish',
        'post_author' => 1,
        'post_type' => 'shop_coupon'
    );

    $new_coupon_id = wp_insert_post($coupon);

    // Discount code settings
    update_post_meta($new_coupon_id, 'discount_type', $discount_type);
    update_post_meta($new_coupon_id, 'coupon_amount', $amount);
    update_post_meta($new_coupon_id, 'individual_use', 'yes');
    update_post_meta($new_coupon_id, 'usage_limit', '1');
    //Set discount code to expire after 3 months
    update_post_meta($new_coupon_id, 'expiry_date', date('Y-m-d', strtotime('+90 days')));
    update_post_meta($new_coupon_id, 'customer_email', array($email));
    update_post_meta($new_coupon_id, 'usage_limit_per_user', '1');
    update_post_meta($new_coupon_id, 'minimum_amount', '50');
    update_post_meta($new_coupon_id, 'product_ids', '');

    // Save the discount code as order meta data for future use
    update_post_meta($order_id, '_discount_coupon_code', $coupon_code);
}
Enter fullscreen mode Exit fullscreen mode

Benefits of Automatic Discount Codes

Giving consumers a discount code for subsequent purchases encourages them to stay loyal to your business. This may be a crucial tactic in preserving enduring connections with your clients.
Having automatic discount codes makes buying more personalized for the individual. Consumers like the gesture of a discount since it makes them feel important and may improve their opinion of your brand.

Best Practices for Implementation

When implementing this feature, it is advisable to:

  • Use a Child Theme: Insert the code into your theme’s child theme rather than the main theme. This ensures that your changes are preserved even after theme updates.
  • Customize Messaging: Tailor the discount code message sent to customers to align with your brand’s tone and enhance their experience.

Conclusion

Using WooCommerce to automatically generate discount codes is a straightforward but powerful way to increase sales and foster client loyalty. You can quickly add this feature to your WordPress website and give your consumers an extra reason to come back and make more purchases by following the above-described procedures. This minor adjustment can have a big impact on customer retention and overall company expansion.

Happy coding :D

Top comments (0)

The discussion has been locked. New comments can't be added.