How to Add a Custom Text Input Metabox to SureCart Products (SureCart 3.0+)

If you’re using SureCart 3.0 or later, you might have noticed that SureCart Products (sc_product) are now stored as a WordPress Custom Post Type.
This opens up exciting possibilities — you can easily add custom fields to your products using WordPress Metaboxes!

In this tutorial, we’ll walk you through adding a simple custom text input field to the SureCart Product edit screen.


Why Add a Custom Text Field to SureCart Products?

Adding a custom metabox can help you:

  • Collect extra product information
  • Add custom pricing notes
  • Store internal product IDs or tags
  • Customize your SureCart workflows

Best of all, it’s easy and uses native WordPress features!


Step 1: Register the Metabox

First, hook into the add_meta_boxes action and register your new metabox for the sc_product post type.

add_action( 'add_meta_boxes', 'register_surecart_custom_metabox' );

function register_surecart_custom_metabox() {
    add_meta_box(
        'surecart_custom_text_field',             // Metabox ID
        esc_html__( 'Custom Product Info', 'your-textdomain' ), // Title
        'render_surecart_custom_metabox',          // Callback function
        'sc_product',                              // Post type
        'normal',                                  // Context (normal/side/advanced)
        'default'                                  // Priority
    );
}

Step 2: Create the Metabox Callback Function

This function will output the HTML for the text input field inside the metabox.

function render_surecart_custom_metabox( $post ) {
    // Add a nonce field for security
    wp_nonce_field( 'surecart_custom_text_nonce_action', 'surecart_custom_text_nonce' );

    // Retrieve existing value from database
    $custom_text = get_post_meta( $post->ID, '_surecart_custom_text', true );

    ?>
    <p>
        <label for="surecart_custom_text"><?php esc_html_e( 'Custom Text', 'your-textdomain' ); ?></label>
        <input type="text" id="surecart_custom_text" name="surecart_custom_text" value="<?php echo esc_attr( $custom_text ); ?>" style="width:100%;" />
    </p>
    <?php
}

Step 3: Save the Custom Field Value

Now, save the field value when the product is updated.

add_action( 'save_post', 'save_surecart_custom_metabox_data' );

function save_surecart_custom_metabox_data( $post_id ) {
    // Verify nonce
    if ( ! isset( $_POST['surecart_custom_text_nonce'] ) || ! wp_verify_nonce( $_POST['surecart_custom_text_nonce'], 'surecart_custom_text_nonce_action' ) ) {
        return;
    }

    // Check autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    // Check user permission
    if ( isset( $_POST['post_type'] ) && 'sc_product' === $_POST['post_type'] ) {
        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return;
        }
    }

    // Sanitize and save the field
    if ( isset( $_POST['surecart_custom_text'] ) ) {
        update_post_meta( $post_id, '_surecart_custom_text', sanitize_text_field( $_POST['surecart_custom_text'] ) );
    }
}

Here’s the full code all in one place — clean, optimized, and ready to add a custom text input metabox to SureCart products (sc_product).

You can either paste this into your theme’s functions.php file or build it into a small plugin.

🎯 Full Code to Add a Custom Text Input Metabox to SureCart Products

<?php
/**
 * Plugin Name: SureCart Product Custom Text Metabox
 * Description: Adds a simple custom text field to SureCart products.
 * Version: 1.0
 * Author: Your Name
 * Text Domain: your-textdomain
 */

// Hook to add metabox
add_action( 'add_meta_boxes', 'register_surecart_custom_metabox' );

/**
 * Register the custom metabox for SureCart products.
 */
function register_surecart_custom_metabox() {
    add_meta_box(
        'surecart_custom_text_field',             // Metabox ID
        esc_html__( 'Custom Product Info', 'your-textdomain' ), // Title
        'render_surecart_custom_metabox',          // Callback function
        'sc_product',                              // Post type
        'normal',                                  // Context (normal/side/advanced)
        'default'                                  // Priority
    );
}

/**
 * Render the custom metabox content.
 *
 * @param WP_Post $post The current post object.
 */
function render_surecart_custom_metabox( $post ) {
    // Add a nonce field for security
    wp_nonce_field( 'surecart_custom_text_nonce_action', 'surecart_custom_text_nonce' );

    // Retrieve existing value
    $custom_text = get_post_meta( $post->ID, '_surecart_custom_text', true );

    ?>
    <p>
        <label for="surecart_custom_text"><?php esc_html_e( 'Custom Text', 'your-textdomain' ); ?></label>
        <input type="text" id="surecart_custom_text" name="surecart_custom_text" value="<?php echo esc_attr( $custom_text ); ?>" style="width:100%;" />
    </p>
    <?php
}

/**
 * Save the metabox data when the post is saved.
 *
 * @param int $post_id The ID of the post being saved.
 */
function save_surecart_custom_metabox_data( $post_id ) {
    // Verify nonce
    if ( ! isset( $_POST['surecart_custom_text_nonce'] ) || ! wp_verify_nonce( $_POST['surecart_custom_text_nonce'], 'surecart_custom_text_nonce_action' ) ) {
        return;
    }

    // Check autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    // Check permissions
    if ( isset( $_POST['post_type'] ) && 'sc_product' === $_POST['post_type'] ) {
        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return;
        }
    }

    // Save or update the custom field
    if ( isset( $_POST['surecart_custom_text'] ) ) {
        $custom_text = sanitize_text_field( wp_unslash( $_POST['surecart_custom_text'] ) );
        update_post_meta( $post_id, '_surecart_custom_text', $custom_text );
    }
}

// Hook to save metabox data
add_action( 'save_post', 'save_surecart_custom_metabox_data' );

?>

✅ What this Code Does:

  • Adds a “Custom Product Info” metabox on the SureCart product edit screen.
  • Allows store admins to input a text value.
  • Saves the value safely to post meta when the product is updated.
  • Secured with nonce verification, autosave check, and permission validation.

💡 Bonus: How to Display This Custom Text on Product Page

If you want to show this custom field on the frontend (like in your product template), use:

<?php
$custom_text = get_post_meta( get_the_ID(), '_surecart_custom_text', true );

if ( ! empty( $custom_text ) ) {
    echo '<p class="surecart-custom-text">' . esc_html( $custom_text ) . '</p>';
}
?>

Conclusion

Adding a custom text input metabox for SureCart products is simple, powerful, and fully WordPress-native.
With this setup, you can extend your SureCart store exactly how you want.

If you enjoyed this tutorial, don’t forget to bookmark it! 🚀

Leave a Reply

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


Review Your Cart
0
Add Coupon Code
Subtotal