How to Create Custom Date-Specific WooCommerce Product Badges?

Adding custom fields to your WooCommerce products on a WordPress site can greatly enhance your online store’s functionality and user experience. If you’re not very tech-savvy, don’t worry! Here’s a straightforward guide on how to add custom fields like “Badge Text,” “Display Start Date,” and “Display End Date” using the Code Snippets plugin. This way, you can manage special promotions or display unique messages on your product pages without touching a single line of code directly in your theme files.

What You’ll Need

  • A WordPress site with WooCommerce installed.
  • The Code Snippets plugin is installed on your WordPress site.

Video: How to Create Custom Date-Specific WooCommerce Product Badges?

1. Install the Code Snippets Plugin

First off, if you haven’t already installed the Code Snippets plugin, here’s how to do it:

  • Go to your WordPress dashboard.
  • Navigate to Plugins > Add New.
  • Search for Code Snippets.
  • Click Install Now and then activate the plugin.

Why Use Code Snippets?

Using the Code Snippets plugin is a safe way to add custom code to your WordPress site. It prevents any potential errors that might break your site if you were to directly edit the theme files. Plus, it’s super convenient to manage all your custom code snippets in one place!

2. Create a New Snippet

With the Code Snippets plugin active, you’re going to create a new snippet that will add these custom fields to your products:

  • In your WordPress dashboard, go to Snippets > Add New.
  • You’ll see a field to enter a title for your snippet. Name it something relevant like “Add Custom Fields to WooCommerce Products”.

3. Copy and Paste the Code

Now, in the large text area below the title:

Copy and paste the entire code you provided here below. This code is what tells WooCommerce to add new fields for badge text and start/end dates, and also how to save and display these fields on your products.

// Add custom fields to WooCommerce backend under General tab
add_action( 'woocommerce_product_options_general_product_data', 'wpsh_add_text_field_below_sale_price' );
function wpsh_add_text_field_below_sale_price() {
    global $product_object;
    
    // Original custom field
    woocommerce_wp_text_input( array(
        'id'            => '_badge',
        'label'         => __( 'Badge text', 'woocommerce' ),
        'description'   => __( 'Enter your pduct badge text here', 'woocommerce' ),
        'desc_tip'      => 'true',
        'type'          => 'text',
        'value'         => $product_object->get_meta( '_badge' ),
    ) );
    
    // Start date field
    woocommerce_wp_text_input( array(
        'id'                => '_start_date',
        'label'             => __( 'Dislay start date', 'woocommerce' ),
        'placeholder'       => 'YYYY-MM-DD',
        'description'       => __( 'Enter the start date of the badge display', 'woocommerce' ),
        'type'              => 'date',
        'value'             => $product_object->get_meta( '_start_date' ),
        'custom_attributes' => array(
            'pattern'   => '\d{4}-\d{2}-\d{2}',
        ),
    ) );
    
    // End date field
    woocommerce_wp_text_input( array(
        'id'                => '_end_date',
        'label'             => __( 'Display end date', 'woocommerce' ),
        'placeholder'       => 'YYYY-MM-DD',
        'description'       => __( 'Enter the start date of the badge displayv', 'woocommerce' ),      
        'type'              => 'date',
        'value'             => $product_object->get_meta( '_end_date' ),
        'custom_attributes' => array(
            'pattern'   => '\d{4}-\d{2}-\d{2}',
        ),
    ) );
}

// Save custom field values including dates for all product types
add_action( 'woocommerce_admin_process_product_object', 'wpsh_save_field', 10, 1 );
function wpsh_save_field( $product ) {
    if ( isset( $_POST['_badge'] ) ) {        
        $product->update_meta_data( '_badge', sanitize_text_field( $_POST['_badge'] ) );
    }
    if ( isset( $_POST['_start_date'] ) ) {
        $product->update_meta_data( '_start_date', sanitize_text_field( $_POST['_start_date'] ) );
    }
    if ( isset( $_POST['_end_date'] ) ) {
        $product->update_meta_data( '_end_date', sanitize_text_field( $_POST['_end_date'] ) );
    }
    $product->save();
}

// Adjust display logic on single product and archive pages
function wpsh_should_display_custom_field($start_date, $end_date, $text) {
    $current_date = date('Y-m-d');
    if (empty($start_date) && empty($end_date) && !empty($text)) {
        return true;
    } elseif (!empty($start_date) && empty($end_date) && $current_date >= $start_date) {
        return true;
    } elseif (empty($start_date) && !empty($end_date) && $current_date <= $end_date) {
        return true;
    } elseif (!empty($start_date) && !empty($end_date) && $current_date >= $start_date && $current_date <= $end_date) {
        return true;
    }
    return false;
}

