Home » WooCommerce: How to Create a Custom Product Type

WooCommerce: How to Create a Custom Product Type

by Tutor Aspire

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 tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    Woo 6
 * @donate $9     https://tutoraspire.com
 */

// --------------------------
// #1 Add New Product Type to Select Dropdown
 
add_filter( 'product_type_selector', 'tutoraspire_add_custom_product_type' );
 
function tutoraspire_add_custom_product_type( $types ){
    $types[ 'custom' ] = 'Custom product';
    return $types;
}
 
// --------------------------
// #2 Add New Product Type Class
 
add_action( 'init', 'tutoraspire_create_custom_product_type' );
 
function tutoraspire_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', 'tutoraspire_woocommerce_product_class', 10, 2 );
 
function tutoraspire_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', 'tutoraspire_custom_product_type_show_price' );

function tutoraspire_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.

You may also like