This code adds a date picker to the Edit Product screen enabling the storeowner to add an expiration date to any single product in WooCommerce. The expiration date is then added to order emails along with the product name and date of purchase.
The date picker only displays if the checkbox is checked.
The data is then displayed on order emails like this :
Code Installation
There’s only 1 step :
Copy and paste the code to the end of your child themes functions file or custom/functionality plugin.
How The Functions Work
Certainly, let’s provide concise explanations for each function without including the code snippets:
Enqueue Datepicker Scripts and Styles
This function enqueues the jQuery UI Datepicker script and its associated CSS file, ensuring they are loaded in the WordPress admin area. Modify URLs or add additional scripts/styles as needed.
Save Custom Meta Box Data
This function saves custom meta box data for a product, checking if the “Add Expiration Date?” checkbox is checked. It saves ‘yes’ to ‘_add_expiration_date’ and the entered expiration date to ‘_expiration_date’. Modify keys or add more fields as needed.
Add Custom Fields to Product Data General Tab
This function adds custom fields (e.g., “Add Expiration Date?” checkbox and datepicker) to the Product Data General tab. Modify the structure of fields, add more, or rearrange their order based on preferences.
Display Custom Fields on Product Page
This function displays custom fields (product name, date of purchase, and expiration date) on the single product page. Customize the HTML structure or modify displayed fields based on design preferences.
Add Custom Fields to Order Email
This function adds custom fields to the order email, checking if the “Add Expiration Date?” checkbox is checked for each product. Modify displayed information or customize the HTML structure of the email.
Modifying/Extending The Code
The code snippets are modular, allowing easy modification or extension based on specific requirements.
If you need to add more fields, change labels, or adjust display logic, you can do so by understanding each function’s structure and making necessary modifications.
Always make backups and test changes in a safe environment.
Reviews