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!
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.
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,
Nice!
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.
Thank you James. I’ve revised the snippet, section #5 should help you
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!
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!
Hi, I can’t set price to the custom product type!! The “General” tab is automatically becoming invisible.
Hi Purnnedu! This may help: https://stackoverflow.com/questions/43109755/how-to-enable-price-and-inventory-for-custom-product-type-in-woocommerce
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?
I guess so, yes
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?
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!
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.
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!
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?
Thank you
That won’t work, no
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?
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
Well Described Rodolfo Melogli π Excellent Post!
Thank you π
Very well described! Rodolfo Melogli.. Adding one para over external/affiliate products will be excellent.. but you did a fantastic job! thanks for information.
Cheers π
Thanks for this! Is there an easy way to display the product type on the order details area and email?
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
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
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 π
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?
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?
Sure Peter, you can just “unset” an existing type by using the same filter: “product_type_selector” π
Where is the code for the WC_Product_Custom class?
Hey Damien, it’s inside this function: bbloomer_create_custom_product_type()
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.
Thank you Pascal π
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.
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 π