WooCommerce: How to Create a Custom Product Type

Ever wondered how you could add a new product type to WooCommerce admin (on top of the default Simple, Variable, Grouped and External)?

Well, while I was coding this for a client I found a lot of literature online – but nothing really worked for the latest WooCommerce release.

So, here’s the working fix!

Add a custom product type to WooCommerce

PHP Snippet: Create a New Product Type @ WooCommerce Admin

/**
 * @snippet       Create a New Product Type @ WooCommerce Admin
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    Woo 6
 * @community     https://businessbloomer.com/club/
 */

// --------------------------
// #1 Add New Product Type to Select Dropdown
 
add_filter( 'product_type_selector', 'bbloomer_add_custom_product_type' );
 
function bbloomer_add_custom_product_type( $types ){
    $types[ 'custom' ] = 'Custom product';
    return $types;
}
 
// --------------------------
// #2 Add New Product Type Class
 
add_action( 'init', 'bbloomer_create_custom_product_type' );
 
function bbloomer_create_custom_product_type(){
    class WC_Product_Custom extends WC_Product {
      public function get_type() {
         return 'custom';
      }
    }
}
 
// --------------------------
// #3 Load New Product Type Class
 
add_filter( 'woocommerce_product_class', 'bbloomer_woocommerce_product_class', 10, 2 );
 
function bbloomer_woocommerce_product_class( $classname, $product_type ) {
    if ( $product_type == 'custom' ) {
        $classname = 'WC_Product_Custom';
    }
    return $classname;
}

// --------------------------
// #4 Show Product Data General Tab Prices

add_action( 'woocommerce_product_options_general_product_data', 'bbloomer_custom_product_type_show_price' );

