// 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 ‘

’ . $text . ‘
’;

        }

    }

}

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 ‘

’ . $text . ‘
’;

}

}

}