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
 * @source        https://businessbloomer.com/?p=73644
 * @author        Rodolfo Melogli
 * @compatible    Woo 3.3.3
 */

// --------------------------
// #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;
}

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.

Share:

Leave a Reply