Are you looking to boost your WooCommerce sales and attract new customers? Referral coupons can be a powerful tool in your marketing arsenal. With WooCommerce, you can easily create referral coupons that come with a notice and convenient copy-paste functionality for the coupon code.
In this tutorial, we will walk you through the process of setting up referral coupons in WooCommerce. We will cover how to add a noticeable message on your product pages, informing customers about the referral coupon offer. Additionally, we will implement a copy-paste feature for the coupon code, making it simple for customers to share it with their friends and family.
By the end of this tutorial, you will have a clear understanding of how to create referral coupons, display a notice, and enable the copy-paste functionality for the coupon code in your WooCommerce store.
Let’s dive in and learn how to maximize the potential of referral coupons in WooCommerce!
To create referral coupons with notice and copy-paste functionality in WooCommerce, we will be using a combination of custom code snippets and the Code Snippets plugin. The code snippets provided in this tutorial are meant to be added to the plugin for easy management and execution.
The code consists of several functions, each serving a specific purpose. Let’s break down how they work:
The functions add_custom_product_checkbox
and add_custom_product_input_field
are responsible for adding a custom checkbox and input field to the WooCommerce product edit page, respectively. These allow you to enable referral coupons for specific products and set the discount amount.
The functions save_custom_product_checkbox
and save_custom_product_input_field
handle saving the values of the custom checkbox and input field when a product is saved or updated. The checkbox value is stored as post meta with the key enable_referral_coupon
, while the input field value is stored with the key referral_coupon_discount
.
The add_custom_css
function adds custom CSS styles to change the appearance of the referral notice, coupon code, discount amount, and expiry date. You can modify these styles to match your website’s design.
The display_single_product_notice
function is responsible for displaying the referral notice on the single product page after the add to cart form. It checks if the “Enable Referral Coupon” checkbox is checked for the product and displays the notice accordingly.
The create_referral_coupon_code_automatically_after_purchase
function is triggered on the “woocommerce_thankyou” action. It creates a referral coupon code automatically after a successful purchase. The function checks if any product in the order has the “Enable Referral Coupon” checkbox checked. If found, it generates a coupon code based on the customer’s billing first name and the discount amount set for the product. The coupon is then saved with the expiry date set to 30 days from the purchase date.
The copy_coupon_code_js
function includes a JavaScript snippet that enables the copy-paste functionality for the coupon code. When the user clicks on the coupon code, it gets copied to their clipboard, and an alert is displayed confirming the successful copy.
To get the referral coupon functionality working on your WooCommerce website, follow these steps:
Install and activate the Code Snippets plugin from the WordPress plugin repository. This plugin allows you to manage and execute custom code snippets easily.
Go to “Snippets” in your WordPress dashboard and click on “Add New.” Copy and paste each code snippet mentioned in this tutorial into separate snippets and save them.
Activate all the snippets you created by checking the corresponding checkboxes next to each snippet and clicking “Save Changes.”
Go to your WooCommerce product edit page and check the “Enable Referral Coupon” checkbox. Set the referral coupon discount amount in the input field. Save or update the product. Now, visit the single product page and confirm that the referral notice is displayed correctly. Place a test order and check if a referral coupon code is generated automatically.
Congratulations! You have successfully implemented referral coupons with notice and copy-paste functionality in WooCommerce.
Note: Make sure to customize the CSS styles provided in the add_custom_css
function to match your website’s design preferences.
Implementing referral coupons in your WooCommerce store can incentivize customers to make purchases and encourage them to refer others, leading to increased sales and customer engagement.
Stay UpTo Date with Latest Post And news:
Stay UpTo Date with Latest Post And news: