Site icon Experience Wordpress Developer Online – Design Develop and Maintenance Support

How to add a new custom product type at WooCommerce Admin

WooCommerce has four product types, Simple Product, Grouped Product, External/Affiliate Product and Variable Product. In many cases, the default product types are not enough. You might need to add new product types such as Subscription product, Membership product, Bookable product & so on. In this post, we will try to add a new custom product type in WooCommerce:Custom product

So, here is code. which you can add in your theme’s functions.php file of your active child theme (or theme).

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

The most basic form of adding a new product type! You will now be able to select your new product type from the drop down on the edit product pages.

Exit mobile version