function bbloomer_custom_product_type_show_price() {
	global $product_object;
	if ( $product_object && 'custom' === $product_object->get_type() ) {
		wc_enqueue_js( "
			$('.product_data_tabs .general_tab').addClass('show_if_custom').show();
			$('.pricing').addClass('show_if_custom').show();
		");
	}
}

// --------------------------
// #5 Show Add to Cart Button

add_action( "woocommerce_custom_add_to_cart", function() {
    do_action( 'woocommerce_simple_add_to_cart' );
});

When should you use a Custom Product Type?

A fan asked this in the comments, so I thought of adding this additional section. The question is: why and when should you use a custom WooCommerce product type?

Well, first of all, the answer is already there. I’m sure you’ve used “WooCommerce Subscriptions” or “WooCommerce Product Bundles” plugins before, and probably noticed that on top of the default Simple, Variable, Grouped and External product type they add their own.

Indeed, the need of using a custom product type comes when you require so much customization that there’s no point in customizing a “Simple” product type for example. In this case, you’d better create your own custom type (mostly if you plan on using this for many products and not just one and let the user play with the product settings).

Finally, having a custom product type allows the user to control its settings. For example, you could create a custom product type that has a checkbox to hide prices and shows a contact form instead. Or a custom product type called “rental”, where it charges a deposit instead of its full price.

Basically, you can do almost anything and creating a custom post type allows you to add options, dropdowns, checkboxes in the Product Edit page so that the user can access them there.

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Add Second Description @ Product Category Pages
    In terms of SEO, if you’re trying to rank your product category pages, you really need to make the most of the default WooCommerce product category “description” and “thumbnail”. Most themes, if compatible with WooCommerce, will show this content right below the product category name and above products. Nothing new so far. But what if […]
  • WooCommerce: Add Column to Orders Table @ WP Dashboard
    The WooCommerce Orders Table, which can be found under WP Dashboard > WooCommerce > Orders, provides us with 7 default columns: Order – Date – Status – Billing – Ship to – Total – Actions. This is used by shop managers to have an overview of all orders, before eventually clicking on a specific one. […]
  • WooCommerce: Display Custom Filters @ WP Dashboard > Products
    If you go to WordPress Dashboard > Products you will find default product admin filters such as “Select a category”, “Filter by product type”, “Filter by stock status”. What if you want to add more custom filters to let your shop managers find products easily? For example, you could add “Filter by product tag” (“product […]
  • WooCommerce: Hide/Show The WP Admin Bar
    In previous WooCommerce versions, new customers could access the WP Admin black bar after purchase. Now this seems fixed. Still, what about other user roles, and what if you want to override this default behavior? Well, here’s a quick snippet for you – feel free to use it in your own WooCommerce site. Enjoy!
  • WooCommerce: Calculate Sales by State
    You’re filing your tax returns and need to know how much you earned in each state… but then find out WooCommerce doesn’t give you this calculation by default within its reports! Don’t worry – today I’ll share a quick snippet so that you can calculate the amount you need in a second. Feel free to […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

35 thoughts on “WooCommerce: How to Create a Custom Product Type

  1. Hi there!
    Excellent! I was able to create a new CPT for my products. How to do it for multiple new custom post types?
    Thank you so much, kind regards,

  2. Thank you for writing this great code. It really helped me BUT

    This code is not working today 2022-05-17.

    After creating a custom product using this code I expect to be able to buy it in my shop. Actually I cannot buy it in my shop because there is no “add to cart” button. There is “Read more” button.

    The normal cause of this is either the product having no price or being out of stock. My product does have a price and is in stock.

    I have spent several hours trying to guess/Google what additional configuration is necessary but I can’t work it out.

    1. Thank you James. I’ve revised the snippet, section #5 should help you

  3. I’ve created a custom product type for discontinued products, which become unpurchasable with no price, and the product type is working perfectly. Except: It seems to be saving information, like price and stock, if the product was originally a simple product before being changed to a discontinued product. Is there anyway to β€œclear” the info from other product types for this product when it’s changed to a discontinued product type? I hope that makes sense!

    1. Hello Rachael, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  4. Hi, I can’t set price to the custom product type!! The “General” tab is automatically becoming invisible.

      1. Does this mean part #4 is obsolete and can be removed from this snippet?
        Instead of #4 you use and change the code from the link mentioned?

  5. how do you make the variations work with custom product type? I displayed the “Used for variations” checkbox, checked it, added the variations but it won’t show on single product page..what could be the issue?

    1. Suresh, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  6. Can you please tell me how can I add a product type query filter option or query source to select individual product type in elementor product widget. I mean the products will be shown by choosing product type.

    1. Hi Rafayan, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  7. Hi,

    Thanks for the post. I have one question. If I have to check whether the cart item is the custom product type then will a function like the following work or do I need to write the function?

    $cart_item['data']->is_custom()

    Thank you

    1. That won’t work, no

  8. Hi Rodolfo! Thanks for the excellent tutorial! I can see the new product type but not the tabs General and Inventory, so I can not set the price and the stock… What it is missing?

    1. Hi Agostino! The “General” tab is actually there, it’s just hidden. WooCommerce thinks it is a grouped product and displays that as none. You will need custom JS or custom CSS to make them show. Hope this helps

  9. Well Described Rodolfo Melogli πŸ™‚ Excellent Post!

    1. Thank you πŸ™‚

  10. Very well described! Rodolfo Melogli.. Adding one para over external/affiliate products will be excellent.. but you did a fantastic job! thanks for information.

    1. Cheers πŸ™‚

  11. Thanks for this! Is there an easy way to display the product type on the order details area and email?

    1. Hey Aaron – thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  12. Hi Rodolfo,

    I am using your code because I have hidden the Add To Cart button for all Simple Products (stupid client request… Don’t ask.), but there is a selection of products that should have an Add To Cart Button… It seems to work when I go to create a product, except 1) the General tab disappears when I select the Custom product type, so I can’t set a price, and 2) Publishing or Updating the product makes it revert back to Simple… See screenshot: https://i.imgur.com/MSb9Y47.png

    1. Hey Joe, thanks for your comment! There is additional PHP to add if you want the custom type to show the Add to Cart etc., but unfortunately this is not part of this post. In regard to the other issue, try updating your PHP, it could be because of that πŸ™‚

    2. I’m actually having the exact same issue. I’m using PHP 7.1.7, WordPress 4.9.6 and WooCommerce 3.4.1.

      Any recommendation?

  13. Hey Rodolfo,

    Thank you for giving us so much ideas. One question: if I want to delete a product type to clean things up, how can I do that? Or it’s not recommended to delete them?

    1. Sure Peter, you can just “unset” an existing type by using the same filter: “product_type_selector” πŸ™‚

  14. Where is the code for the WC_Product_Custom class?

    1. Hey Damien, it’s inside this function: bbloomer_create_custom_product_type()

  15. As usual nice to learn from your posts. I spent my days on developing new ideas for plugins and better performance and you help me a lot by sharing. Request: how to move the coupon field in checkout after summary of products instead of on top. This item has never been publiced by anyone yet.

    1. Thank you Pascal πŸ™‚

  16. Nice post. Can you give an example of why a new custom product would be needed? Aside from subscriptions, the simple, grouped, bundled products are all I’ve ever seen use cases for.

    1. Jonathan, thanks for your comment and good point! I’m just about to add a new paragraph to the tutorial that should help you with that πŸ™‚

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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