How To Add a New Custom Post Type in WooCommerce

If you have a unique product that doesn't quite fit into the default WooCommerce product types, like "Simple," "Variable," or "Grouped," you can create a custom product type to help you categorize and manage it more effectively. Yes, you heard that right.

Let’s walk you through the process of setting one up; don’t worry; it’s easier than it sounds!

Add New Custom Post Types in WordPress

To add or create new custom post types in WordPress, You can either write the code manually or easily install a plugin, You can find many plugins for creating custom post types and install one of them which is super easy. 

Example :

Advanced Custom Fields (ACF),

  Custom Post Type UI  

Let's talk about both methods, first have a look at some prerequisites.

Prerequisites

Before you step into adding your new customer product type, there are few things you should make sure you have done already.

WordPress & Custom CPT WooCommerce Installed: Though you must have the WordPress already installed, still for double checking keep it in your checklist. Also, install the Custom Post WooCommerce plugin.

Access to a Code Editor: You'll need to get your hands on some code, so make sure you can access your theme’s functions.php file.

Integrate a Custom Post Type With WooCommerce

Add the New Custom Post Type to WooCommerce follow the steps below

  • Install and activate the “Custom Post Type Woocommerce Integration” plugin on your WordPress site.
  • After activation, a new menu item named “WC Integration” will appear in the WordPress admin sidebar. Click on it to access the plugin settings.
  • In the plugin settings page, locate the “Select Post type” option And select your CPT.
  • Switch on to Add Default Price Field And Others Meta Fields.
  • Use shortcode :

Display price: 

[cptwooint_price/

Display Add to cart Button:

Shortcode: [cptwooint_cart_button/]

Create a Custom Post Type Manually

You do not need to worry about how you can add the new custom product type. We are here to show you the simplest way with raw codes. Simply follow the steps and copy-paste the code.

Step 1: Set Up a Child Theme or Custom Plugin

First things first, if you’re going to mess with your theme files, it’s best to create a child theme. This way, your changes don’t get wiped out when the theme updates.

Step 2: Add New Custom Post Type to the Backend of WP Site

Let’s add your new custom post type to the backend of your WordPress site. Here’s what you need to do:

Navigate to your theme directory and open the functions.php file.

Add the following code to the functions.php file.

When creating custom post types, use the init hook in add_action() and pass arguments to the register_post_type() function.

More Details about register post type : https://developer.wordpress.org/reference/functions/register_post_type/

Code: 

/**
 * Register a custom post type called "book".
 *
 * @see get_post_type_labels() for label keys.
 */
function wpdocs_codex_book_init() {
	$labels = array(
		'name'                  => _x( 'Books', 'Post type general name', 'textdomain' ),
		'singular_name'         => _x( 'Book', 'Post type singular name', 'textdomain' ),
		'menu_name'             => _x( 'Books', 'Admin Menu text', 'textdomain' ),
		'name_admin_bar'        => _x( 'Book', 'Add New on Toolbar', 'textdomain' ),
		'add_new'               => __( 'Add New', 'textdomain' ),
		'add_new_item'          => __( 'Add New Book', 'textdomain' ),
		'new_item'              => __( 'New Book', 'textdomain' ),
		'edit_item'             => __( 'Edit Book', 'textdomain' ),
		'view_item'             => __( 'View Book', 'textdomain' ),
		'all_items'             => __( 'All Books', 'textdomain' ),
		'search_items'          => __( 'Search Books', 'textdomain' ),
		'parent_item_colon'     => __( 'Parent Books:', 'textdomain' ),
		'not_found'             => __( 'No books found.', 'textdomain' ),
		'not_found_in_trash'    => __( 'No books found in Trash.', 'textdomain' ),
		'featured_image'        => _x( 'Book Cover Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'textdomain' ),
		'set_featured_image'    => _x( 'Set cover image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
		'remove_featured_image' => _x( 'Remove cover image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
		'use_featured_image'    => _x( 'Use as cover image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
		'archives'              => _x( 'Book archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'textdomain' ),
		'insert_into_item'      => _x( 'Insert into book', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'textdomain' ),
		'uploaded_to_this_item' => _x( 'Uploaded to this book', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'textdomain' ),
		'filter_items_list'     => _x( 'Filter books list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'textdomain' ),
		'items_list_navigation' => _x( 'Books list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'textdomain' ),
		'items_list'            => _x( 'Books list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'textdomain' ),
	);

	$args = array(
		'labels'             => $labels,
		'public'             => true,
		'publicly_queryable' => true,
		'show_ui'            => true,
		'show_in_menu'       => true,
		'query_var'          => true,
		'rewrite'            => array( 'slug' => 'book' ),
		'capability_type'    => 'post',
		'has_archive'        => true,
		'hierarchical'       => false,
		'menu_position'      => null,
		'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
	);

	register_post_type( 'book', $args );
}

add_action( 'init', 'wpdocs_codex_book_init' );

Step 3: Add a New WordPress Custom Post Type

To add a new "Book" post to your site:

  • Go to the Books post and type in your WordPress dashboard.
  • Click Add New.
  • Enter the title, content, and excerpt, and set a featured image.
  • Click Publish to make your new book post live.

Step 4: Publish a post for Books

To publish the books:

  • Go to Books → Add New 
  • Create a new post called Books.
  • Click Publish to save your changes.
  • Add price for the books

Review and Deploy

Make sure you:

  • Check for any weird behaviors or glitches.
  • Before you push anything to a live site, back it up!

And there you have it! You’ve created a custom product type in WooCommerce!

Verdict

When running a WooCommerce business you may need to add new custom post type. That's why today we show you two methods that is using our Custom Post WooCommerce Integration plugin from TinySolutions or use raw codes. You need to decide how you want to do that.

Component 2
Tiny Solutions is dedicated to providing innovative and efficient WordPress plugins. Our team focuses on creating tools that simplify and enhance the WordPress experience, ensuring our users have the best possible solutions at their fingertips.