Gift discount coupon on every complete order WooCommerce

Last edited:
November 7, 2023
Reading time:
9 mins
Topics:
Checkout, Coupons

/

Blog

/

WooCommerce

/

Gift discount coupon on e...

💡We may earn a commision if you subscribe to a service from a link on this page.

E-commerce businesses often seek innovative ways to improve customer retention and increase sales. One effective strategy is to offer gift discount coupons to customers upon completing an order in WooCommerce. This approach not only shows appreciation to your customers but also incentivizes them to return and make additional purchases.

In this comprehensive guide, we will explore the benefits of gifting after every order, compare the use of a lightweight code snippet versus plugins, and provide step-by-step instructions to implement this feature by adding code to your child theme’s functions.php file.

 

Why Gifting After Every Order Is Beneficial

Enhanced Customer Loyalty

When you reward customers with a discount coupon after they complete an order, you demonstrate that their business is valued. This gesture can significantly enhance customer loyalty, as it makes shoppers feel appreciated and more likely to return for future purchases.

Increased Customer Retention

Offering gift discount coupons can be an effective retention strategy. These coupons encourage customers to return and redeem them, thus driving repeat business. As a result, you can increase the lifetime value of each customer.

Boosted Sales

By providing customers with a discount coupon, you’re essentially incentivizing them to make another purchase. Whether it’s a percentage-based or fixed amount discount, this offer can lead to higher conversion rates and increased sales.

Positive Brand Perception

Customers who receive gift discount coupons often have a more positive perception of your brand. This not only leads to repeat business but also improves your brand’s reputation through word-of-mouth and referrals.

Lightweight Snippet vs. Plugins

Using a Lightweight Code Snippet

One way to implement the gift discount coupon feature is by using a lightweight code snippet. This method offers several advantages:

  • Performance: Code snippets are lightweight and don’t add overhead to your site’s performance.
  • Customization: You have full control over the coupon’s rules and conditions.
  • Cost-Effective: Implementing a code snippet is a cost-effective solution, as you won’t need to invest in premium plugins.

Plugins for Advanced Features

While code snippets work well for basic implementations, some businesses may require more advanced features and capabilities. In such cases, WooCommerce plugins specifically designed for this purpose can be advantageous. They often come with features such as:

  • Scheduled Coupons: Plugins can automate the coupon generation and delivery process.
  • Advanced Conditions: You can set intricate rules for coupon usage.
  • Reporting and Analytics: Plugins may provide detailed insights into coupon performance.

The choice between using a code snippet or a plugin depends on your specific needs and technical expertise. In this guide, we’ll focus on implementing a code snippet for its simplicity and cost-effectiveness.

Instructions to Add Code in Child Theme functions.php

To gift a discount coupon to customers upon completing an order in WooCommerce, you can follow these step-by-step instructions. Make sure to create a backup of your website before making any changes to the code.

  1. Access Your Website Files: Use an FTP client or a file manager in your hosting control panel to access your website’s files.
  2. Locate Your Child Theme: Navigate to the wp-content/themes directory, where you should find a folder for your child theme. If you don’t have a child theme, create one to avoid losing your customizations during theme updates.
  3. Edit functions.php: Inside your child theme folder, locate the functions.php file. This is where you’ll add the code snippet to gift discount coupons.
  4. Add the Code Snippet: Open the functions.php file with a text editor. You can now add the code snippet that generates discount coupons. The code should look something like this:
    // Add a custom coupon for first-time customers
    add_action('woocommerce_thankyou', 'generate_coupon_for_first_order');
    
    function generate_coupon_for_first_order($order_id) {
        // Retrieve the order and customer data
        $order = wc_get_order($order_id);
    
        // Retrieve the customer's email
        $email = sanitize_email( $order->get_billing_email() );
    $coupon_generated = $order->get_meta('coupon_generated');
        if ($coupon_generated) {
            return;
        }
        // Check if the billing email has previous orders, uncomment for first order only
       /* $customer_orders = wc_get_orders(array(
            'customer' => $email,
            'status'   => array('completed', 'processing'),
            'limit'    => 2,
        ));
    
        // If the customer has more than 1 previous order, exit
        if (count($customer_orders) > 1) {
            return;
        }*/
    
        // Generate a unique coupon code
        $coupon_code = 'FIRSTORDER' . uniqid();
    
        // Create a new coupon object
        $coupon = new WC_Coupon();
        $coupon->set_code( sanitize_text_field( $coupon_code ) );
        $coupon->set_discount_type( 'percent' );
        $coupon->set_amount( '5' );
        $coupon->set_individual_use( true );
        $coupon->set_usage_limit( 1 );
    
        // Save the coupon
        $coupon->save();
    
    	$order->update_meta_data( 'generated_coupon_code', sanitize_text_field( $coupon_code ) );
    	$order->update_meta_data('coupon_generated', true);
    	//update_post_meta( $order_id, '_generated_coupon_code', sanitize_text_field( $coupon_code ) );
    	echo 'untilhere';
    	echo $coupon_code;
        $order->save();
    
        // Prepare the email subject and content
            $subject = 'Thank you for your order and here is your coupon!';
            $message = '🎉 Thank you for your order! 🎉' . "\n\n";
            $message .= 'We appreciate your business and hope you enjoy your purchase.' . "\n\n";
            $message .= 'As a token of our appreciation, please find your coupon code below:' . "\n\n";
            $message .= 'Coupon Code: ' . $coupon_code . "\n\n";
            $message .= 'This coupon code provides a 5% discount on your next purchase.' . "\n\n";
            $message .= 'If you have any questions or need further assistance, feel free to reach out to us.' . "\n\n";
            $message .= '🎉 Have a wonderful day! 🎉';
    
            // Get the email recipient
            $email = $order->get_billing_email();
    
            // Send the custom email
            wp_mail($email, $subject, $message);
    }
    /**
     * Display field value on the order edit page
     */
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_display_admin_order_meta', 10, 1 );
    
    function my_custom_display_admin_order_meta($order){
    	echo '

    '.__('Coupon Code').': ' . $order->get_meta('generated_coupon_code') . '

    '; }

    This code sets up a coupon with the code ‘FIRST0RDER_123456789’, which provides a 5% discount on orders over $50.

  5. Save the File: Save the functions.php file after adding the code.
  6. Test the Coupon: Place a test order to ensure the coupon is applied correctly upon order completion.
  7. Customize as Needed: You can customize the coupon code, discount type, amount, and minimum order amount to match your specific requirements.
  8. Ensure Compatibility: Check for compatibility with your theme and plugins to confirm that the coupon feature works seamlessly.
  9. Monitor Performance: Keep an eye on how the coupon affects your sales, customer retention, and overall website performance.

Share gifts to your customers!

By offering gift discount coupons to customers upon completing an order in WooCommerce, you can significantly improve customer loyalty, retention, and overall sales. This thoughtful gesture not only shows appreciation to your customers but also incentivizes them to return for more purchases. Whether you choose to use a lightweight code snippet or a dedicated plugin, implementing this feature can have a positive impact on your e-commerce business. Customization options are vast, allowing you to tailor your coupons to best fit your business goals and customer preferences.

Leave a Reply

Your email address will not be published. Required fields are marked *