add_action( 'woocommerce_before_add_to_cart_form', 'wpsh_display_on_single_product_page', 1 );
function wpsh_display_on_single_product_page() {
    global $product;
    if ( is_a( $product, 'WC_Product' ) ) {
        $text = $product->get_meta( '_badge' );
        $start_date = $product->get_meta( '_start_date' );
        $end_date = $product->get_meta( '_end_date' );
        if ( wpsh_should_display_custom_field($start_date, $end_date, $text) ) {
            echo '<div class="woocommerce-message"> ' . $text . '</div>';
        }
    }
}

add_action( 'blocksy:woocommerce:product-card:title:after', 'wpsh_display_on_archive_page', 10 );
function wpsh_display_on_archive_page() {
    global $product;
    if ( is_a( $product, 'WC_Product' ) ) {
        $text = $product->get_meta( '_badge' );
        $start_date = $product->get_meta( '_start_date' );
        $end_date = $product->get_meta( '_end_date' );
      if (wpsh_should_display_custom_field($start_date, $end_date, $text)) {
    echo '<div class="custom-text"> ' . $text . '</div>';
}
}
}

4. Review and Activate

Before activating the snippet, make sure everything looks right. There’s a description box below the code area where you can note what this snippet does (though this is optional). When ready:

  • Click the Save Changes and Activate button at the bottom of the page.

5. Test It Out

Go to any product in your WooCommerce store (Products > All Products > Edit a product), and scroll down to the Product Data section. Under the General tab, you should now see your new fields for badge text and dates.

This is the result:

6. Adding Dates and Badges

When editing or adding a new product:

  • You can enter a badge text that might say something like “New Arrival” or “Limited Edition”.
  • Set a start and an end date for when this badge should be displayed on the product page.

Wrap-Up

And that’s pretty much it! You’ve now added custom fields to your WooCommerce products without any complex coding. Go ahead and give your products that personal touch with custom badge texts and date-specific notes. If you ever need to turn off these features temporarily, you can simply deactivate the snippet in the Code Snippets dashboard. Simple as that!

If you have any questions or need further assistance, don’t hesitate to reach out or consult the detailed documentation available for the Code Snippets plugin and WooCommerce. Happy selling!

Here are some of my favorite WordPress tools

Thanks for reading this article! I hope it's been useful as you work on your own websites and e-commerce sites. I wanted to share some tools I use as a WordPress developer, and I think you'll find them helpful too.

Just so you know, these are affiliate links. If you decide to use any of them, I'll earn a commission. This helps me create tutorials and YouTube videos. But honestly, I genuinely use and recommend these tools to my friends and family as well. Your support keeps me creating content that benefits everyone.

Themes: Over the past few years, I've consistently relied on two primary themes for all sorts of projects: the Blocksy theme and the Kadence Theme. If you explore this website and my YouTube channel, you'll come across numerous tutorials that delve into these themes. If you're interested in obtaining a 10% discount for both of these themes, then:

Code Snippets Manager: WPCodeBox allows you to add code snippets to your site. Not only that, but it also provides you with the capability to construct and oversee your WordPress Code Snippets library right in the cloud. You can grab it with the 20% discount here (SAVE 20% Coupon: WPSH20).

Contact forms: There are hundreds of contact forms out there but Fluent Forms is the one I like the most. If you need a 20% discount then use this link (save 20% coupon is WPSH20).

Gutenberg add-ons: If I need a good Gutenberg blocks add-on then Kadence Blocks is the one I have used the most. You’ll get a 10% discount with the coupon SIMPLEHACKS here.

Website migration: While building a website you probably need a good plugin that can help you with the migration, backups, restoration, and staging sites. Well, WpVivid is the one I have used for the last couple of years. If you use this link along with the WPSH20 coupon you’ll get a 20% discount.

Woocommerce extensions: There are a bunch of Woocommerce extensions that I like but the one that stands out is Advanced Dynamic Pricing. Once again, you’ll get a 20% discount if you use this link here (save 20% coupon is WPSH20)

Web Hosting: If you would like to have a really fast and easy-to-use managed cloud hosting, then I recommend Verpex Hosting (see my review here). By the way, this site is hosted in Verpex.)

To see all my most up-to-date recommendations, check out this resource that I made for you!

Do you want to thank me and buy me a beer?

Every donation is entirely welcome but NEVER required. Enjoy my work for free but if you would like to thank me and buy me a beer or two then you can use this form here below.

Donation Form (#2)

Janek T.
Janek T.

Improve this text: {CLIPBOARD}

- I have been passionate about Wordpress since 2011, creating websites and sharing valuable tips on using Wordpress and Woocommerce on my site.
- Be the first to receive notifications about new tutorials by subscribing to my Youtube channel .
- Follow me on Twitter here

Articles: 116